OpenCv Learning Chapter - Image Mean Filtering Algorithm

1. Algorithm principle

    Value filtering is a typical linear filtering algorithm. It refers to giving a template to the target pixel on the image, and the template includes its surrounding adjacent pixels (8 pixels around the target pixel as the center constitute a filtering template, that is, remove the The target pixel itself), and then replace the original pixel value with the average value of all pixels in the template.

2. Source code

#include <opencv2\highgui\highgui.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2/opencv.hpp>  
#include<ctime>  
using namespace cv;
using namespace std;


//均值滤波  
void AverFiltering(const Mat &src, Mat &dst) {
if (!src.data) return;
//at访问像素点  
for (int i = 1; i<src.rows; ++i)
for (int j = 1; j < src.cols; ++j) {
if ((i - 1 >= 0) && (j - 1) >= 0 && (i + 1)<src.rows && (j + 1)<src.cols) 
{//边缘不进行处理  
dst.at<Vec3b>(i, j)[0] = (src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i - 1, j - 1)[0] + src.at<Vec3b>(i - 1, j)[0] + src.at<Vec3b>(i,j - 1)[0] +
src.at<Vec3b>(i - 1, j + 1)[0] + src.at<Vec3b>(i + 1, j - 1)[0] + src.at<Vec3b>(i + 1, j + 1)[0] + src.at<Vec3b>(i, j + 1)[0] +
src.at<Vec3b>(i + 1, j)[0]) / 9;
dst.at<Vec3b>(i, j)[1] = (src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i - 1, j - 1)[1] + src.at<Vec3b>(i - 1, j)[1] + src.at<Vec3b>(i, j - 1)[1] +
src.at<Vec3b>(i - 1, j + 1)[1] + src.at<Vec3b>(i + 1, j - 1)[1] + src.at<Vec3b>(i + 1, j + 1)[1] + src.at<Vec3b>(i, j + 1)[1] +
src.at<Vec3b>(i + 1, j)[1]) / 9;
dst.at<Vec3b>(i, j)[2] = (src.at<Vec3b>(i, j)[2] + src.at<Vec3b>(i - 1, j - 1)[2] + src.at<Vec3b>(i - 1, j)[2] + src.at<Vec3b>(i, j - 1)[2] +
src.at<Vec3b>(i - 1, j + 1)[2] + src.at<Vec3b>(i + 1,j - 1)[2] + src.at<Vec3b>(i + 1, j + 1)[2] + src.at<Vec3b>(i, j + 1)[2] +
src.at<Vec3b>(i + 1, j)[2]) / 9;
}
else {//边缘赋值  
dst.at<Vec3b>(i, j)[0] = src.at<Vec3b>(i, j)[0];
dst.at<Vec3b>(i, j)[1] = src.at<Vec3b>(i, j)[1];
dst.at<Vec3b>(i, j)[2] = src.at<Vec3b>(i, j)[2];
}
}
}
//图像椒盐化  
void salt(Mat &image, int num) {
if (!image.data) return;//防止传入空图  
int i, j;
srand(time(NULL));
for (int x = 0; x < num; ++x) {
i = rand() % image.rows;
j = rand() % image.cols;
image.at<Vec3b>(i, j)[0] = 255;
image.at<Vec3b>(i, j)[1] = 255;
image.at<Vec3b>(i, j)[2] = 255;
}
}
void main() {
Mat image = imread("fzh.jpg");


Mat Salt_Image;
image.copyTo(Salt_Image);
salt(Salt_Image, 3000);


Mat image1(image.size(), image.type());
Mat image2;
AverFiltering(Salt_Image, image1);
blur(Salt_Image, image2, Size(3, 3));//The mean filter function that comes with the openCV library  
imshow("original image", image);
imshow("salt image", Salt_Image);
imshow("custom mean filter", image1);
imshow("openCV The built-in mean filter", image2);
waitKey();

}

1> The if judgment in the double-layer for loop is to not deal with the noise of the edge image

2>dst.at<Vec3b>(i, j)[0] This expression dst is a mat variable, vec3b means vector, then the coordinate point and the 0th channel, because it is a color image, it is RGB three channels


3. Program running effect

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325755661&siteId=291194637