0% found this document useful (0 votes)
17 views18 pages

CVT-Assignment: Sujal Badgujar BT22CSA040

Assesment

Uploaded by

kyakrnahetujhe
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)
17 views18 pages

CVT-Assignment: Sujal Badgujar BT22CSA040

Assesment

Uploaded by

kyakrnahetujhe
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/ 18

CVT-Assignment

Sujal Badgujar
BT22CSA040

Q1: What is noise model? what are the most commonly encountered noise models in image
processing.
Ans: In image processing, a noise model refers to a mathematical representation of noise that distorts
the original image. Noise is often introduced during image acquisition, transmission, or compression, and
understanding the noise model helps in designing appropriate filters or techniques for noise removal.
Most Commonly Encountered Noise Models:
Gaussian Noise: Noise is distributed according to a Gaussian distribution (bell curve).
Salt-and-Pepper Noise: Randomly replaces some pixel values with either maximum (white) or minimum
(black) intensity values.
Poisson Noise: Arises from the discrete nature of photon counting in cameras.
Speckle Noise: Multiplies the image signal by noise
Uniform Noise: Noise values are uniformly distributed between a minimum and maximum range.
Quantization Noise: Caused by the quantization process during analog-to-digital conversion, where
continuous signals are mapped to discrete levels.

Q2: An 8-bit grayscale image is corrupted by Gaussian noise with a mean of 0 and a variance of 25.
Explain how you would model this noise mathematically and discuss its effect on the image histogram.

Ans: Gaussian noise can be modelled mathematically by using the Gaussian distribution formula
which is mentioned above and then adding it to the input image as:-

𝐺(𝑥, 𝑦) = 𝐼(𝑥, 𝑦) + η(𝑥, 𝑦), where η(𝑥, 𝑦) is the Gaussian Noise. In this
2
(𝑧−𝑚)
− 2

Gaussian Noise the formula for which is 𝑝(𝑧) = 1 𝑒 2σ


has its mean
σ
2
value as 0 and standard deviation ( as 25. So after
σ ) substituting these values we will get the
output image 𝐺(𝑥, 𝑦)

The effect of Image in Histogram will be:-

The histogram of the original image will spread out or become wider.

The peak of the histogram will lower in height but broaden, reflecting the added variance.

Since the mean of the noise is 0, the histogram’s central position will remain similar, but with
more pixels appearing at slightly higher and lower intensity levels due to the added noise.

Q3: Consider an image with a resolution of 256x256 pixels. If the image is corrupted by salt-and-
pepper noise with a noise density of 0.1, how many pixels will be affected by this noise? Show
your calculations.

Ans: Total number of pixels = 256 × 256


= 65536 pixels
Noise Density = 0. 1
So, the number of pixels affected = 𝑃(𝑧) = number of pixels × Noise Density
= 65536 × 0. 1 ≈ 6554 pixels
Thus, approximately 6554 pixels will be affected by this salt and pepper noise.

Q4: Explain the process of using a notch filter to reduce this noise and provide the general form of
the notch filter in the frequency domain. Consider suitable example. Apply the notch filtering
process on a sample noisy image using Python. Present the code, the filtered Fourier spectrum,
and the restored image.

Ans: A notch filter is primarily used in the frequency domain to remove specific frequency components
from an image. It is particularly useful in eliminating periodic noise. However, salt and pepper noise is a
type of impulse noise, which appear as white and black dots randomly distributed in the image typically
affecting specific pixels rather than periodic patterns.

In practice, notch filters are more suitable for removal of periodic noise but can sometimes be adapted
or combined with other filtering techniques to mitigate the effects of salt and pepper noise, especially if
noise introduces periodic like components in the frequency domain.
Process: -

1) Convert img into frequency domain using DFT


2) Design Notch Filter
3) Apply Notch Filter
4) Inverse Fourier transform
5) Normalize and Display Image.

CODE:
import cv2 as cv import numpy as np
from matplotlib import pyplot as plt
def notch_filter(shape, u_k, v_k, D0=10):
rows, cols = shape
u = np.arange(rows)
v = np.arange(cols)
U, V = np.meshgrid(u, v, sparse=False, indexing='ij')
D1 = np.sqrt((U - u_k)**2 + (V - v_k)**2)
D2 = np.sqrt((U + u_k)**2 + (V + v_k)**2)
H = 1 - np.exp(-0.5 * ((D1 * D2) / (D0**2)))
return H

img = cv.imread('lena_noisy.png', 0)

dft = cv.dft(np.float32(img), flags=cv.DFT_COMPLEX_OUTPUT)


dft_shift = np.fft.fftshift(dft)

rows, cols = img.shape


notch = notch_filter((rows, cols), rows//2, cols//2, D0=30)
dft_shift[:, :, 0] *= notch
dft_shift[:, :, 1] *= notch
f_ishift = np.fft.ifftshift(dft_shift)
img_back = cv.idft(f_ishift)
img_back = cv.magnitude(img_back[:, :, 0],

img_back[:, :, 1]) plt.figure(figsize=(12, 6))

plt.subplot(131), plt.imshow(img, cmap='gray')


plt.title('Noisy Image'), plt.xticks([]), plt.yticks([])

plt.subplot(132), plt.imshow(np.log(1 + np.abs(dft_shift[:, :, 0])), cmap='gray')


plt.title('Filtered Fourier Spectrum'), plt.xticks([]), plt.yticks([])

plt.subplot(133), plt.imshow(img_back, cmap='gray') plt.title('Restored Image'), plt.xticks([]),


plt.yticks([])

plt.show()
Q5:
Describe the difference between linear and non-linear filtering techniques for noise reduction in
the spatial domain. Provide examples of each and discuss their effectiveness against different noise
models.

Ans:

Linear Filtering Techniques:-

Linear filtering techniques involve the application of a linear operation to the pixels of an image.
Each output pixel is compared as a weighted sum of neighboring input pixels, where the weights
are defined by the filter kernel. The filtering process is linear because the output for any input is
directly proportional to the input itself.

Examples:-

● Mean Filter
● Gaussian Filter Effectiveness:-

Linear Filtering method is particularly effective in reducing gaussian noise due to their smoothing
properties.
Linear Filters are less effective against impulsive noise because they tend to blur the image and do
not preserve the edges well.
Non-Linear Filtering Techniques:-

Non Linear filtering techniques involve the application of non-linear operation to the pixels of an image.
Unlike linear filters, non-linear filters can adapt based on the local characteristics of the image, making
them more effective in preserving important features like edges and details.

Examples:-

● Median Filters
● Bilateral Filters

Effectiveness:-

➔ Against both salt and pepper noise as well as Gaussian noise, making them more
versatile than linear filters in various scenarios.

Comparison:-

Linear Non-Linear

Linear filters are not as good as nonlinear Non-linear filters outperform linear ones in
ones when it comes to edge preservation preserving fine details

Linear filters are effective against Gaussian Non-linear filters are effective against
Noise Impulse noise

Linear filters require less computational Non-linear filters require more


complexity computational complexity
Q6:
Consider the following 5x5 pixel grayscale image (attached below) corrupted by salt-and-pepper noise:

i. Apply a median filter with a 3x3 window on this image and show the resulting 5x5 matrix after
filtering.
ii. Compare the effectiveness of a median filter with an average filter (mean filter) for removing
salt-and-pepper noise, using the above image as an example.
Ans:

Initial Matrix (A6):

[ 10 20 255 20 10 ]
[ 20 90 40 255 20 ]
[255 40 85 40 20 ]
[ 20 255 40 30 20 ]
[ 10 10 255 20 10 ]

1. First padded with 01:


[ 01 01 01 01 01 01 01 ]
[ 01 10 20 255 20 10 01 ]
[ 01 20 90 40 255 20 01 ]
[ 01 255 40 85 40 20 01 ]
[ 01 20 255 40 30 20 01 ]
[ 01 10 10 255 20 10 01 ]
[ 01 01 01 01 01 01 01 ]

2. Median Filter:

Using a 3x3 window


After applying the median filter:
[ 2 2 2 0
0 0 0 0 ]
[ 4 4 4 2
2 0 0 0 0
0 ]
[ 4 4 4 2
4 0 0 0 0
0 ]
[ 4 4 4 2
2 0 0 0 0
0 ]
[ 2 2 2 0
0 0 0 0 ]
3. Mean Filter:
Rounded values are considered. [ 9 42 69 67 34 ]
[ 42 74 85 102 62 ]
[ 42 83 89 108 75 ]
[ 67 102 102 102 84 ]
[ 34 62 75 62 32 ]

3. Mean Filter:
Rounded values are considered. [ 9 42 69 67 34 ]
[ 42 74 85 102 62 ]
[ 42 83 89 108 75 ]
[ 67 102 102 102 84 ]
[ 34 62 75 62 32 ]
Median filters effectively remove the salt and pepper noise while preserving the edges. The
average filter on the other hand blurs the images and is less effective at removing salt and pepper
noise

Q7:
Develop a Python (or any programming language) script to apply a Wiener filter on a noisy image.
Explain how the Wiener filter adapts to the local mean and variance of the image.

Ans:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import wiener

image = cv2.imread('noisy_image.jpg', 0)

wiener_filtered = wiener(image, (5, 5))


wiener_filtered = np.uint8(np.clip(wiener_filtered, 0, 255))

plt.figure(figsize=(8, 4))

plt.subplot(1, 2, 1)
plt.title('Original Noisy Image')
plt.imshow(image, cmap='gray')

plt.subplot(1, 2, 2)
plt.title('Wiener Filtered Image')
plt.imshow(wiener_filtered, cmap='gray')
plt.tight_layout()
plt.show()
How the Wiener Filter Works:
Local Mean: The Wiener filter calculates the mean of pixel intensities within the specified local
neighborhood (e.g., a 5x5 window around each pixel).
This local mean provides an estimate of the true intensity in smooth regions.

Local Variance: The local variance measures the spread of pixel intensities within the same window.
If the variance is high (e.g., near edges or textures), the filter will reduce noise less aggressively. If
the variance is low, it assumes the region is smooth and applies stronger noise reduction.

Adapting to Noise: The filter subtracts the noise variance from the local variance to decide how much
smoothing to apply. If the noise variance is higher than the local variance, the filter avoids strong
smoothing to prevent blurring details.

How the Filter Adapts:


In Smooth Regions: The local variance is low, so the Wiener filter strongly reduces noise by
averaging the pixel with its neighbors.

In Edge Regions: The local variance is high due to the change in intensity, so the filter applies less
smoothing to preserve details like edges.
Q8:
Implement a hybrid approach by first applying a frequency domain technique (like notch filtering)
followed by a spatial domain technique (like median filtering) on a noisy image. Compare the results with
using each technique individually.

Ans:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from scipy import fftpack

def apply_notch_filter(image, d0=30, u_k=0, v_k=0):


rows, cols = image.shape
crow, ccol = rows // 2, cols // 2
f_transform = np.fft.fft2(image)
f_shift = np.fft.fftshift(f_transform)
mask = np.ones((rows, cols), np.uint8)

for u in [-u_k, u_k]:


for v in [-v_k, v_k]:
mask[crow + u - d0:crow + u + d0, ccol + v - d0:ccol + v + d0] = 0

f_shift_filtered = f_shift * mask


f_ishift = np.fft.ifftshift(f_shift_filtered)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)

return np.uint8(img_back)
def apply_median_filter(image, kernel_size=3):
return cv2.medianBlur(image, kernel_size)

image = cv2.imread('noisy_image.jpg', 0)

notch_filtered = apply_notch_filter(image, d0=30, u_k=20, v_k=20)


median_filtered = apply_median_filter(image, kernel_size=3)
hybrid_filtered = apply_median_filter(notch_filtered, kernel_size=3)

plt.figure(figsize=(10, 8))

plt.subplot(2, 2, 1)
plt.title('Original Noisy Image')
plt.imshow(image, cmap='gray')

plt.subplot(2, 2, 2)
plt.title('Notch Filtered Image')
plt.imshow(notch_filtered, cmap='gray')

plt.subplot(2, 2, 3)
plt.title('Median Filtered Image')
plt.imshow(median_filtered, cmap='gray')

plt.subplot(2, 2, 4)
plt.title('Hybrid (Notch + Median) Filtered Image')
plt.imshow(hybrid_filtered, cmap='gray')

plt.tight_layout()
plt.show()
Result Comparison:
Notch Filtering: This will remove periodic noise (such as regular patterns or stripes) by filtering out
specific frequency components. The result should show that periodic artifacts are removed, but
some speckle or impulse noise might still be present.

Median Filtering: This will remove impulsive noise (like salt-and-pepper) by replacing each pixel
with the median value of its neighborhood. It works well on small noise specks but may not handle
periodic noise effectively.

Hybrid Approach: By first removing periodic noise in the frequency domain and then applying median
filtering to clean up impulsive noise, the hybrid approach should produce the cleanest result by
addressing both types of noise.

You might also like