0% found this document useful (0 votes)
39 views4 pages

4experiment Writeup DPCOE

Uploaded by

Sachin Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views4 pages

4experiment Writeup DPCOE

Uploaded by

Sachin Rathod
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DHOLE PATIL EDUCATION SOCIETY’s

DHOLE PATIL COLLEGE OF ENGINEERING


DEPARTMENT: E&TC Engg ACADEMIC YEAR:

CLASS: TE SEMESTER : 6

SUBJECT: DIP DATE:

NAME: ROLL NO.:

TITLE : Implement various Smoothing spatial filters.

AIM Learn to:

(a) Blur images with various low-pass filters


(b) Apply custom-made filters to images (2D convolution)

SOFTWARE REQUIREMENT: Matlab /Python

THEORY:
Image Blurring (Image Smoothing)

Image blurring is achieved by

convolving the image with a low-pass filter kernel. It is useful in removing noise. It removes high-
frequency content from the image and results in blurred edges. OpenCV provides mainly four
types of blurring techniques.

1. Averaging

This is done by convolving the image with a normalized box filter. It simply takes the average of
all the pixels under the kernel area and replaces the central element with this average. This is done
by the function cv2.blur() or cv2.boxFilter(). We should specify the width and height of the
kernel. A 3x3 normalized box filter would look like this:

DPCOE/E&TC DIP 1
2. Gaussian Filtering

In this approach, instead of a box filter consisting of equal filter coefficients, a Gaussian kernel is
used. It is done with the function, cv2.GaussianBlur(). We should specify the width and height of
the kernel which should be positive and odd. We also should specify the standard deviation in the
X and Y directions, σx and σy respectively. If only σx is specified, σy is taken as equal to σx. If both
are given as zeros, they are calculated from the kernel size. Gaussian filtering is highly effective
in removing Gaussian noise from the image.

If you want, you can create a Gaussian kernel with the function, cv2.getGaussianKernel().

The above code can be modified for Gaussian blurring:

blur = cv2.GaussianBlur(img,(5,5),0)

3. Median Filtering

Here, the function cv2.medianBlur() computes the median of all the pixels under the kernel
window and the central pixel is replaced with this median value. This is highly effective in
removing salt-and-pepper noise. One interesting thing to note is that, in the Gaussian and box
filters, the filtered value for the central element can be a value that may not exist in the original
image. However, this is not the case in median filtering, since the central element is always
replaced by some pixel value in the image. This reduces the noise effectively. The kernel size must
be a positive odd integer.

In this demo, we add a 50% noise to our original image and use a median filter. Check the result:

median = cv2.medianBlur(img,5)

4. Bilateral Filtering

The filters we presented earlier tend to blur edges. This is not the case for the bilateral
filter, cv2.bilateralFilter(), which was defined for, and is highly effective at noise removal while
preserving edges. But the operation is slower compared to other filters.

DPCOE/E&TC DIP 2
The bilateral filter also uses a Gaussian filter in the space domain, but it also uses one more
(multiplicative) Gaussian filter component which is a function of pixel intensity differences. The
Gaussian function of space makes sure that only pixels are ‘spatial neighbors’ are considered for
filtering, while the Gaussian component applied in the intensity domain (a Gaussian function of
intensity differences) ensures that only those pixels with intensities similar to that of the central
pixel (‘intensity neighbors’) are included to compute the blurred intensity value. As a result, this
method preserves edges, since for pixels lying near edges, neighboring pixels placed on the other
side of the edge, and therefore exhibiting large intensity variations when compared to the central
pixel, will not be included for blurring.

The sample below demonstrates the use of bilateral filtering (For details on arguments, see the
OpenCV docs).

blur = cv2.bilateralFilter(img,9,75,75)

ALGORITHMS:

Convolution with Averaging filter of size 3x3


import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('opencv_logo.png')

kernel = np.ones((3,3),np.float32)/9

dst = cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

1) Averaging with a box filter of size 3x3


import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('opencv_logo.png')

blur = cv2.blur(img,(3,3))

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')

DPCOE/E&TC DIP 3
plt.xticks([]), plt.yticks([])
plt.show()

RESULTS:

See the attachment

CONCLUSION:
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

REFERENCES:
1. Rafael C. Gonzalez and Richard E. Woods, “Digital Image Processing”, 3rd Edition,
Pearson Education.
2. S Sridhar, “Digital Image Processing”, Oxford University Press.
3. Rafael C. Gonzalez, Richard E. Woods, and Steven L. Eddins, “Digital Image Processing
using MATLAB”, 2nd Edition, Tata McGraw Hill Publication.

Submission date: / / Subject Teacher

DPCOE/E&TC DIP 4

You might also like