C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
OpenCV 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 BlurringThe benefits of blurring are the following:
OpenCV provides mainly the following type of blurring techniques. OpenCV AveragingIn 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 Median BlurThe 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 Gaussian BlurImage 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:
borderType - These are the specified image boundaries while the kernel is applied on the image borders. Possible border type is:
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:
Next TopicOpenCV Image Filters
|