Program 10: Write a program to blur and smoothing an image.
import cv2
# Load the image
image = cv2.imread("D:/DSATM/CG/CG&DIP/Lab_Images/Prog7_flower.jpg")
# Gaussian Blur
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Median Blur
median_blur = cv2.medianBlur(image, 5)
# Bilateral Filter Smoothing
bilateral_filter = cv2.bilateralFilter(image, 9, 75, 75)
# Display the original and processed images
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Blur', gaussian_blur)
cv2.imshow('Median Blur', median_blur)
cv2.imshow('Bilateral Filter', bilateral_filter)
# Wait for a key press to close the windows
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Explanation:
import cv2: This imports the OpenCV library, which is used for computer vision tasks.
cv2.imread("D:/DSATM/CG/CG&DIP/Lab_Images/Prog7_flower.jpg"): This function
reads the image from the specified path. The image is loaded in color mode by default.
Applying Gaussian Blur
cv2.GaussianBlur(image, (5, 5), 0): This function applies a Gaussian blur to the image.
The first argument is the source image.
The second argument (5, 5) specifies the kernel size. The kernel is a matrix that
slides over the image, applying the Gaussian blur.
The third argument 0 specifies the standard deviation in the X direction. If set to 0, it
is calculated from the kernel size.
Applying Median Blur
cv2.medianBlur(image, 5): This function applies a median blur to the image.
The first argument is the source image.
The second argument 5 specifies the kernel size. The kernel is a square matrix that
slides over the image, and the median of the pixels under the kernel is calculated
and used as the output pixel value.
Applying Bilateral Filter
cv2.bilateralFilter(image, 9, 75, 75): This function applies a bilateral filter to the image.
The first argument is the source image.
The second argument 9 specifies the diameter of each pixel neighborhood used
during filtering.
The third argument 75 is the sigma value for the color space. A larger value means
that farther colors within the pixel neighborhood will be mixed together, resulting
in larger areas of semi-equal color.
The fourth argument 75 is the sigma value for the coordinate space. A larger value
means that farther pixels will influence each other as long as their colors are close
enough.
Displaying the Images
cv2.imshow('Original Image', image): This function displays the original image in
a window titled "Original Image".
cv2.imshow('Gaussian Blur', gaussian_blur): This function displays the Gaussian
blurred image in a window titled "Gaussian Blur".
cv2.imshow('Median Blur', median_blur): This function displays the median
blurred image in a window titled "Median Blur".
cv2.imshow('Bilateral Filter', bilateral_filter): This function displays the image
with the bilateral filter applied in a window titled "Bilateral Filter".
Waiting for a Key Press and Closing the Windows
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
This program performs the following tasks:
1. Loads an image from a specified path.
2. Applies Gaussian blur to the image.
3. Applies median blur to the image.
4. Applies bilateral filter to the image.
5. Displays the original and processed images in separate windows.
6. Waits for a key press and then closes all the displayed windows.
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.
Several other blurring techniques available in image processing:
Average (Box) Blur
This is the simplest blurring technique. It takes the average of all the pixels under
the kernel area and replaces the central element with this average.
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.
Custom Kernel Blur
You can create custom blurring effects by defining your own kernel. This allows for
a wide range of blurring effects.
Image smoothing is a fundamental operation in image processing that helps in reducing
noise and details in an image. Various techniques can be used for smoothing, each with its
own characteristics and applications.
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.