Image Blurring Example with OpenCV in Python

    Image blur refers to the effect of making an image less sharp and more difficult to distinguish the details. Although blurring is typically a negative effect in photographs, it is useful method in image processing tasks. Image blur is used in image processing for a variety of reasons, such as image smoothing, edge detection, obscuring sensitive information, and preprocessing steps. 

    Blurring an image involves the process of convolution with a designated kernel or filter. A kernel, represented by a small matrix, is employed on each pixel within the image. The output image's pixel values are determined by the multiplication of corresponding kernel values with the neighboring pixel values, followed by the summation of the outcomes.

     In this tutorial, you'll briefly learn how to blur image with OpenCV API in Python.   

    The tutorial covers;

  1. Gaussian blur
  2. Median blur
  3. Bilateral filter method
  4. Averaging method
  5. Source code listing

    We'll start by loading the required libraries.


import cv2
import matplotlib.pyplot as plt 
 
 
  

In this tutorial, we need target image file to blur. Below code shows how to load image and display it in a plot. Since I use
Matplotlib library to display the image, the RGB color order needs to be adjusted.  
 

file = "/Users/user/Desktop/image.jpg"

img0 = cv2.imread(file)
img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)

plt.imshow(img)
plt.show() 
 
 
 

Gaussian blur

    The Gaussian filter is a kernel that gives more weight to pixels that are closer to the center pixel and less weight to pixels that are farther away. It is implemented using the cv2.GaussianBlur() function in OpenCV.


gaussian = cv2.GaussianBlur(img, (5, 5), 5)

plt.imshow(gaussian)
plt.show()  

 
The first argument to this function is the image to be filtered, the second argument is the kernel size, which is set to (5, 5) and the last argument is the standard deviation in the x and y direction, which is set to 5.


Median blur

    Median blur is a non-linear image smoothing filter that is used to reduce noise in an image while preserving edges. It works by replacing the color of each pixel with the median of the colors in its neighborhood.

    Median blur is implemented using the cv2.medianBlur() function in OpenCV. The function takes two parameters: the input image and the kernel size. The kernel size parameter is used to specify the size of the neighborhood to consider for calculating the median of each pixel.


median = cv2.medianBlur(img, 7) plt.imshow(median) plt.show()  



Bilateral filter method
 
    The cv2.bilateralFilter() function is an edge-preserving filter that is used to blur an image while preserving the edges. Unlike the Gaussian filter, which only consider the pixel values in the neighborhood, the bilateral filter also takes into account the pixel intensity difference between the center pixel and the pixels in its neighborhood. 
    Below code shows how to implement bileteral blur in OpenCV. The function takes four parameters: the input image, the diameter of the neighborhood to consider, the standard deviation of the color space, and the standard deviation of the coordinate space.
 
 
bilateral = cv2.bilateralFilter(img, 7, 75, 75)

plt.imshow(bilateral)
plt.show()   


Averaging  method
 
    The cv2.blur() function can be used to apply the averaging method of blurring to an image. The function uses a square-shaped kernel with equal values in all its elements and the sum of all elements is equal to 1. The kernel is convolved with the image, and the value of each pixel in the output image is calculated by averaging the values of the pixels in the neighborhood of the current pixel.
      Below example shows how to implement averaging method with cv2.blur() function.
 
 
averaging = cv2.blur(img, (7, 7))

plt.imshow(averaging)
plt.show()     

    Below code shows how to display above blurred images in a plot.

 
gaussian = cv2.GaussianBlur(img, (15, 15), 5)
median = cv2.medianBlur(img, 25)
bilateral = cv2.bilateralFilter(img, 21,61,61)
average = cv2.blur(img, (25,25))

blurs = { 
    "Gaussian blur":gaussian,
    "Median blur": median,
    "Average blur": average,
    "Bilateral blur": bilateral }

i=0
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))
for row in range(2):
    for col in range(2):
        ax[row][col].imshow(list(blurs.values())[i])
        ax[row][col].set_title(list(blurs.keys())[i], fontsize=20)
        ax[row][col].axis('off')
        i = i + 1
        
plt.tight_layout()        
plt.show()   
    
  


 
    In this tutorial, we've briefly learned how to blur image with OpenCV functions. The full source code is listed below. 
 
 
Source code listing

 
import cv2
import matplotlib.pyplot as plt  
 
 
file = "/Users/user/Desktop/image.jpg"
img0 = cv2.imread(file)
img = cv2.cvtColor(img0, cv2.COLOR_BGR2RGB)
plt.imshow(img)
plt.show() 
 
gaussian = cv2.GaussianBlur(img, (5, 5), 5)
plt.imshow(gaussian)
plt.show()  

median = cv2.medianBlur(img, 7)
plt.imshow(median)
plt.show()  
 
bilateral = cv2.bilateralFilter(img, 7, 75, 75)
plt.imshow(bilateral)
plt.show()    
 
averaging = cv2.blur(img, (7, 7))
plt.imshow(averaging)
plt.show()     

 
gaussian = cv2.GaussianBlur(img, (15, 15), 5)
median = cv2.medianBlur(img, 25)
bilateral = cv2.bilateralFilter(img, 21,61,61)
average = cv2.blur(img, (25,25))

blurs = { 
    "Gaussian blur":gaussian,
    "Median blur": median,
    "Average blur": average,
    "Bilateral blur": bilateral }

i = 0
fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(12, 8))
for row in range(2):
    for col in range(2):
        ax[row][col].imshow(list(blurs.values())[i])
        ax[row][col].set_title(list(blurs.keys())[i], fontsize=20)
        ax[row][col].axis('off')
        i = i + 1
        
plt.tight_layout()        
plt.show()      
  
 
References:


No comments:

Post a Comment