Blurring/smoothening filters

Blurring an image is one of the most important image filtering tasks, and there are many algorithms that can perform it, each one with their own pros and cons, which we'll be talking about in this section.

Let's start with the simplest filter used for smoothening images, which is called the median filter, and it can be done by using the medianBlur function, as seen in the following example:

int ksize = 5; // must be odd 
medianBlur(image, result, ksize); 

This filter simply finds the median value of the neighboring pixels of each pixel in an image. ksize, or the kernel size parameter, decides how big the kernel used for blurring is, or, in other words, how far the neighboring pixels will be considered in the blurring algorithm. The following image depicts the result of increasing the kernel size from 1 to 7:

Note that the kernel size must be an odd value, and a kernel size of 1 will produce the exact same image as the input. So, in the previous images, you can see the increase in blurring level from the original image (the leftmost) until the kernel size of 7.

Note that you can use extremely high kernel sizes if you need, but it is rarely necessary and would usually be needed in case you need to de-noise an extremely noisy image. Here's an example image depicting the result of a kernel size of 21:

Another method of blurring an image is by using the boxFilter function. Let's see how it's done with an example code and then break it down further to better understand its behavior:

int ddepth = -1; 
Size ksize(7,7); 
Point anchor(-1, -1); 
bool normalize = true; 
BorderTypes borderType = BORDER_DEFAULT; 
 
boxFilter(image, 
          result, 
          ddepth, 
          ksize, 
          anchor, 
          normalize, 
          borderType); 

Box filtering is referred to as a blurring method in which a matrix of ones with the given ksize and anchor point is used for blurring an image. The major difference here, when compared with the medianBlur function, is that you can actually define the anchor point to be anything other than the center point in any neighborhood. You can also define the border type used in this function, whereas in the medianBlur function, BORDER_REPLICATE is used internally and cannot be changed. For more information about border types, you might want to refer to Chapter 3, Array and Matrix Operations. Finally, the normalize parameter allows us to normalize the result to the displayable result.

The ddepth parameter can be used to change the depth of the result. However, you can use -1 to make sure the result has the same depth as the source. Similarly, anchor can be supplied with -1 values to make sure the default anchor point is used.

The following image depicts the result of the previous example code. The image on the right is the box-filtered result of the image on the left-hand side:

We can perform the exact same task, in other words, the normalized box filter, by using the blur function, as seen in the following example code:

Size ksize(7,7); 
Point anchor(-1, -1); 
BorderTypes borderType = BORDER_DEFAULT; 
 
blur(image, 
     result, 
     ksize, 
     anchor, 
     borderType); 

The result of this sample code is exactly the same as calling boxFilter, as we saw previously. The obvious difference here is that this function does not allow us to change the depth of the result and applies normalization by default.

In addition to the standard box filter, you can also apply a square box filter with the sqrBoxFilter function. In this method, instead of calculating the sum of the neighboring pixels, the sum of their square values is calculated. Here is an example, which is incredibly similar to calling the boxFilter function:

int ddepth = -1; 
Size ksize(7,7); 
Point anchor(-1, -1); 
bool normalize = true; 
BorderTypes borderType = BORDER_DEFAULT; 
 
sqrBoxFilter(image, 
             result, 
             ddepth, 
             ksize, 
             anchor, 
             normalize, 
             borderType); 
The unnormalized versions of the boxFilter and sqrBoxFilter functions can also be used to find statistical information about the neighboring areas of all pixels in an image, and their use case is not limited to just blurring an image.

One of the most popular blurring methods in computer vision is the Gaussian blur algorithm, and it can be performed in OpenCV by using the GaussianBlur function. This function, similar to the previous blur functions that we learned about, requires a kernel size, along with standard deviation values in the X and Y direction, called sigmaX and sigmaY, respectively. Here's an example of how this function is used:

Size ksize(7,7); 
double sigmaX = 1.25; 
double sigmaY = 0.0; 
BorderTypes borderType = BORDER_DEFAULT; 
 
GaussianBlur(image, 
             result, 
             ksize, 
             sigmaX, 
             sigmaY, 
             borderType); 

Note that a value of zero for sigmaY means the value of sigmaX will also be used in the Y direction. For more information about Gaussian blur, you can read about the Gaussian function in general and the GaussianBlur function in the OpenCV documentation pages.

The last smoothening filter that we'll learn about in this section is called the bilateral filter, and it can be achieved by using the bilateralFilter function. Bilateral filtering is a powerful method of de-noising and smoothening an image, while preserving edges. This function is also much slower and more CPU-intensive in comparison with the blur algorithms that we saw previously. Let's see how bilateralFilter is used with an example and then break down the required parameters:

int d = 9; 
double sigmaColor = 250.0; 
double sigmaSpace = 200.0; 
BorderTypes borderType = BORDER_DEFAULT; 
 
bilateralFilter(image, 
                result, 
                d, 
                sigmaColor, 
                sigmaSpace, 
                borderType); 

d, or the filter size, is the diameter of the pixel neighborhood that will participate in the filter. The sigmaColor and sigmaSpace values are both used to define the effect of the color and coordinate the pixels nearer or farther from the pixel whose filtered value is being calculated. Here's a screenshot that demonstrates the effect of the bilateralFilter function when executed on our example image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.211.106