OpenCV 2.4.3 C + + smoothing Process Analysis _c language

Source: Internet
Author: User

Principle

Smoothing is also called Fuzzy, which is a simple and high frequency image processing method.

A filter is needed to smooth the process. The most commonly used filter is the linear filter, the output pixel value of the linear filter processing (for example:) is theweighted average of the input pixel value (for example:):

    

Called the nucleus, it is just a weighted coefficient.

Mean-value Smoothing

The following is a mean smoothing using the Blur function:

Copy Code code as follows:

#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 ("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 data:

The fuzzy image operation is performed using a normalized block filter.

C + +: void Blur (Inputarray src, Outputarray DST, Size ksize, point Anchor=point ( -1,-1), int Bordertype=border_default ) Parameters src– Enter a picture so that you can make any number of channels that are handled independently of the channel, but the depth can only be cv_8u, cv_16u, cv_16s, cv_32fOr cv_64f. DST– The output picture is the same size and depth as the input picture. ksize– Fuzzy kernel size. Anchor– Anchor points, the default value is ( -1,-1), where the anchor is in the center of the kernel. Bordertype– The pattern used to determine image boundaries.

This function smoothes the image using the following kernel:

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 mean value of the pixel value in the kernel window (all pixel weighting coefficients are equal).

Gaussian smoothing

The following code uses Gaussianblur to achieve smoothing:

Copy Code code as follows:

#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 ("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 Data:

Fuzzy operation using Gaussian filter

C + +:  void Gaussianblur (Inputarray src, Outputarray DST, Size ksize, double Sigmax, double Sigmay=0, int Bordertype=border_default ) Parameters src– Enter a picture so that you can make any number of channels that are handled independently of the channel, but the depth can only be cv_8u, cv_16u, cv_16s, cv_32fOr cv_64f. DST– The output picture is the same size and depth as the input picture. ksize– The Gaussian kernel size. Ksize.widthAnd Ksize.heightPermission is not the same but they must be positive odd numbers. or equal to 0, by argument Sigma's flight decision. Sigmax– The standard deviation of the Gaussian core in the x direction. Sigmay– The standard deviation of the Gaussian core in the y direction. If Sigmay for 0, he will and Sigmax values are the sameIf they were all 0, then they were by Ksize.widthAnd Ksize.heightCalculated. Bordertype– The pattern used to determine image boundaries.

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

Referring to a Gaussian function, we can see that he is a small function on both sides of the middle.

So the weighted number of Gaussian filters is large in the middle and small in the surrounding.

The second Gaussian function is:

    

Where the mean value (the peak corresponds to the position), representing the standard deviation (variable and variable each have a mean value, each has a standard deviation).

Medium Value Smoothing

Use Medianblur to perform median smoothing:

Copy Code code as follows:

#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 ("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 Data:

Fuzzy operation using Median filter

C + +:  void Medianblur (Inputarray src, Outputarray DSTInt ksize ) Parameters: src-Supports 1, 3, 4 channel image input, when ksizeFor 3 or 5 o'clock, the depth of the picture can only be cv_8u,, cv_16uOr cv_32f, for other large aperture dimensionssupports only depth to cv_8u. DST– The output picture is the same size and depth as the input picture. ksize– The linear diameter size can only be an odd number greater than 1, for example: 3, 5, 7 ...

The median filter replaces each pixel of the image with the median of the pixel in the neighborhood (the square area centered on the current pixel).

Bilateral smoothing

Use Bilateralfilter to perform bilateral smoothing:

Copy Code code as follows:

#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 ("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);
}

Bilateralfilter API Information:

Apply a bilateral filter to a picture.

C + +:  void Bilateralfilter (Inputarray src, outputarray dst, int D, double sigmacolor, double sigmaspace, Intbordertype=border_default ) Parameters:The src– source must be a 8-bit or floating-point number, 1 or 3-channel picture. dst– output picture, and input picture the same size and depth. D-the neighborhood diameter of each pixel used in the filtering process, if this is a non integer, the value is SigmaspaceDecided. sigmacolor– the standard variance of the color space. The larger the number, the farther the color will be infiltrated into the neighborhood, so that the larger color segment gets the same color. sigmaspace– the callout variance of the coordinate space. The larger the number, the farther the pixel will affect each other, so that the larger area is sufficiently similar to the color to get the same color. When d>0,d Specifies the neighborhood size and is independent of sigmaspace. Otherwise, DProportional to Sigmaspace。

principle can refer to :

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

At present, we understand the filter is to smooth the image, the problem is sometimes these filters not only weaken the noise, even with the edge also to wear off. To avoid such a situation (at least to a certain extent), we can use bilateral filtering.

Similar to the Gaussian filter, the bilateral filter also assigns a weighting coefficient to each neighborhood pixel. These weighting coefficients consist of two parts, the first part is the same as Gaussian filter, and the weight of the second part depends on the gray difference between the neighboring pixel and the current pixel.

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.