# Opencv2 series of learning notes 8 (image filtering)

**WhatsUp Gold**.

**Not end users.**

A: Concept:

Filtering is a basic operation in image processing, signal processor. Filtering the noise removal in images, feature extraction of interest, allowing the image resampling.

The image in the spatial domain and frequency domain: spatial domain refers to the use of gray scale value image; to describe an image; but the frequency domain refers to the image gray value changes to describe a picture. The concept and the low-pass filter and high pass filter is generated in the frequency domain.

Low pass filter to remove high frequency components in the image, while the high pass filter to remove the low frequency components in the image.

Behind the low pass filter, value and Gauss filter; value filter; nonlinear filter; highpass filter - Sobel operator (filter) and Laplasse transform (two derivative). The use of directional filter and Laplasse transform can detect the edge of image.

Two: low pass filter

<1>The cv:: blur function: each pixel is replaced by the average pixel adjacent rectangular.value

<2>The cv:: GaussianBlur function: the Gauss kernel to replace

Code:

Mat result; // Replace the linear smoothing filter each pixel as the average value of pixel adjacent rectangular. blur(image, result,Size(5,5)); // Filter2D can customize the kernel of linear filtering Mat gauResult; GaussianBlur(image, gauResult, Size(5,5), 1.5); // Gauss smooth fuzzy linear filter

Result:

sourceImage:

BlurResult:

GaussianBlur:

The low pass filter effect is fuzzy and smoothing the image, weaken the rapid change of the edges of objects visible. It is a linear filter, principle of convolution and nucleus, the nuclear internal, when we need to specify the kernel function can be used filter2D function convolution, it use blog: .

Three: value filter

value filter is a nonlinear filter, it is the principle of just calculate the number of groups, and value value replace pixel value current;. Therefore, for the removal of salt and pepper noise is very useful.

Code:

Mat medianResult; medianBlur(image, medianResult,5); // Filter is a nonlinear, to replace the current pixel value by the median, for the removal of salt and pepper noise is particularly useful

Result:

Three: directional filter - Sobel operator

A derivative of Sobel operator is to calculate the image by convolution operation, because the edge of image gray change rate is large, it can use the Sobel operator for edge detection. Nuclear Sobel operator is defined as:

The Y axis:

The X axis:

The Sobel function: Sobel(InputArray src, OutputArray dst, int ddepth,

int dx, int dy, int ksize=3,

doublescale=1, double delta=0,

int borderType=BORDER_DEFAULT );

Where ddepth is the image type, (DX, Dy) = (1,0) x directional derivative, (DX, Dy) = (0,1) y directional derivative, scale and delta is the role of the image can be scaled, and then save the former formula is: DST =dst * scale + delta.

Code:

int main() { Mat image = imread("F:\\lena.png", 0); if(!image.data) { cout <<"Fail to load image" <<endl; return 0; } Mat sobel_x, sobel_y; //Sobel(image, sobel_x, CV_8U, 1, 0, 3, 0.4, 128); //Sobel(image, sobel_y, CV_8U, 0, 1, 3, 0.4, 128); Sobel(image, sobel_x, CV_16S, 1, 0); // Because the back to add so using 16 bit signed integers to represent another derivative must contain negative numbers Sobel(image, sobel_y, CV_16S, 0, 1); Mat sobel; sobel = abs(sobel_x) + abs(sobel_y); double sobmin, sobmax; minMaxLoc(sobel, &sobmin, &sobmax); Mat sobelImage; sobel.convertTo(sobelImage, CV_8U, -255.0/sobmax, 255); // Equivalent to saturate_cast(a*sobel + b) Mat sobelThresholded; int thre = 200; threshold(sobelImage, sobelThresholded, thre, 255, THRESH_BINARY); namedWindow("sobelImage", 0); imshow("sobelImage", sobelImage); namedWindow("sobelThresholded", 0); imshow("sobelThresholded", sobelThresholded); waitKey(0); return 0; }

Result:

Of course, in addition to the Sobel operator, and other operators, such as Scharr operator, it is more accurate, faster. It's core:

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

Or:

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

Four: Laplasse transform

High pass filter image derivative based on Laplasse changes, the calculation of two order derivative to measure the bending of the image

Code:

int main() { Mat image = imread("F:\\lena.png", 0); if(!image.data) { cout <<"Fail to load image" <<endl; return 0; } Mat laplacian; Laplacian(image, laplacian, CV_16S, 3); // The two order derivative must contain negative numbers.. High pass filter based on image derivatives, two order derivative calculation in order to measure the degree of curvature image laplacian = abs(laplacian); Mat laplacianImage; laplacian.convertTo(laplacianImage, CV_8U); namedWindow("laplacianImage"); imshow("laplacianImage", laplacianImage); waitKey(0); return 0; }

Result:

Author: small village source: welcome to reprint or share, but please be sure to declare the source. Sina (micro-blog: small village Zack, welcomed the exchange of！)

**Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**.

**Free Download**

Posted by Daphne at November 02, 2014 - 3:25 AM