Blur/Smooth Images
OpenCV blur is a function used to reduce noise and detail in an image. Blur is a very useful tool when dealing with images of low quality or images with a lot of noise. Blur can also be used to give images a smoother and more professional look. OpenCV blur is a widely used function and can be used in a variety of applications. There are various ways to blur an image in OpenCV like - Averaging, Median blur, Gaussian blur, etc. In this tutorial, we will learn how to smooth images with OpenCV in python.
Let's understand the concept with some practical work.
1. Averaging - Averaging blur is a common image processing technique used to reduce noise and reduce detail in an image. It works by taking the average of the pixels of the original image and replacing the original pixel with this average. In OpenCV, it can be achieved using the cv2.blur() or cv2.boxFilter() functions.
Example:
Input
Code:
import cv2
img = cv2.imread('demo.png')
image = cv2.blur(img,(15,15))
cv2.imshow('Rotated Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here, first, we imported cv2. then we used cv2.imread to read the image. then we used the cv2.blur() function to apply a 15x15 averaging filter to the image, which blurs the image. At last, we used cv2.imshow() to display the final image.
Result:
2. Median Blur - The median blur operation in OpenCV is a non-linear image smoothing technique that can also be used to reduce noise and other small variations in an image. It works by replacing each pixel in the image with the median value of the pixels in a small neighborhood around it.
Example:
Code:
import cv2
img = cv2.imread('demo.png')
image = cv2.medianBlur(img,15)
cv2.imshow('Rotated Image', image)
cv2.imwrite('Averaging.jpg', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here, first, we imported cv2. then we used cv2.imread to read the image. then we used the cv2.medianBlur() function to apply a 15 median value filter to the image, which blurs the image. At last, we used cv2.imshow() to display the final image.
Result:
3. Gaussian Blur - Gaussian blur is also a smoothing technique that is used to reduce noise in an image. It is named after the Gaussian function. The basic idea behind a Gaussian blur is to replace the value of each pixel in an image with the average value of the pixels in its surrounding area.
Example:
Code:
import cv2
img = cv2.imread('demo.png')
image = cv2.GaussianBlur(img,(15,15),0)
cv2.imshow('Rotated Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here, first, we imported cv2. then we used cv2.imread to read the image. then we used the cv2.GussianBlur() function to apply a 15x15 Gaussain blur filter to the image. At last, we used cv2.imshow() to display the final image.
Result: