The median filtering algorithm in the previous filter is modified, and the boundary pixels are processed by copymakeborder.
void Medianblur (const mat &IMAGE_SRC, Mat &image_dst, int width_aperture) {/////////////////////////////redistribution image (if required)/////////////////////////////////////////////int Width_dst=image_src.cols;int height_Dst=image_Src.rows; Image_dst.create (Size (WIDTH_DST,HEIGHT_DST), CV_8UC1);//If reassigned, the previous space is discarded Image_dst.setto (Scalar (0));//Set to 0// sliding window int pixelcount=width_aperture*width_aperture;//The total number of pixels in the window Mat image_aperture (width_aperture,width_aperture,cv_ 8UC1)///Slide window image//histogram mat histogram;int histogramsize=256;//gray level int THRESHOLDVALUE=PIXELCOUNT/2+1;//STEP1. Set the Threshold value ( Step reference: High-efficiency programming points of the image four)//calculate the starting point coordinate int startx=width_aperture/2;int starty=width_aperture/2;/////////////////////////////// Expand the image boundary///////////////////////////////////////////int Height_extend=starty;int width_extend=startx; Mat Image_new;copymakeborder (image_src,image_new,height_extend,height_extend,width_extend,width_extend,border_ default);//Border_reflectint height_new=image_new.rows;int width_new=image_new.cols;//First line///Here you need to set 3 pointers, slide together/ 1. Pixels processed in the source image//2. MeshThe pixel//3 The marked image is processed. Source Image Sliding window Uchar *row_new=image_new.data+starty*width_new+startx;//New Image Uchar *row_dst=image_dst.data;// Target image Uchar *row_aperture_new=image_new.data;//the sliding window in the source image for (int y=starty;y<=height_new-starty-1;++y) {//Column Uchar * Col_new=row_new;uchar *col_dst=row_dst;uchar *col_aperture_new=row_aperture_new;//operation of the entire sliding window////////////////////// For sliding window operations/////////////////////////////////////////////calculates the first sliding window histogram per row//Extract sliding window image Uchar *row_aperture=image_ aperture.data;//sliding window image uchar *row_aperture_new_2=col_aperture_new;for (int k=0;k<=width_aperture-1;++k) {// Column Uchar *col_apertureimage=row_aperture;uchar *col_aperture_new_2=row_aperture_new_2;for (int w=0;w<=width_ APERTURE-1;++W) {//process each pixel col_apertureimage[0]=col_aperture_new_2[0];//next pixel col_apertureimage++;col_aperture_new _2++;} Next line Row_aperture+=width_aperture;row_aperture_new_2+=width_new;} Step 2. Determine the median value and record the number of pixels in the value of the luminance <=//Histogram calchist (&image_aperture, 1,//mat number 0,//used to calculate the histogram of the channel index, the channel index in order to row the mat (),// Mat () returns a null value, indicating no mask,histogram,//histogram 1,//StraightFanthou dimension, if 2 histogram is calculated, it is 2&histogramsize,//histogram rank number (such as gray level), that is, the number of rows per column 0//component variation range);//The number of pixels in the luminance median and <= value int Medianvalue , Pixlecountlowermedian; Calculateimage_mediangray_pixelcount (Histogram,pixelcount,medianvalue,pixlecountlowermedian);///////////////// Sliding window operation End//////////////////////////////////////////////filter col_dst[0]= (UCHAR) medianvalue;//slide one pixel ( Three pointers moving together) col_dst++;col_new++;col_aperture_new++;for (int x=startx+1;x<=width_new-startx-1;++x)//start with the second filter pixel per line {//////////////////////////////////Maintain sliding window histogram//////////////////////////////////////////step 3. Remove left Uchar *col_Left =col_aperture_new-1;float *data= (float*) histogram.data;for (int k=0;k<=width_aperture-1;++k) {int gray=col_Left[ 0];d ata[gray]-=1.0;if (gray<=medianvalue) {pixlecountlowermedian--;} Col_left+=width_new;} Step 4: Add right Uchar *col_right=col_aperture_new+width_aperture-1;for (int k=0;k<=width_aperture-1;++k) {int gray= Col_right[0];d ata[gray]+=1.0;if (gray<=medianvalue) {pixlecountlowermedian++;} Col_right+=width_new;} //Search for the new median if (Pixlecountlowermedian>thresholdvalue)//step 6. {while (1) {pixlecountlowermedian-=data[medianvalue];medianvalue--;if (pixlecountlowermedian<=thresholdvalue) { Break;}}} Else{while (pixlecountlowermedian<thresholdvalue)//step 5{medianvalue++;p ixlecountlowermedian+=data[ Medianvalue];}} Filter col_dst[0]= (UCHAR) medianvalue;//slide one pixel col_new++;col_dst++;col_aperture_new++;} End of x//next line row_new+=width_new;row_dst+=width_dst;row_aperture_new+=width_new;} End of Y}
Original:
High-efficiency median filtering (using Copymakeborder to process boundary pixels)