Dark Mode
Image

Blur (Image Smoothing)

Blurring is the commonly used technique for image processing to removing the noise. It is generally used to eliminate the high-frequency content such as noise, edges in the image. The edges are being blurred when we apply blur to the image. The advantages of blurring are the following:

Advantages of Blurring

The benefits of blurring are the following:

  • It removes low-intensity edges.
  • It helps in smoothing the image.
  • It is beneficial in hiding the details; for example, blurring is required in many cases, such as police intentionally want to hide the victim's face.

OpenCV provides mainly the following type of blurring techniques.

OpenCV Averaging

In this technique, the image is convolved with a box filter (normalize). It calculates the average of all the pixels which are under the kernel area and replaces the central element with the calculated average. OpenCV provides the cv2.blur() or cv2.boxFilter() to perform this operation. We should define the width and height of the kernel. The syntax of cv2.blur() function is following.

         cv2.blur(src, dst, ksize, anchor, borderType)  

Parameters:

src - It represents the source (input) image.

dst - It represents the destination (output) image.

ksize - It represents the size of the kernel.

anchor - It denotes the anchor points.

borderType - It represents the type of border to be used to the output.

Consider the following example:

im = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat_16x9.jpg')  
cv2.imshow('Original Image',im)  
cv2.imshow('Blurred Image', cv2.blur(im, (3,3)))  
cv2.waitKey(0)  
cv2.destroyAllWindows()

Output

OpenCV Blur

OpenCV Median Blur

The median blur operation is quite similar to the Gaussian blur. OpenCV provides the medianblur() function to perform the blur operation. It takes the median of all the pixels under the kernel area, and the central element is replaced with this median value. It is extremely effective for the salt-and-paper noise in the image. The kernel size should be a positive odd integer. Following is the syntax of this method.

       cv2.medianBlur(src, dst, ksize)  

Parameters:

src- It represents the source (input image).

dst - It represents the destination (output image).

ksize - It represents the size of the kernel.

Consider the following example:

import cv2    
import numpy    
# read image    
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat_16x9.jpg', 1)      
# apply gaussian blur on src image    
dst = median = cv2.medianBlur(img,5)  
# display input and output image    
cv2.imshow("Gaussian Smoothing", numpy.hstack((src, dst)))    
cv2.waitKey(0)  # waits until a key is pressed    
cv2.destroyAllWindows()  # destroys the window showing image  

Output

OpenCV Blur

OpenCV Gaussian Blur

Image smoothing is a technique which helps in reducing the noise in the images. Image may contain various type of noise because of camera sensor. It basically eliminates the high frequency (noise, edge) content from the image so edges are slightly blurred in this operation. OpenCV provide gaussianblur() function to apply smoothing on the images. The syntax is following:

      dst=cv2.GuassiasBlur(src, ksize, sigmaX[,dst[,sigmaY[, borderType=BORDER_DEFAULT]]]  

Parameters:

  • src -It is used to input an Image.
  • dst -It is a variable which stores an output Image.
  • ksize -It defines the Gaussian Kernel Size[height width ]. Height and width must be odd (1,3,5,..) and can have different values. If ksize is set to [0,0], then ksize is computed from sigma value.
  • sigmaX - Kernel standard derivation along X-axis.(horizontal direction).
  • sigmaY - Kernel standard derivation along Y-axis (vertical direction). If sigmaY = 0 then sigmaX value is taken for sigmaY.

borderType - These are the specified image boundaries while the kernel is applied on the image borders. Possible border type is:

  • cv.BORDER_CONSTANT
  • cv.BORDER_REPLICATE
  • cv.BORDER_REFLECT
  • cv.BORDER_WRAP
  • cv.BORDER_REFLECT_101
  • cv.BORDER_TRANSPARENT
  • cv.BORDER_REFLECT101
  • cv.BORDER_DEFAULT
  • cv.BORDER_ISOLATED

Consider the following example:

import cv2    
import numpy    
    
# read image    
src = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat_16x9.jpg', 1)      
# apply gaussian blur on src image    
dst = cv2.GaussianBlur(src, (5, 5), cv2.BORDER_DEFAULT)    
    
# display input and output image    
cv2.imshow("Gaussian Smoothing", numpy.hstack((src, dst)))    
cv2.waitKey(0)  # waits until a key is pressed    
cv2.destroyAllWindows()  # destroys the window showing image 

Output:

OpenCV Gaussian Blur

Comment / Reply From