Sunday, November 12, 2023

OpenCV and Image Processing - Blur Image

Blurring an image is a common image processing technique used to reduce noises, smooth out edges, and simplify the image. This post will introduce two types of image blurring techniques, Gaussian Blur and Median Blur. 

Gaussian Blur

Gaussian Blur is a widely used effect for image processing and is available in many image-editing software. Basically it reduces noises and hides details of the image, and makes the image smoothing. Figure 1 below shows what it looks like, the left one is the original image, and the right one is a Gaussian blurred image. By changing the Kernel size, the level of blurring will be changed.


Figure 1 Gaussian Blur

Let’s take a close look at how the Gaussian blur works, notice the small area highlighted by a white square pointed by a white arrow in Figure 1 above. Now zoom in this area and show the details in pixel level, as shown in Figure 2, this square is 9 by 9 pixels. Apply a Gaussian blur on this 9 × 9 area, we get the rightside image in Figure 2, the edge of the flower becomes blurred. The size of this area is called kernel size, in this example the area is a square of 9 × 9 pixels, then its kernel size is 9. It doesn’t have to be a square, it could be a rectangle, say 5 × 9 pixels. However, the kernel size must be odd numbers.

This small area is called filter, the Gaussian filter will be applied throughout the entire image starting from the top-left corner, moving from left towards right and from top towards down until the bottom-right corner, in another word the filter swept over the whole image, this is the image blurring process.

Figure 2

Now, let’s look at how the Gaussian function works, in other words how the filter is calculated and applied to the area.

Gaussian filter does not simply calculate the average value of the area, in this case the 9 × 9 area. Instead, it calculates a weighted average of the value for this area, the pixels near the center get more weights, and the pixels far away from the center get less weights. And the calculation is done on a channel-by-channel basis, which means it calculates the blue, green and red channels respectively.

Let’s dig a little deeper and see how the Gaussian function works, this is the formula in one dimension:


However, a pixel is determined by x and y coordinates in an image, so the two dimensional Gaussian formula should be used for image processing, it’s shown as Figure 3.

Figure 3

The two-dimensional Gaussian formula is:


Now imagine the Gaussian filter mentioned above, in our case an area of 9 x 9 pixels, is overlaying on the vertex of the above plot, the height is the weight for calculation. The pixels near the center are more important and then get more weights, the pixels near the edge are less important and then get less weights.

This is a basic idea of how Gaussian blur works in general, although the algorithm is not as simple as described above. Fortunately, OpenCV provides a function for this purpose, cv2.GaussianBlur(), and all the complexities are hidden behind the scenes. All we need to do is to call this function and specify the kernel size in width and height, which doesn’t have to be the same, but must be odd numbers.

In the next post we'll implement Gaussian blur function.


Share:

0 comments:

Post a Comment