The previous filtering algorithm is modified, and the Copymakeborder expansion boundary pixels are adopted, which brings great convenience to the processing of boundary pixels.
<span style= "FONT-SIZE:14PX;" >void Blur (const Mat &IMAGE_SRC, Mat &image_dst, Size size_aperture) {/////////////////////////////step 1. Reassign the image (if needed)///////////////////////////////////////////////the size of the new image 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///////////////////////// Step 2. Create a new image (expand the source image)//////////////////////////////////////////////expanded size int width_extend=size_ Aperture.width>>1;int height_extend=size_aperture.height>>1;//Create a new image </span>
<span style= "FONT-SIZE:14PX;" ><span style= "White-space:pre" ></span>//here with OpenCV in the same way Border_defaultmat image_new; Copymakeborder (Image_src,image_new,height_extend,height_extend,width_extend,width_extend,border_default);///// Step 3. Filter////////////////////////////////////////////////new Image parameter int Width_new=image_ New.cols;int height_new=image_new.rows;int widthstep_new=width_new;//sliding window int width_aperture=size_aperture.width; int Height_aperture=size_aperture.height;int pixelcount=width_aperture*height_aperture;//calculates the starting coordinate int startX= of the filtered pixels Width_aperture>>1;int starty=height_aperture>>1;//Each column's grayscale value and int *sum_percol=new int[width_New];// Filtering the new image Uchar *row_new=image_new.data+starty*widthstep_new+startx;uchar *row_dst=image_dst.data;//first row Uchar *row_ aperture_new=image_new.data;for (int y=starty;y<=height_new-starty-1;++y) {//column Uchar *col_dst=row_dst;uchar *col_ aperture_new=row_aperture_new;//calculates the grayscale value of height_aperture pixels per column and//First row, calculates all columns and if (Y==starty) {for (int K=0;k<=width_new-1;++k) {sum_percol[k]=0;//The first pointer of each column UCHAR *col_perline=col_aperture_new+k;for (int t=0;t<= Height_aperture-1;++t) {sum_percol[k]+=col_perline[0];col_perline+=widthstep_new;//The next line}}}else//not the first line {for (int k=0; K<=WIDTH_NEW-1;++K) {//The first pointer of each column Uchar *col_=col_aperture_new+k;sum_percol[k]-=col_[0-widthstep_new];//minus the top sum_ percol[k]+=col_[0+ (height_aperture-1) *widthstep_new];//Add the following}}//calculate the column sum of the width_aperture rows int sum_aperture=0;for (int x= STARTX;X<=WIDTH_NEW-STARTX-1;++X) {//first element of each line, width_aperture columns and if (X==STARTX) {for (int k=0;k<=width_ APERTURE-1;++K) {sum_aperture+=sum_percol[k];}} else//not the first element {//minus the left sum_aperture-=sum_percol[x-startx-1];//plus the right Sum_aperture+=sum_percol[x+startx];} Averaging uchar meanvalue=sum_aperture/pixelcount;col_dst[0]=meanvalue;//sliding one pixel col_dst++;//col_new++;// There is no need to slide the source image pointer}//the next line row_dst+=width_dst;row_aperture_new+=width_new;}} </span>
Test results:
Original
Results
High-efficiency mean filtering (using Copymakeborder to process boundary pixels)