Opencv2 Series Learning Notes 8 (Image Filtering)

First: Concept:

Filtering is a basic operation in image processing of signal processor. The filter removes the noise in the image, extracts the features of interest, and allows the image to be re-sampled.

Frequency domain and spatial domain: Spatial domain refers to describing an image with the gray value of the image; Frequency domain refers to describing an image with the change of the gray value of the image. The concepts of low-pass filter and high-pass filter are generated in frequency domain.

Low-pass filter refers to the removal of high-frequency components in the image, while high-pass filter refers to the removal of low-frequency components in the image.

Later, we will introduce low-pass filters - mean and Gauss filters; median filters - non-linear filters; high-pass filters - sobel operators (directional filters) and Laplace transforms (second derivatives). The edge of image can be detected by directional filter and Laplace transform.

II: Low Pass Filter

< 1 > cv:: blur function: each pixel is replaced by the average of the pixels in the adjacent rectangle

< 2 > cv:: Gaussian Blur function: substitution by Gaussian kernels

Code:

  1. Mat result;                              //Linear smoothing filter replacing each pixel with the average of the pixels in the adjacent rectangle  
  2.     blur(image, result,Size(5,5));         //filter2D can customize kernels for linear filtering  
  3.   
  4.     Mat gauResult;  
  5.     GaussianBlur(image, gauResult, Size(5,5), 1.5);    //Gauss Smoothing Fuzzy Linear Filter  

Result:

sourceImage:

BlurResult:


GaussianBlur:


The effect of low-pass filter is to blur and smooth the image, which weakens the rapid change of object edge visibility. It is a linear filter, the principle is to convolute with the core. At this time, the core verification, when we need to specify the kernel function for convolution, we can use the filter 2D function. Its use can be seen in blog: http://blog.csdn.net/lu597203933/article/details/16811851.  

3: Median filter

Median filter is a non-linear filter. Its principle is to only calculate the median of this group and replace the current pixel value with the median. So it is very useful for removing salt and pepper noise.

Code:

  1. Mat medianResult;  
  2.     medianBlur(image, medianResult,5);  //A non-linear filter, using median to replace the current pixel value, is particularly useful for removing salt and pepper noise.  

Result:

 

3: Directional filter-sobel operator

Sobel operator calculates the first derivative of an image by convolution operation. Because the gray level of the image at the edge changes greatly, Sobel operator can be used for edge detection. The core of Sobel operator is defined as:

y coordinate axis:


x coordinate axis:


Sobel function: Sobel (Input Array src, Output Array dst, int ddepth,

                         int dx, int dy, int ksize=3,

                         doublescale=1, double delta=0,

                         int borderType=BORDER_DEFAULT );

The ddepth is the image type, (dx,dy) = (1,0) is the x-direction derivative, (dx,dy) = (0,1) is the y-direction derivative. The function of scale and delta is to zoom the image before saving. The formula is: dst =dst * scale + delta.

Code:

  1. int main()  
  2. {  
  3.     Mat image = imread("F:\\lena.png", 0);  
  4.     if(!image.data)  
  5.     {  
  6.         cout << "Fail to load image" << endl;  
  7.         return 0;  
  8.     }  
  9.     Mat sobel_x, sobel_y;  
  10.     //Sobel(image, sobel_x, CV_8U, 1, 0, 3, 0.4, 128);  
  11.     //Sobel(image, sobel_y, CV_8U, 0, 1, 3, 0.4, 128);  
  12.   
  13.     Sobel(image, sobel_x, CV_16S, 1, 0);  //Since the latter is to be added up, it is expressed as a 16-bit signed integer, and the derivative must contain a negative number.  
  14.     Sobel(image, sobel_y, CV_16S, 0, 1);  
  15.     Mat sobel;  
  16.     sobel = abs(sobel_x) + abs(sobel_y);  
  17.   
  18.     double sobmin, sobmax;  
  19.     minMaxLoc(sobel, &sobmin, &sobmax);  
  20.     Mat sobelImage;  
  21.     sobel.convertTo(sobelImage, CV_8U, -255.0/sobmax, 255);   //Equivalent to saturate_cast(a*sobel+b)  
  22.       
  23.     Mat sobelThresholded;  
  24.     int thre = 200;  
  25.     threshold(sobelImage, sobelThresholded, thre, 255, THRESH_BINARY);  
  26.   
  27.     namedWindow("sobelImage", 0);  
  28.     imshow("sobelImage", sobelImage);  
  29.     namedWindow("sobelThresholded", 0);  
  30.     imshow("sobelThresholded", sobelThresholded);  
  31.   
  32.     waitKey(0);  
  33.     return 0;  
  34. }  

Result:


Of course, in addition to sobel operator, there are other operators, such as Scharr operator, which is more accurate and faster. Its core is:

 

Sobel(image, sobelX, CV_16S, 1, 0,CV_SCHARR);

Or:

Scharr(image, scharrX, CV_16S, 1, 0, 3);

Fourth: Laplacian transformation

High-pass filters based on image derivatives are used to calculate second-order derivatives to measure image curvature when Laplace changes.

Code:

  1. int main()  
  2. {  
  3.     Mat image = imread("F:\\lena.png", 0);  
  4.     if(!image.data)  
  5.     {  
  6.         cout << "Fail to load image" << endl;  
  7.         return 0;  
  8.     }  
  9.     Mat laplacian;  
  10.     Laplacian(image, laplacian, CV_16S, 3);  //The second derivative must contain negative numbers. High-pass filter based on image derivative calculates second-order derivative to measure image curvature  
  11.     laplacian = abs(laplacian);  
  12.     Mat laplacianImage;  
  13.     laplacian.convertTo(laplacianImage, CV_8U);  
  14.     namedWindow("laplacianImage");  
  15.     imshow("laplacianImage", laplacianImage);  
  16.     waitKey(0);  
  17.     return 0;  
  18.   
  19. }  

Result:


Author: Villager from: http://blog.csdn.net/lu597203933 Reprint or share are welcome, but be sure to state the source of the article. (Sina Weibo: small village leader zack, welcome to exchange!)

Posted by undertaker16 on Wed, 03 Apr 2019 12:51:31 -0700