OpenCV 2.4.3 C ++ Smooth Processing Analysis

Source: Internet
Author: User

Principle

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

A filter is required for smooth processing. The most common filter is a linear filter. The output pixel value of linear filter processing (for example, :) is the weighted average of input pixel values (for example:

    

It is called the core, and it is just a weighting coefficient.

Mean Smoothing

Below is a mean smoothing using the blur function:

Copy codeThe Code is as follows: # include "opencv2/core. hpp"

# Include "opencv2/highgui. hpp"

# Include "opencv2/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 image \ n ");
Return-1;
}

NamedWindow ("Smooth Processing-input ");
NamedWindow ("Smooth Processing-output ");

Imshow ("Smooth Processing-input", image );

Mat out;

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

Imshow ("Smooth Processing-output", out );

WaitKey (0 );
}

Blur function API materials:

Use the normalized block filter to perform fuzzy image operations.

C ++: VoidBlur(InputArraySrc, OutputArrayDst, SizeKsize, PointAnchor= Point (-1,-1), intBorderType= BORDER_DEFAULT)
Parameters

Src-Input image, which can be any number of channels. This function processes the channel independently, but the depth can only beCV_8U,CV_16U,CV_16S,CV_32FOrCV_64F.Dst-The size and depth of the output image are the same as those of the input image.Ksize-Blur the kernel size.Anchor-Anchor. The default value is(-1,-1)That is, the anchor is in the center of the kernel.BorderType-This mode is used to determine the image boundary.

This function uses the following kernel for smooth image processing:

CallBlur (src, dst, ksize, anchor, borderType)Equivalent to callingBoxFilter (src, dst, src. type (), anchor, true, borderType ).

Blur uses a normalized block filter, and the output pixel value is the average value of the pixel value in the kernel window (the weighting coefficients of all pixels are equal ).

Gaussian smoothing

The following code uses GaussianBlur for smoothing:

Copy codeThe Code is as follows: # include "opencv2/core. hpp"

# Include "opencv2/highgui. hpp"

# Include "opencv2/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 image \ n ");
Return-1;
}

NamedWindow ("Smooth Processing-input ");
NamedWindow ("Smooth Processing-output ");

Imshow ("Smooth Processing-input", image );

Mat out;

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

Imshow ("Smooth Processing-output", out );

WaitKey (0 );
}

GaussianBlur function API materials:

Perform fuzzy operations using Gaussian filter

C ++: VoidGaussianBlur(InputArraySrc, OutputArrayDst, SizeKsize, DoubleSigmaX, DoubleSigmaY= 0, intBorderType= BORDER_DEFAULT)
Parameters

Src-Input image, which can be any number of channels. This function processes the channel independently, but the depth can only beCV_8U,CV_16U,CV_16S,CV_32FOrCV_64F.Dst-The size and depth of the output image are the same as those of the input image.Ksize-Gaussian Kernel size.Ksize. widthAndKsize. heightDifferent values are allowed, but they must be positive and odd numbers. Or equal to 0, by the ParameterSigma's flight decision.SigmaX-Standard deviation of Gaussian Kernel in the X direction.SigmaY-Standard deviation of Gaussian Kernel in Y direction. IfSigmaY is 0.The value of sigmaX is the same.If they are both 0Ksize. widthAndKsize. heightCalculated.BorderType-This mode is used to determine the image boundary.

The most useful filter (though not the fastest ). Gaussian filter convolutionates every pixel in the input array with the Gaussian Kernel and uses it as the output pixel value.

By referring to the one-dimensional Gaussian function, we can see that it is a function with both sides of the middle.

Therefore, the weighted number of Gaussian filters is large in the middle and small in four weeks.

The second-dimension Gaussian function is:

    

The mean value (the location corresponding to the peak value) represents the standard deviation (each variable has an mean and each has a standard deviation ).

Median Smoothing

Use medianBlur to perform a median smoothing:

Copy codeThe Code is as follows: # include "opencv2/core. hpp"

# Include "opencv2/highgui. hpp"

# Include "opencv2/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 image \ n ");
Return-1;
}

NamedWindow ("Smooth Processing-input ");
NamedWindow ("Smooth Processing-output ");

Imshow ("Smooth Processing-input", image );

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

Imshow ("Smooth Processing-output", out );

WaitKey (0 );
}

MedianBlur function API materials:

Use a median filter for Fuzzy Operations

C ++: VoidMedianBlur(InputArraySrc, OutputArrayDst, IntKsize)
Parameters:

Src-Supports image input through channels 1, 3, and 4.KsizeWhen it is 3 or 5, the image depth can only beCV_8U,,CV_16U, OrCV_32F, for other large aperture sizesOnly the depth isCV_8U.Dst-The size and depth of the output image are the same as those of the input image.Ksize-Linear diameter, which can only be an odd number greater than 1, for example, 3, 5, 7...

The median filter uses the neighborhood (Square area centered on the current pixel) for each pixel of the image.Median.

Bilateral Smoothing

Use bilateralFilter to perform bilateral smoothing:

Copy codeThe Code is as follows: # include "opencv2/core. hpp"

# Include "opencv2/highgui. hpp"

# Include "opencv2/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 image \ n ");
Return-1;
}

NamedWindow ("Smooth Processing-input ");
NamedWindow ("Smooth Processing-output ");

Imshow ("Smooth Processing-input", image );

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

Imshow ("Smooth Processing-output", out );

WaitKey (0 );
}

API Information of bilateralFilter:

Apply a bilateral filter to an image.

C ++: VoidBilateralFilter(InputArray src, OutputArray dst, int d, double sigmaColor, double sigmaSpace, intborderType = BORDER_DEFAULT)
Parameters:

Src-the source must be an 8-bit or floating-point number, one or three-channel image. Dst-output image, the same size and depth as the input image. D-the diameter of the neighboring pixels used in the filtering process. If this is a non-integer, the value is determinedSigmaSpaceDecide. SigmaColor-standard variance of the color space. The larger the value, the longer the color will be mixed into the neighborhood, so that a larger color segment can obtain the same color. SigmaSpace-annotation variance of coordinate space. The larger the value, the longer the pixel will affect each other, so that the larger area is similar enough to obtain the same color. WhenD> 0. d specifies the neighborhood size and is irrelevant to sigmaSpace. Otherwise,DProportionalSigmaSpace.

For more information, see:

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

At present, we know that filters are used to smooth images. The problem is that these filters not only weaken noise, but also eliminate edges. To avoid this situation (at least to a certain extent), we can use bilateral filtering.

Similar to Gaussian filter, bilateral filters also assign a weighting factor to each neighboring pixel. These weighting coefficients consist of two parts. The first part of the weighting method is the same as Gaussian filtering, and the second part of the weight is determined by the gray difference between the neighboring pixel and the current pixel.

Related Article

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.