1.源码实现
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
/*平均值池化*/
int main()
{
Mat srcImage = imread("3.jpg");
int width, height;
int n = 9;
int r, g, b;
width = srcImage.cols;
height = srcImage.rows;
Mat dstImage(width, height, CV_8UC3);
//遍历所有像素, 并设置像素值
for(int i=0; i<height/n; i++)
{
for(int j=0; j<width/n; j++)
{
r = 0;
g = 0;
b = 0;
for(int u=0; u<n; u++)
{
for(int v=0; v<n; v++)
{
b += srcImage.at<Vec3b>(n*i+u, n*j+v)[0];
g += srcImage.at<Vec3b>(n*i+u, n*j+v)[1];
r += srcImage.at<Vec3b>(n*i+u, n*j+v)[2];
}
}
for(int u=0; u<n; u++)
{
for(int v=0; v<n; v++)
{
dstImage.at<Vec3b>(n*i+u,n*j+v)[0] = saturate_cast<uchar>(b / (n*n));
dstImage.at<Vec3b>(n*i+u,n*j+v)[1] = saturate_cast<uchar>(g / (n*n));
dstImage.at<Vec3b>(n*i+u,n*j+v)[2] = saturate_cast<uchar>(r / (n*n));
}
}
}
}
imwrite("mean_pooling.jpg", dstImage);
return 0;
}
2.编译源码
$ g++ -o test test.cpp -std=c++11 -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -Wl,-rpath=/usr/local/lib
3.运行结果
![](https://img.haomeiwen.com/i16386400/6c6bba3886f7bcb3.jpg)
3.jpg
![](https://img.haomeiwen.com/i16386400/40882f1d70c920b5.jpg)
mean_pooling.jpg
网友评论