Image Smoothing (normalized block filtering, Gaussian filtering, median filtering, bilateral filtering)

Source: Internet
Author: User
Tags types of filters
Smooth image processing Target

This tutorial teaches you how to use various linear filters to smooth the image, the related OPENCV function is as follows: Blur Gaussianblur Medianblur bilateralfilter principle

Note

The following principles derive from Richard Szeliski's writings computer vision:algorithms and applications and learning OpenCV

Smoothing, also known as Blur, is a simple and highly-used image processing method.

There are many uses for smoothing, but in this tutorial we only focus on its function of reducing noise (other uses will come into contact in a later tutorial).

A filter is required for smooth processing. The most commonly used filter is a linear filter, and the output pixel value of the linear filter processing (i.e.) is the weighted sum of the input pixel values (i.e.):

Called a nucleus, it is just a weighting factor.

Think of the filter as a window with a weighted coefficient, and when you use this filter to smooth the image, slide the window over the image.

There are many types of filters, and only the most commonly used ones are mentioned: normalized block filter (normalized Box filter)

The simplest filter, the output pixel value is the mean value of the pixel value in the kernel window (all pixel weighted coefficients are equal)

The nucleus is as follows:

Gaussian filter (Gaussian filter)

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

Remember what the 1 Gaussian function looks like?

Assuming that the image is 1-dimensional, then it is not difficult to see that the weighted coefficients of the median pixels are the largest, and the weighted coefficients of the surrounding pixels gradually decrease as they move away from the median pixels.

Note

The 2 Gaussian function can be expressed as:

The mean value (peak corresponding position), which represents the standard deviation (variable and variable have one mean and one standard deviation) median filter (Median filter)

Median filtering replaces each pixel of an image with the median of the pixels in the neighborhood (the square area centered on the current pixel). Bilateral filtering (bilateral filter) The filters we now know are all designed to smooth the image, and the problem is that sometimes these filters not only weaken the noise, but also grind the edges away with them. To avoid such a situation (at least to a certain extent), we can use bilateral filtering. Similar to the Gaussian filter, the 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 weight of the second part depends on the gray difference between the neighborhood pixel and the current pixel. Detailed explanation can view the link source code

What does this program do? Load an image using 4 different filters (see Principle section) and display smooth images

Code at a glance:

#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace std;

using namespace CV;
global variable int delay_caption = 1500;
int delay_blur = 100;

int max_kernel_length = 31; Mat src;
Mat DST;

Char window_name[] = "Filter Demo 1";
function declaration int Display_caption (char* caption);

int display_dst (int delay);

   /** * Main function */int main (int argc, char** argv) {Namedwindow (window_name, cv_window_autosize); Load Original image src = Imread ("..

   /images/lena.jpg ", 1);

   if (Display_caption ("Original Image")! = 0) {return 0;}
   DST = Src.clone ();

   if (DISPLAY_DST (delay_caption)! = 0) {return 0;}

   Use mean smoothing if (display_caption ("Homogeneous Blur")! = 0) {return 0;}
         for (int i = 1; i < max_kernel_length; i = i + 2) {blur (src, DST, Size (i, I), point ( -1,-1));

    if (DISPLAY_DST (delay_blur)! = 0) {return 0;}}

    Use Gaussian smoothing if (display_caption ("Gaussian Blur")! = 0) {return 0;} For (int i = 1; i < max_kernel_length; i = i + 2)
          {Gaussianblur (src, DST, Size (i, I), 0, 0);

     if (DISPLAY_DST (delay_blur)! = 0) {return 0;}}

     Use the median smoothing if (display_caption ("Median Blur")! = 0) {return 0;}
           for (int i = 1; i < max_kernel_length; i = i + 2) {Medianblur (src, DST, i);

     if (DISPLAY_DST (delay_blur)! = 0) {return 0;}}

     Use a bilateral smoothing if (display_caption ("Bilateral Blur")! = 0) {return 0;}
           for (int i = 1; i < max_kernel_length; i = i + 2) {bilateralfilter (src, DST, I, i*2, I/2);

     if (DISPLAY_DST (delay_blur)! = 0) {return 0;}}

     Wait for user input display_caption ("end:press a key!");
     Waitkey (0);
 return 0; } int Display_caption (char* caption) {DST = Mat::zero

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.