Program 10: Write A Program To Blur and Smoothing An Image
Program 10: Write A Program To Blur and Smoothing An Image
import cv2
# Gaussian Blur
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Median Blur
median_blur = cv2.medianBlur(image, 5)
Output:
Explanation:
import cv2: This imports the OpenCV library, which is used for computer vision tasks.
cv2.GaussianBlur(image, (5, 5), 0): This function applies a Gaussian blur to the image.
cv2.bilateralFilter(image, 9, 75, 75): This function applies a bilateral filter to the image.
cv2.waitKey(0): This function waits indefinitely until a key is pressed. It keeps the image
windows open until a key event occurs.
cv2.destroyAllWindows(): This function closes all the OpenCV windows opened by the
program.
Summary
Make sure the image path in cv2.imread is correct and points to the location of your image
file.
Additional information
Gaussian Blur
Gaussian Blur is a widely used image processing technique for reducing image noise
and detail. It uses a Gaussian function to calculate the transformation to apply to
each pixel in the image. This results in a smoothing effect where the sharp edges are
blurred.
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
Kernel Size (5, 5): This defines the size of the kernel. A larger size results in a more
significant blur.
SigmaX (0): This specifies the standard deviation in the X direction. If set to 0, it is
calculated from the kernel size.
Smooths the image by averaging pixel values with a Gaussian weight.
Can blur edges, depending on the kernel size and sigma.
Useful for general noise reduction.
Median Blur
Median Blur is another popular image processing technique used primarily for
reducing noise while preserving edges. Unlike Gaussian Blur, which uses a weighted
average of neighboring pixels, Median Blur replaces each pixel's value with the
median value of the neighboring pixels.
median_blur = cv2.medianBlur(image, 5)
Kernel Size (5): This defines the size of the window around each pixel. The size must
be an odd number. A larger size results in a more significant noise reduction effect.
Reduces noise by taking the median value of neighboring pixels.
Preserves edges better than Gaussian Blur.
Particularly effective against salt-and-pepper noise.
Bilateral Filter
Bilateral filtering is an advanced blurring technique that preserves edges while
reducing noise. It combines Gaussian filtering in both the spatial and intensity
domains.
Motion Blur
Motion blur simulates the effect of the camera moving while capturing the image.
Bilateral Filtering
Bilateral filtering is an edge-preserving smoothing technique. It smooths the image
while maintaining sharp edges by combining Gaussian filtering in both the spatial
and intensity domains.