Getting started with opencv 7. Image Filtering

Source: Internet
Author: User

 

Filtering is actually a concept in signal processing, and the image itself can also be seen as a two-dimensional signal. The level of the pixel gray value indicates the signal strength.

High frequency: the sharp gray-scale changes in the image.

Low Frequency: Flat points in the image with little gray variation.

Based on the features of high-frequency and low-frequency images, we can design corresponding high-pass and low-pass filters to detect sharp and obvious changes in the image. Low-pass filtering can smooth the image, filter out the noise in the image.

I. Low-Pass Filtering

1. Blur Functions

This function is a function for smoothing an image. It replaces the gray scale of a vertex with the average gray scale value of the pixel in the neighbor of a vertex.

cv::blur(image,result,cv::Size(5,5));

2. Gaussian blur

The above blur smoothing principle is to replace the current gray value with the average value in the neighborhood, but we often hope that the closer we get to the pixel, the higher the weight, in this way, Gaussian fuzzy filtering is generated. Its filter or mask is a two-dimensional matrix of Gaussian distribution.

cv::GaussianBlur(image,result,cv::Size(5,5),1.5);

The parameter image is the input image, and the result is the output image. Size (5, 5) defines the kernel size. The last parameter illustrates the variance of Gaussian Kernel.

3. Median Filter

The two filters mentioned above are the gray values of the current vertex after adding the pixels in the neighborhood according to a weight. This operation is also called convolution. Such a filter is called a linear filter, there is also a non-linear filter, such as the median filter, which takes the values of all pixels in the neighborhood as the gray value of the current point.

Median ({1, 2, 3, 7, 5,}) = 3.

cv::medianBlur(image,result,5);

The last parameter specifies the size of the neighborhood to 5*5. Median Filter is also the most widely used smoothing filter in practice. It can effectively remove interference such as pretzels.

Next we will compare the effects of the above three filters:

 1 #include <opencv2/core/core.hpp> 2 #include <opencv2/highgui/highgui.hpp> 3 #include <opencv2/imgproc/imgproc.hpp> 4 int main() 5 { 6 using namespace cv; 7 Mat image=imread("../cat.png"); 8 cvtColor(image,image,CV_BGR2GRAY); 9 Mat blurResult;10 Mat gaussianResult;11 Mat medianResult;12 blur(image,blurResult,Size(5,5));13 GaussianBlur(image,gaussianResult,Size(5,5),1.5);14 medianBlur(image,medianResult,5);15 namedWindow("blur");imshow("blur",blurResult);16 namedWindow("Gaussianblur");imshow("Gaussianblur",gaussianResult);17 namedWindow("medianBlur");imshow("medianBlur",medianResult);18 waitKey();19 return 0;20 }

Ii. Qualcomm Filtering: Edge Detection

One of the best applications of Qualcomm filter is edge detection. From the analysis at the beginning of the article, we can see that the high frequency is the place where the changes in the image are intense, so the Edge Area of the image exactly meets this characteristic, we can use Qualcomm filter to expose the edge of the image and further calculate some features of the image.

Edge detection was originally intended to write an article as a separate topic. However, due to the complicated and large length of content, therefore, we first use Sobel Edge Detection in Qualcomm filtering as an example. Later, we will write this article as a separate example.

In fact, opencv provides the Sobel edge detection function, but on the one hand, the threshold seems to be not very good, on the other hand, it does not refine the last edge, so the effect is not very satisfactory, this article is written by imitating algorithms in MATLAB. For relevant theories, refer to my original article visual algorithm: Sobel edge detection.

The following is the c ++ Code implemented by Sobel:

1 bool Sobel (const mat & image, mat & result, int type) 2 {3 if (image. channels ()! = 1) 4 return false; 5 // The coefficient is set to 6 int kx (0); 7 int KY (0); 8 If (type = sobel_horz) {9 kx = 0; ky = 1; 10} 11 else if (type = sobel_vert) {12 kx = 1; Ky = 0; 13} 14 else if (type = sobel_both) {15 kx = 1; Ky = 1; 16} 17 else18 return false; 19 // set mask20 float mask [3] [3] = {1, 1 }, {0, 0}, {-1,-2,-1 }}; 21 mat y_mask = MAT (3,3, cv_32f, mask)/8; 22 mat x_mask = y_mask.t (); // transpose 23 // calculate the 24 mat sobelx, sobely, and 25 filter2d (image, sobelx, cv_32f, x_mask); 26 filter2d (image, sobely, cv_32f, y_mask); 27 sobelx = ABS (sobelx); 28 sobely = ABS (sobely ); 29 // gradient chart 30 mat gradient = kx * sobelx. mul (sobelx) + Ky * sobely. mul (sobely); 31 // calculation threshold 32 int scale = 4; 33 double cutoff = scale * mean (gradient) [0]; 34 result. create (image. size (), image. type (); 35 result. setto (0); 36 for (INT I = 1; I <image. rows-1; I ++) 37 {38 float * sbxptr = sobelx. PTR <float> (I); 39 float * sbyptr = sobely. PTR <float> (I); 40 float * preptr = gradient. PTR (I-1); 41 float * curptr = gradient. PTR <float> (I); 42 float * lstptr = gradient. PTR <float> (I + 1); 43 uchar * rstptr = result. PTR <uchar> (I); 44 // threshold and maximum suppression 45 for (Int J = 1; j <image. cols-1; j ++) 46 {47 If (curptr [J]> cutoff & (48 (sbxptr [J]> kx * sbyptr [J] & curptr [J]> curptr [J-1] & curptr [J]> curptr [J + 1]) | 49 (sbyptr [J]> Ky * sbxptr [J] & curptr [J]> preptr [J] & curptr [J]> lstptr [J]) 50 rstptr [J] = 255; 51} 52} 53 return true; 54}

 

Getting started with opencv 7. Image Filtering

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.