OpenCV 2.4.3 C++ smoothing analysis

  • 2020-04-01 21:31:37
  • OfStack

The principle of

Smoothing, also known as blur, is a simple and frequently used image processing method.

Smoothing requires a filter. The most commonly used filter is a linear filter, the output of linear filtering processing pixel values (for example: < img border = 0 class = math Alt = g (I, j) SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130969.png" >) is input pixel values (such as: < img border = 0 class = math Alt = f (I + k, j + l) SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130970.png" >) of a weighted average:

< img Alt = border = 0 "g (I, j) = \ sum_ {k, l} f (I + k, j + l) h (k, l)" SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130971.png" >

< img border = 0 class = math Alt = h (k, l) SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130972.png" > called nuclear, it is just a weighted coefficient.

The mean smooth

Here is an average smoothing using the blur function:


#include "opencv2/core/core.hpp" 

#include "opencv2/highgui/highgui.hpp" 

#include "opencv2/imgproc/imgproc.hpp" 

#include <stdio.h> 

using namespace cv; 

int main( int argc, char** argv ){ 
Mat image; 
image = imread( argv[1]); 

if( argc != 2 || !image.data ){ 
printf(" No picture n"); 
return -1; 
} 

namedWindow( " smoothing - The input " ); 
namedWindow( " smoothing - The output " ); 

imshow( " smoothing - The input ", image ); 

Mat out; 

blur( image, out, Size(3, 3)); 

imshow( " smoothing - The output ", out ); 

waitKey( 0 ); 
}

Blur function API data:

A normalized block filter is used to blur the image.

C + + : Void the blur (InputArray The SRC , OutputArray DST The Size, ksize That Point The anchor = Point (1, 1), int borderType = BORDER_DEFAULT) parameters The SRC The output image can be any number of channels. The function handles channels independently, but the depth can only be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F. DST The output image is the same size and depth as the input image. ksize The size of the kernel is blurred. The anchor The default value is (-1,-1), which means that the anchor is in the center of the kernel. borderType The stream is used to determine the pattern of the image boundary.

This function smooths the image using the following kernel:

\texttt{K} = \frac{1}{\texttt{ksi.width * ksi.height}} \begin{bmatrix {\ end bmatrix}

Calling blur(SRC, DST, ksize, anchor, borderType) is equivalent to calling boxFilter(SRC, DST, src.type(), anchor, true, borderType).

Blur USES a normalized block filter and the output pixel value is the average of the pixel values in the core window (all pixels have equal weighting coefficients).

The gaussian smoothing

The following code USES GaussianBlur for smoothing:


#include "opencv2/core/core.hpp" 

#include "opencv2/highgui/highgui.hpp" 

#include "opencv2/imgproc/imgproc.hpp" 

#include <stdio.h> 

using namespace std; 
using namespace cv;int main( int argc, char** argv ){ 
Mat image; 
image = imread( argv[1]); 

if( argc != 2 || !image.data ){ 
printf(" No picture n"); 
return -1; 
} 

namedWindow( " smoothing - The input " ); 
namedWindow( " smoothing - The output " ); 

imshow( " smoothing - The input ", image ); 

Mat out; 

GaussianBlur( image, out, Size( 3, 3 ), 0, 0 ); 

imshow( " smoothing - The output ", out ); 

waitKey( 0 ); 
}

GaussianBlur function API

Gaussian filter is used for fuzzy operation

C + + :   Void  GaussianBlur (InputArray  The SRC OutputArray  DST Size  ksize Double  sigmaX Double  sigmaY = 0, int  borderType = BORDER_DEFAULT) parameters The SRC   The number of channels can be any number of channels. The function handles channels independently, but the depth can only be CV_8U,& PI. CV_16U,   CV_16S,   CV_32F   Or  CV_64F. DST   �   The output picture is the same size and depth as the input picture. ksize   Size of the wok-gaussian kernel. Ksi.width and ksi.height are allowed to be different but they must be positive odd. Or is equal to 0, depending on the probability of the parameter sigma. sigmaX   The standard deviation of the gaussian kernel in the X direction. sigmaY   The standard deviation of the gaussian kernel in the Y direction. If sigmaY is 0, it will be the same as sigmaX, if they are both 0, they will be calculated by ksir.width and ksize-height. borderType   �   Used to determine the pattern of image boundaries.

The most useful filter (though not the fastest). Gaussian filtering is the convolution of each pixel of the input array with the gaussian kernel and the convolution with the sum as the output pixel value.

The < img border = 0 style = "FLOAT: Right "alt=http://www.cnblogs.com/http://www.cnblogs.com/_images/Smoothing_Tutorial_theory_gaussian_0.jpg SRC =" / / files.jb51.net/file_images/article/201211/2012112116130974.jpg ">

With respect to the one-dimensional gaussian, we can see that it's a smaller function on the middle side.

So the weights of a gaussian filter are big in the middle and small around.

The two-dimensional gaussian function is:

< img Alt = "border = 0 G_ {0} (x, Y) = A e ^ {\ dfrac {- (x - \ mu_ {x}) ^ {2}} {\ sigma 2 ^ {2} _ {x}} + \ dfrac {- (y - \ mu_ {y}) ^ {2}} {\ sigma 2 ^ {2} _ {y}}} "SRC =" / / files.jb51.net/file_images/article/201211/2012112116130975.png ">  

Among them   The < img border = 0 class = math Alt = \ mu SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130976.png" >   As the mean (peak corresponding location), < img border = 0 class = math Alt = \ sigma SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130977.png" >   On behalf of the standard deviation (variable < img border = 0 class = math Alt = x SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130978.png" >   And variable < img border = 0 class = math Alt = y SRC = "/ / files.jb51.net/file_images/article/201211/2012112116130979.png" >   Each has one mean and one standard deviation.

The median smoothing

Use medianBlur to perform median smoothing:


#include "opencv2/core/core.hpp" 

#include "opencv2/highgui/highgui.hpp" 

#include "opencv2/imgproc/imgproc.hpp" 

#include <stdio.h> 

using namespace std; 
using namespace cv;int main( int argc, char** argv ){ 
Mat image; 
image = imread( argv[1]); 

if( argc != 2 || !image.data ){ 
printf(" No picture n"); 
return -1; 
} 

namedWindow( " smoothing - The input " ); 
namedWindow( " smoothing - The output " ); 

imshow( " smoothing - The input ", image ); 

Mat out; 
medianBlur( image, out, 3); 

imshow( " smoothing - The output ", out ); 

waitKey( 0 ); 
}

API information of medianBlur function:

The median filter is used for the fuzzy operation

C + + :   Void  MedianBlur (InputArray  The SRC OutputArray  DST Int  ksize The Parameters: The SRC   The content supports 1, 3, and 4 channels of image input. When the ksize is 3 or 5, the image depth can only be CV_8U, CV_16U, or   CV_32F, only depth of CV_8U is supported for other large aperture sizes. DST   �   The output picture is the same size and depth as the input picture. ksize   The linear diameter can only be an odd number greater than 1, such as 3, 5, 7...

Median filtering takes each pixel of an image and USES it as a pixel of the neighborhood (a square area centered on the current pixel) The median Instead.  

Bilateral smooth

Use the bilateralFilter to perform bilateral smoothing:


#include "opencv2/core/core.hpp" 

#include "opencv2/highgui/highgui.hpp" 

#include "opencv2/imgproc/imgproc.hpp" 

#include <stdio.h> 

using namespace std; 
using namespace cv;int main( int argc, char** argv ){ 
Mat image; 
image = imread( argv[1]); 

if( argc != 2 || !image.data ){ 
printf(" No picture n"); 
return -1; 
} 

namedWindow( " smoothing - The input " ); 
namedWindow( " smoothing - The output " ); 

imshow( " smoothing - The input ", image ); 

Mat out; 
bilateralFilter ( image, out, 3, 3*2, 3/2 ); 

imshow( " smoothing - The output ", out ); 

waitKey( 0 ); 
}

API information for bilateralFilter:

Apply a bilateral filter to an image.

C + + :   Void  BilateralFilter (InputArray  The SRC, OutputArray  DST, int  D, double  SigmaColor double  SigmaSpace, intborderType = BORDER_DEFAULT  ) The Parameters: src  The source must be an 8-bit or floating point, 1 or 3-channel image. Dst  �   The output picture is the same size and depth as the input picture. D  The neighborhood diameter of each pixel used in the filter of the wok is determined by the sigmaSpace if it is a non-integer. SigmaColor  The standard variance of the color space of the pool. The larger the value, the more distant the color will be mixed into the neighborhood, so that the larger color segment will get the same color. SigmaSpace  The annotation variance of the copyspace. The larger the number, the further away the pixels will interact, so that the larger area is similar enough to get the same color. When d> 0, d specifies the neighborhood size and is independent of the sigmaSpace. Otherwise, d is proportional to sigma pace.

Principle for reference :

http://www.dai.ed.ac.uk/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html

The filters we know so far are all about smoothing the image, and the problem is that sometimes they not only reduce the noise, they also wear off the edges. To avoid this (at least to some extent), we can use bilateral filtering.  

Similar to a gaussian filter, a bilateral filter assigns a weighting factor to each neighborhood pixel. These weighting coefficients consist of two parts. The first part is weighted in the same way as the gaussian filter, and the second part is weighted according to the gray difference between the neighborhood pixel and the current pixel.


Related articles: