preface
- In this article we are going to introduce filtering,
The theory of
Smooth/Blur is the simplest and most common operation in image processing, which can reduce the noise during image preprocessing. Image smoothing often makes the boundary and contour of the image become blurred, because the image is averaged or integrated. From the perspective of frequency domain, the essence of image blurring is that its high-frequency components are attenuated
F (I,j) represents an image, the pixel of the ith row and j column, h(k, L) is the convolution kernel/convolution operator, and the k L size is also called the window size. Within the range of K L, f(I,j) and H (k, L) multiply each value to obtain a new pixel value and output the image G (I,j).
Convolution processThere is a 3×3 window above 6×6, moving from left to right and from top to bottom. The sum of the values of each yellow pixel is averaged and assigned to the central red pixel as the new pixel value after its convolution processing. Move it one pixel at a time.
Filtering is divided into two categories: linear filtering and nonlinear filtering
- Linear filtering: box filtering, mean filtering, Gaussian filtering
- Nonlinear filtering: median filtering, bilateral filtering
The mean of fuzzy
The disadvantage of mean filtering is that it can not protect the details well. When the image is desiccated, it also destroys the details of the image, so that the image becomes blurred and the noise cannot be removed well.
/* blur */ - (UIImage*)kj_opencvBlurX:(int)x y:(int)y{CV ::Mat SRC, DST; UIImageToMat(self,src,true); Blur (SRC, DST, CV ::Size(x,y)); return kMatToUIImage(dst); }Copy the code
Gaussian blur
Gaussian filtering can eliminate gaussian noise and is widely used in image processing. Gaussian filtering, Size The Size of the Gaussian kernel must be a positive odd number
/* UIImage*)kj_opencvGaussianBlurX:(int)x y:(int)y{CV ::Mat SRC, DST; UIImageToMat(self,src,true); // GaussianBlur(SRC, DST, CV ::Size(x,y), 0); return kMatToUIImage(dst); }Copy the code
The median fuzzy
Basic idea is used in the areas of pixel gray value instead of the gray levels of pixels, this method in the removal of impulse noise, salt and pepper noise while preserving image details (in the case of no edge blur) the thought of the median filter and mean filter looks very similar, is only an average, a take the median
*/ - (UIImage*)kj_opencvMedianBlurksize:(int)ksize{CV ::Mat SRC, DST; UIImageToMat(self,src,true); // medianBlur(SRC, DST, ksize); return kMatToUIImage(dst); }Copy the code
Gaussian bilateral blur
The biggest feature of bilateral filter is edge preservation, which can make skin whitening effect. Bilateral filter can save image edge details well and filter out low-frequency noise. Sigma <10 has little influence on the filter, but if sigma>150, it will have a great influence on the filter, making the picture look like a cartoon
/ * bilateral gaussian blur, can do exfoliating whitening effect * / - (UIImage *) kj_opencvBilateralFilterBlurRadio: (int) radio sigma (int) sigma {CV: : Mat SRC, DST; UIImageToMat(self,src,true); src = kFourChannelsBecomeThree(src); // If sigma<10, it has little influence on the filter; if sigma>150, it has a great influence on the filter. BilateralFilter (SRC, DST, Radio, Sigma, 5); bilateralFilter(SRC, DST, Radio, Sigma, 5); dst = kPromoteImageContrast(dst); return kMatToUIImage(dst); }Copy the code
Taken together, bilateral filtering is the clearest of all
Custom linear blur
Filter2D customizes the convolution kernel and then achieves the blur effect
/* UIImage* / - (UIImage*)kj_opencvCustomBlurksize:(int)ksize{CV ::Mat SRC, DST; UIImageToMat(self,src,true); if (! (ksize%2)) ksize++; Mat kernel = Mat::ones(CV ::Size(ksize,ksize), CV_32F/(float)(ksize*ksize)); filter2D(src, dst, -1, kernel); return kMatToUIImage(dst); }Copy the code
rendering
Gaussian bilateral blur effect:
Serial article association
1, how to use OpenCV in iOS project 2, iOS using OpenCV to adjust the image brightness and contrast (1) 3, iOS using OpenCV image fusion (2) 4, iOS using OpenCV filter processing (3)
Then I will slowly added Opencv related articles, temporarily to common image processing and image algorithms encapsulation, friends in need can be to pod ‘KJExtensionHandler/Opencv’