Image filtering is mostly used for image blurring and noise reduction (personal understanding). Common filters are: mean filter, median filter, Gaussian filter, etc. Here I mainly introduce the median filter, which is very effective in dealing with salt and pepper noise. Let me briefly introduce its principle and the median filter function I wrote myself.
Mean filter: The median filter is processed for a point on a pair of images, the pixels of the desired pixel in the mask and the pixel values of its neighborhood are sorted, the median value is determined, and the median value is assigned to the pixel. As shown in a 5 x 5 neighborhood, the median value of 124 is used instead of the center pixel value.
Mat Medianfilter (Mat img)//median filter {int count = 0;
Number of pixels in the field int index=0; int pixel1[9];
Array holds pixel value int pixel2[9];
int pixel3[9]; for (int x = 1, x < img.rows-1, x + +)//traversal pixel, and the median value of the neighborhood pixel instead of the pixel {for (int y = 1; y < img.cols-1; y++) {count
= 0;
for (int row =-KERNEL_SIZE/2; row <= kernel_size/2; row++)//Compute neighborhood pixels {int row2 = x + row;
for (int col =-kernel_size/2; col <= kernel_size/2; col++) {int col2 = y + col; Pixel1[count] = * (Img.data + img.step[0] * row2 + img.step[1] * col2);
Place the neighborhood pixels into the array pixel2[count] = * (Img.data + img.step[0] * row2 + img.step[1] * col2 + img.elemsize1 ());
Pixel3[count] = * (Img.data + img.step[0] * row2 + img.step[1] * col2 + img.elemsize1 () * *);
count++; }} bubblingsort (Pixel1,count);
Sort Bubblingsort (Pixel2, Count);
Bubblingsort (Pixel3, Count); * (Img.data + img.step[0] * x + img.step[1] * y) = PIXEL1[COUNT/2]; Replace this pixel with the median of the neighborhood pixel * (Img.data + img.steP[0] * x + img.step[1] * y + img.elemsize1 ()) = PIXEL2[COUNT/2];
* (Img.data + img.step[0] * x + img.step[1] * y + img.elemsize1 () * = PIXEL3[COUNT/2];
}} return img; }
Note: The edge pixels of the image are not processed in my code, and because the input and output are the same picture, the edge pixels remain the original pixels.
Operation Result:
Original (with salt and pepper noise): After processing (median filter):
Obviously the median filter to remove salt and pepper noise has a clear role, but because of limited capacity, and did not completely achieve the effect of removal, and feel that some of the details are missing, welcome to the great God at any time to add communication.
Reprint please indicate the source. Thank you