OpenCV2 Study Notes (v): Fundamentals of Image filtering

Source: Internet
Author: User

A: Basic concepts

Filtering is a basic operation in digital image processing, which can be said everywhere in the field of signal processing. Image filtering, that is, in the case of preserving the image detail characteristics of the target image of the noise suppression, usually digital image processing is an indispensable operation, the quality of its processing effect will directly affect the subsequent operation and analysis of the effect. Simply put, the fundamental purpose of image filtering is to extract the characteristics of human interest in the image.

When we look at an image, there are two ways of dealing with it:
1. Observe the distribution of different grayscale (or color values) in the image, that is, the spatial distribution.
2. Observe the change in grayscale (or color) in the image, which is related to frequency problems.

Therefore, the image filtering is divided into the frequency domain and the spatial filter, in a nutshell, the spatial domain refers to the image using the gray value to describe an image, and the frequency field refers to the change of the gray value of the image to describe an image. The concept of low-pass filters and high-pass filters is generated in the frequency domain. The low-pass filter is designed to remove high-frequency components from the image, while the pass filter removes low-frequency components from the image.

Here we simply record the mean and Gaussian filters (linear filters), median filters (nonlinear filters), Sobel operators (directional filters) and Laplace transforms (second derivative) in the high-pass filter, where the Sobel operator and the Laplace transform can detect the edges of the image.

Two: low-pass filter
Eliminating the noise component in the image is called smoothing or low-pass filtering of the image. The energy of the signal or image is mostly concentrated in the low and middle frequencies of the amplitude spectrum, and in the higher frequency bands, the information of interest is often overwhelmed by noise. Therefore, a filter that reduces the amplitude of high-frequency components can reduce the effect of noise.
The purpose of image filtering is two: one is to extract the characteristics of the object as the feature pattern of image recognition, the other is to adapt to the requirements of image processing, eliminate the noise mixed in the image digitization.
Of course, in the design of low-pass filter, to take into account the filtering of the image caused by the loss of details and so on.

Smoothing filtering is a spatial domain filtering technique with low frequency enhancement. There are two kinds of purposes: one is image blur, the other is filtering image noise.
The smoothing filtering in spatial domain is usually done by simple averaging method, which is to find the average gray value or luminance value of neighboring pixel points. The size of the neighborhood is directly related to the smoothing effect, the greater the smoothing effect of the neighborhood, but the larger the neighborhood, the greater the loss of the edge information, so that the output of the image becomes blurred, so it is necessary to select the neighborhood size reasonably.
On the filter, an image analogy is: we can think of the filter as a window containing a weighted coefficient, when using this filter to smooth image processing, the window is placed above the image, through this window to see the image we get.
There are many kinds of filters, in OpenCV, we provide the following common image smoothing operation methods and functions:
1. Domain mean filter: blur function to replace each pixel of an image with the mean value of the pixels within the adjacent rectangle (mean filter)
2. Gaussian low-pass filter: Gaussianblur function
3. Box Filter: Boxblur function
4. Median Filter: Medianblur function
5. Bilateral filtering: Bilateralfilter function

The following is a simple code for mean filtering and Gaussian low-pass filtering (unmodified):

#include <iostream>#include <opencv2/core/core.hpp>#include <opencv2/imgproc/imgproc.hpp>#include <opencv2/highgui/highgui.hpp>int main () {// ReadInput imageCV:: MatImage=CV:: Imread(".. /boldt.jpg ",0);if(!image.data)return 0;// DisplayThe imageCV:: Namedwindow("Original Image");CV:: Imshow("Original Image", image);// BlurThe imageCV:: MatResultCV:: Gaussianblur(Image,result,CV:: Size(5,5),1.5);// DisplayThe blurred imageCV:: Namedwindow("Gaussian filtered Image");CV:: Imshow("Gaussian filtered Image", result);// GetThe Gaussian kernel (1.5)CV:: Matgauss=CV:: Getgaussiankernel(9,1.5,cv_32f);// DisplayKernel valuesCV::mat_<float>:: Const_iteratorit= Gauss.begin<float> ();CV::mat_<float>:: Const_iteratoritend= Gauss.End<float> ();std:: cout<<"["; for(; it!= itend; ++it) {std:: cout<< *it <<" "; }std:: cout<<"]"<<std:: Endl;// GetThe Gaussian kernel (0.5) gauss=CV:: Getgaussiankernel(9,0.5,cv_32f);// DisplayKernel values it= Gauss.begin<float> (); itend= Gauss.End<float> ();std:: cout<<"["; for(; it!= itend; ++it) {std:: cout<< *it <<" "; }std:: cout<<"]"<<std:: Endl;// GetThe Gaussian kernel (2.5) gauss=CV:: Getgaussiankernel(9,2.5,cv_32f);// DisplayKernel values it= Gauss.begin<float> (); itend= Gauss.End<float> ();std:: cout<<"["; for(; it!= itend; ++it) {std:: cout<< *it <<" "; }std:: cout<<"]"<<std:: Endl;// GetTheDerivKernel (2.5)CV:: MatKX, KY;CV:: Getderivkernels(Kx,ky,2,2,7,true);// DisplayKernel valuesCV::mat_<float>:: Const_iteratorkit= kx.begin<float> ();CV::mat_<float>:: Const_iteratorkitend= kx.End<float> ();std:: cout<<"["; for(; kit!= kitend; ++kit) {std:: cout<< *kit <<" "; }std:: cout<<"]"<<std:: Endl;// BlurThe image with a mean filterCV:: Blur(Image,result,CV:: Size(5,5));// DisplayThe blurred imageCV:: Namedwindow("Mean filtered Image");CV:: Imshow("Mean filtered Image", result);// ReadInput image with Salt&pepper noise image=CV:: Imread(".. /salted.bmp ",0);if(!image.data)return 0;// DisplayTheS&PImageCV:: Namedwindow("The Image");CV:: Imshow("The Image", image);// BlurThe image with a mean filterCV:: Blur(Image,result,CV:: Size(5,5));// DisplayThe blurred imageCV:: Namedwindow("Mean filtered Image");CV:: Imshow("Mean filtered Image", result);// applyingA median filterCV:: Medianblur(Image,result,5);// DisplayThe blurred imageCV:: Namedwindow("Median filtered Image");CV:: Imshow("Median filtered Image", result);// ReduceBy4The size of the image (the wrong-image=)CV:: Imread(".. /boldt.jpg ",0);CV:: MatReduced (image.rows/2, image.cols/2,cv_8u); for(int i=0; i<reduced.rows; i++) for(int j=0; j<reduced.cols; J + +) reduced.at<uchar> (i,j) = image.at<uchar> (i*2, j*2);// DisplayThe reduced imageCV:: Namedwindow("badly reduced Image");CV:: Imshow("badly reduced Image", reduced);CV:: Waitkey();return 0;}

To be Continued ...

OpenCV2 Study Notes (v): Fundamentals of 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.