OpenCV image recognition from zero to proficient-----linear filtering and nonlinear filtering

Source: Internet
Author: User

One, introduction and convolution of noise

Second, the interpretation of each filter function, definition and source code

Three, integrated all the filter, plus slider to control the core size to blur

iv. Auxiliary expression of Matlab   

First, the introduction of noise

image noise is the random signal interference of the image when it is ingested or transmitted, and it is the various factors that hinder people's acceptance of the information in the image. Many times the image noise is regarded as a multidimensional stochastic process , so the method of describing the noise can borrow the description of stochastic process, namely, its probability distribution function and probability density distribution function. What we often see is the salt&pepper noise, here the noise has a theoretical introduction http://blog.csdn.net/qq_20823641/article/details/51513567, you can learn.

Second, the interpretation of each filter function, definition and source code

The noise is divided into linear and non-linear, the official Boxblur,blur,gaussianblur,medianblur,bilateralblur, wherein the box filter and mean filter have the same difference, prepared to say mean filter is the Special box filter, As can be seen from the function Boxblur and Blur, the following figure is also an explanation


Filter: It can be said that the filter is a window containing a weighted coefficient, when using this filter to smooth image processing, the window is placed on the image, through the window to see the image, sometimes called the kernel function kernel, I believe that often see this word.

When I see this window, I suggest you look at the concept of convolution, http://baike.baidu.com/link?url= Lqxrsr-c41ioynqyyowxe1ffsufcuptxfhbe2ajexol9bdhsvoarppd7f4r8t8c5dar8ggggqvxrgmmhr-pfjq

<span style= "FONT-SIZE:18PX;" >c++: void Boxfilter (Inputarray src,outputarray DST, int ddepth, Size ksize, point Anchor=point ( -1,-1), boolnormalize= True, int bordertype=border_default)  </span>

  • The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able. This function is independent of the channel and can handle images of any number of channels, but it should be noted that the depth of the image to be processed should be one of cv_8u, cv_16u, Cv_16s, cv_32f, and cv_64f.
  • The second parameter, the Outputarray type of DST, which is the target image, needs to have the same size and type as the source picture.
  • The third argument, the ddepth of the int type, the depth of the output image, and 1 represents the use of the original depth, i.e. src.depth ().
  • The fourth parameter, size type (which is explained later in the size type), is the ksize of the kernel. Typically this is written in size (w,h) to represent the kernel (where w is the pixel width and h is the pixel height). Size (3,3) represents the size of the 3x3 kernel, and size (5,5) represents the size of the 5x5 kernel
  • The fifth parameter, the point type of anchor, represents the anchor point (that is, the one that is smoothed), and note that he has the default value ( -1,-1). If the point coordinates are negative, then the center of the kernel is the anchor point, so the default value point ( -1,-1) indicates that the anchor is at the center of the nucleus.
  • The sixth parameter, normalize of type bool, is the default value of True, an identifier that indicates whether the kernel is normalized by its region (normalized).
  • The seventh parameter, the bordertype of type int, is used to infer some boundary pattern for the outer pixels of the image. There is a default value of Border_default, we generally do not care about it
See above the box filter, do not think too much, because we generally use the mean filter, box filter is an excessive, while looking at the mean filter below, but also see it is called Bofilter
<span style= "FONT-SIZE:18PX;" >void Cv::blur (inputarray src, outputarray DST,             Size Ksize, point anchor, int bordertype)  {      Boxfilter (s RC, DST,-1, Ksize, anchor, True, Bordertype);  }  </span>
  • The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able. This function is independent of the channel and can handle images of any number of channels, but it should be noted that the depth of the image to be processed should be one of cv_8u, cv_16u, Cv_16s, cv_32f, and cv_64f.
  • The second parameter, the Outputarray type of DST, which is the target image, needs to have the same size and type as the source picture. For example, you can use the Mat::clone, the source image as a template, to initialize the target image such as false replacement.
  • The third parameter, the size type (which is explained later in the size type), is the ksize of the kernel. Typically this is written in size (w,h) to represent the kernel (where w is the pixel width and h is the pixel height). Size (3,3) represents the size of the 3x3 kernel, and size (5,5) represents the size of the 5x5 kernel
  • The fourth parameter, the point type of anchor, represents the anchor point (that is, the one that is smoothed), and note that he has the default value ( -1,-1). If the point coordinates are negative, then the center of the kernel is the anchor point, so the default value point ( -1,-1) indicates that the anchor is at the center of the nucleus.
  • The fifth parameter, the bordertype of type int, is used to infer some boundary pattern for the outer pixels of the image. There is a default value of Border_default, we generally do not care about it.

<span style= "FONT-SIZE:18PX;" &GT;CV::P tr<cv::filterengine> cv::createboxfilter (int srctype, int dsttype, Size ksize, point      Anchor, BOOL normalize, int bordertype) {int sdepth = cv_mat_depth (srctype);      int cn = CV_MAT_CN (srctype), sumtype = cv_64f;          if (sdepth <= cv_32s && (!normalize | | Ksize.width*ksize.height <= (sdepth = = cv_8u? (1<<23): sdepth = = cv_16u?      (1 <<): (1 <<))) Sumtype = cv_32s;      Sumtype = Cv_maketype (Sumtype, CN);      ptr<baserowfilter> rowFilter = Getrowsumfilter (Srctype, Sumtype, Ksize.width, anchor.x);  ptr<basecolumnfilter> columnfilter = Getcolumnsumfilter (Sumtype, Dsttype, Ksize.height, Anchor.y, normalize ?        1./(ksize.width*ksize.height): 1); Return ptr<filterengine> (New Filterengine (ptr<basefilter> (0), RowFilter, Columnfilter, Srctype, DS  Ttype, Sumtype, Bordertype)); }</span&Gt 
The Gaussian function is mainly to look at the following function, Gaussian function, I just want to say that it may be the image processing is inseparable from the function, because he is too easy to use, in the future frequency domain learning will be so useful
<span style= "FONT-SIZE:18PX;" >c++: void Gaussianblur (Inputarray src,outputarray DST, Size ksize, double Sigmax, double sigmay=0, Intbordertype=bord Er_default)  </span>

  • The first parameter, the Inputarray type SRC, the input image, namely the source image, fills the Mat class the object to be able. It can be a single picture of any number of channels, but it should be noted that the image depth should be one of cv_8u,cv_16u, Cv_16s, cv_32f and cv_64f.
  • The second parameter, the Outputarray type of DST, which is the target image, needs to have the same size and type as the source picture. For example, you can use the Mat::clone, the source image as a template, to initialize the target image such as false replacement.
  • The third parameter, size of type ksize Gaussian kernel. where Ksize.width and ksize.height can be different, but they both have to be positive and odd. Alternatively, they can be zero, and they are all calculated by Sigma.
  • The fourth parameter, a double of type Sigmax, represents the standard deviation of the Gaussian kernel function in the x direction.
  • The fifth parameter, a double of type Sigmay, represents the standard deviation of the Gaussian kernel function in the y direction. If the Sigmay is zero, it is set to Sigmax, if both Sigmax and Sigmay are 0, then it is calculated by Ksize.width and ksize.height.
  • For the sake of correctness of the result, it is best to assign the third parameter size, the fourth parameter Sigmax and the fifth parameter Sigmay all to.
  • a sixth parameter, An bordertype of type int that infers some boundary pattern for the outer pixels of the image. There is a default value of Border_default, we generally do not care about it
Gauss Source code is still very long, because there are a lot of things behind, so here do not show, want to see can go here to http://blog.csdn.net/xiaowei_cqu/article/details/7785365
<span style= "FONT-SIZE:18PX;" >c++: void Medianblur (Inputarray src,outputarray DST, int ksize)  </span>

    • The first parameter, the Inputarray type of SRC, the input parameter of the function, fills 1, 3 or 4 channels of the mat type of image, when the ksize is 3 or 5, the image depth needs to be cv_8u,cv_16u, or cv_32f one of them, and for larger aperture size picture, It can only be cv_8u.
    • The second parameter, the Outputarray type of DST, that is, the target image, the output parameter of the function, needs to have the same size and type as the source picture. We can use the Mat::clone, with the source image as a template, to initialize the target image such as false replacement.
    • The third parameter, the ksize of type int, the linear dimension of aperture (aperture linear size), note that this parameter must be an odd number greater than 1, for example: 3,5,7,9 ...
source code can be seen here, can also go to \opencv\sources\modules\imgproc\src\ Smooth.cpp's 1653th line starts, but looked also useless, the essence did not write in the source code inside, also must go further, has the interest can see http://blog.csdn.net/poem_qianmo/article/details/23184547
Bilateral filtering I'm very fond of, because we all know smoothing time is very blur, at the same time lost a lot of detail, especially the edge, then I will extract the edge contour when the feeling is very bad, but the bilateral filtering to meet the requirements, Here is an introduction to his theory about I http://blog.csdn.net/qq_20823641/article/details/51533420
<span style= "FONT-SIZE:18PX;" >c++: void Bilateralfilter (Inputarray src, outputarraydst, int d, double sigmacolor, double sigmaspace, int bordertype= Border_default)  </span>

  • The first parameter, the Inputarray type of SRC, the input image, which is the source image, requires a 8-bit or floating-point single-channel, three-channel image.
  • The second parameter, the Outputarray type of DST, which is the target image, needs to have the same size and type as the source picture.
  • The third parameter, type d of int, represents the diameter of each pixel neighborhood during the filtering process. If this value is set to a non-positive number, then OPENCV will calculate it from the fifth parameter sigmaspace.
  • The fourth parameter, a double of type Sigmacolor, is the sigma value of the color space filter. The larger the value of this parameter, the larger the color in the neighborhood of the pixel is blended together to produce a large half-equal color region.
  • The fifth parameter, the value of the Sigma of the filter in the Sigmaspace coordinate space of a double type, and the label variance of the coordinate space. The larger the number, the farther the pixels will affect each other, so that the larger regions have enough similar colors to get the same color. When d>0,d specifies the neighborhood size and is Sigmaspace Independent. Otherwise, d is proportional to sigmaspace.
  • The sixth parameter, the bordertype of type int, is used to infer some boundary pattern for the outer pixels of the image. Note that it has a default value of Border_default.
three, integrated all the filter, plus slider to control the core size to blur
/span>
<span style= "FONT-SIZE:18PX;" > #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/ Imgproc/imgproc.hpp> #include <iostream>using namespace std;using namespace CV;  Mat G_srcimage,g_dstimage1,g_dstimage2,g_dstimage3,g_dstimage4,g_dstimage5;int g_nboxfiltervalue=6;  Box filter core value int g_nmeanblurvalue=10;  Mean filter core value int g_ngaussianblurvalue=6;  Gaussian filter core value int g_nmedianblurvalue=10;  Median filter parameter value int g_nbilateralfiltervalue=10; Bilateral filtering parameter value static void On_boxfilter (int, void *);//box filter static void On_meanblur (int, void *);//mean block filter static void On_ Gaussianblur (int, void *);//Gaussian filter static void On_medianblur (int, void *);//median filter static void on_bilateralfilter (int, void *);//Bilateral filter int main () {g_srcimage = Imread ("lena.jpg", 1); g_dstimage1 = G_srcimage.clone (); g_dstimage2 = g_srcimage.cl One (); g_dstimage3 = G_srcimage.clone (); g_dstimage4 = G_srcimage.clone (); g_dstimage5 = G_srcimage.clone (); NamedWindow ("<0> Original Window", 1); Imshow ("" <0> original Window "", g_srcimage); Namedwindow (" "<1> Box Filter", 1); Createtrackbar ("Kernel Value:", "" "<1> Box Filter" ", &g_nboxfiltervalue, 50,on_boxfilter); On_meanblur (g_nboxfiltervalue,0); Imshow ("<1> box filter", G_dstimage1); Namedwindow ("" <2 > Mean Filter "", 1); Createtrackbar ("Kernel Value:", "" "<2> Mean Filter" ", &g_nmeanblurvalue, 50,on_meanblur); On_meanblur (g_ nmeanblurvalue,0);</span>

<span style= "FONT-SIZE:18PX;" >namedwindow ("<3> Gaussian filter", 1); Createtrackbar ("Kernel Value:", "" "<3> Gaussian Filter" ", &g_ngaussianblurvalue, 50,on_ Gaussianblur); On_gaussianblur (g_ngaussianblurvalue,0); Namedwindow ("<4> median filter", 1); Createtrackbar ("Parameter value:", " "<4> median filter" ", &g_nmedianblurvalue, 50,on_medianblur); On_medianblur (g_nmedianblurvalue,0); Namedwindow (" " <5> bilateral Filtering "", 1); Createtrackbar ("Parameter value:", "" "<5> Bilateral Filter" ", &g_nbilateralfiltervalue, 50,on_bilateralfilter) ; On_bilateralfilter (g_nbilateralfiltervalue,0); return 0;} static void On_boxfilter (int, void *) {boxfilter (G_srcimage, G_dstimage1, -1,size (g_nboxfiltervalue+1, G_nboxfilte       rvalue+1)); Imshow ("<1> box filter", G_dstimage1);} static void On_meanblur (int, void *) {blur (G_srcimage, G_dstimage2, Size (g_nmeanblurvalue+1, g_nmeanblurvalue+1), point ( -1,-1)); Imshow ("<2> mean Filter", g_dstimage2);} static void On_gaussianblur (int, void *) {Gaussianblur (G_srcimage, G_dstimage3, Size (G_ngaussianblurvalue*2+1, G_ngaussianblurvalue*2+1), 0, 0); Imshow ("<3> Gaussian filter", G_dstimage3);} static void On_medianblur (int, void *) {Medianblur (G_srcimage, G_dstimage4, g_nmedianblurvalue*2+1); Imshow ("" <4 > Median filter "", G_dstimage4);} static void On_bilateralfilter (int, void *) {bilateralfilter (G_srcimage, G_dstimage5, G_nbilateralfiltervalue, g_ Nbilateralfiltervalue*2, G_NBILATERALFILTERVALUE/2) imshow ("<5> Bilateral Filtering", g_dstimage5);} </span>


Four, Matlab auxiliary
<span style= "FONT-SIZE:18PX;" >h=imread (' d:\lena.jpg '); A=fspecial (' average ', 3); OUTPUT1 = IMFilter (H, A, ' conv ', ' replicate '); A=fspecial (' Gaussian ', 3); Output2 = IMFilter (H, A, ' conv ', ' replicate '); OUTPUT3 = Medfilt2 (H, [3, 3]);</span>

Clear All;close all;clc;img=imread (' lena.jpg '); Img=mat2gray (IMG); [M N]=size (IMG); Imshow (IMG); r=10;        Imgn=zeros (m+2*r+1,n+2*r+1); IMGN (r+1:m+r,r+1:n+r) =IMG;IMGN (1:r,r+1:n+r) =img (1:r,1:n);                IMGN (1:m+r,n+r+1:n+2*r+1) =IMGN (1:m+r,n:n+r);    IMGN (m+r+1:m+2*r+1,r+1:n+2*r+1) =IMGN (m:m+r,r+1:n+2*r+1);    IMGN (1:m+2*r+1,1:r) =IMGN (1:m+2*r+1,r+1:2*r);      sigma_d=2;sigma_r=0.1; [x, Y] = Meshgrid (-r:r,-r:r); W1=exp (-(x.^2+y.^2)/(2*sigma_d^2));     For I=r+1:m+r for    j=r+1:n+r                w2=exp (-(IMGN (i-r:i+r,j-r:j+r)-IMGN (i,j)). ^2/(2*sigma_r^2));         W=W1.*W2;         S=IMGN (i-r:i+r,j-r:j+r). *w;        IMGN (i,j) =sum (sum (s))/sum (SUM (w));    End Endfigure;imshow (Mat2gray (IMGN (r+1:m+r,r+1:n+r));

There is another method of function call can refer to here http://blog.csdn.net/abcjennifer/article/details/7616663

OpenCV image recognition from zero to proficient-----linear filtering and nonlinear 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.