[SOLVED]  DIP Assignment 2

30.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You will receive the following solution file(s) instantly after successful payment:

zip file icon Assignment-2-8rdddn.zip ()
Assignment Instructions Updated Recently? Submit Below and we will provide new Solution!
Submit New Instructions
🔒 Securely Powered by:
Secure Checkout
Rate this product

1 Download image files
[ ]: !wget –no-check-certificate ‘https://docs.google.com/uc?
,→export=download&id=1ktuKqlB6fXrYs_-0tc4TlvDfL0_Xao4i’ -O Lena.png !wget –no-check-certificate ‘https://docs.google.com/uc? ,→export=download&id=18fbzSR80lQ6t9rvS67CSIMQBDalYK1Lt’ -O dog.tif !wget –no-check-certificate ‘https://docs.google.com/uc?
,→export=download&id=1bsKGmmUnVEuQN5XepxjHyQ-qIQjgaZdn’ -O images.zip
!unzip images.zip -d images/
–2021-04-18 14:14:35-https://docs.google.com/uc?export=download&id=1ktuKqlB6fXrYs_-0tc4TlvDfL0_Xao4i
Resolving docs.google.com (docs.google.com)… 142.250.1.113, 142.250.1.102,
142.250.1.101, …
Connecting to docs.google.com (docs.google.com)|142.250.1.113|:443… connected.
HTTP request sent, awaiting response… 302 Moved Temporarily
Location: https://doc-14-3c-docs.googleusercontent.com/docs/securesc/ha0ro937gcu c7l7deffksulhg5h7mbp1/8dgf4s02ukn0bt87isisv7botfa2fni7/1618755225000/14943465539 724169100/*/1ktuKqlB6fXrYs_-0tc4TlvDfL0_Xao4i?e=download [following] Warning: wildcards not supported in HTTP.
–2021-04-18 14:14:36– https://doc-14-3c-docs.googleusercontent.com/docs/secur esc/ha0ro937gcuc7l7deffksulhg5h7mbp1/8dgf4s02ukn0bt87isisv7botfa2fni7/1618755225
000/14943465539724169100/*/1ktuKqlB6fXrYs_-0tc4TlvDfL0_Xao4i?e=download
Resolving doc-14-3c-docs.googleusercontent.com
(doc-14-3c-docs.googleusercontent.com)… 142.250.103.132,
2607:f8b0:4001:c2b::84
Connecting to doc-14-3c-docs.googleusercontent.com
(doc-14-3c-docs.googleusercontent.com)|142.250.103.132|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 473831 (463K) [image/png]
Saving to: ‘Lena.png’
Lena.png 100%[===================>] 462.73K –.-KB/s in 0.005s
2021-04-18 14:14:36 (100 MB/s) – ‘Lena.png’ saved [473831/473831]
–2021-04-18 14:14:36-https://docs.google.com/uc?export=download&id=18fbzSR80lQ6t9rvS67CSIMQBDalYK1Lt
Resolving docs.google.com (docs.google.com)… 142.250.1.139, 142.250.1.101,
142.250.1.138, …
Connecting to docs.google.com (docs.google.com)|142.250.1.139|:443… connected.
HTTP request sent, awaiting response… 302 Moved Temporarily
Location: https://doc-0g-3c-docs.googleusercontent.com/docs/securesc/ha0ro937gcu c7l7deffksulhg5h7mbp1/3de2bg4jp14por2lmgrll9q6h8bfe7v9/1618755225000/14943465539 724169100/*/18fbzSR80lQ6t9rvS67CSIMQBDalYK1Lt?e=download [following] Warning: wildcards not supported in HTTP.
–2021-04-18 14:14:37– https://doc-0g-3c-docs.googleusercontent.com/docs/secur esc/ha0ro937gcuc7l7deffksulhg5h7mbp1/3de2bg4jp14por2lmgrll9q6h8bfe7v9/1618755225
000/14943465539724169100/*/18fbzSR80lQ6t9rvS67CSIMQBDalYK1Lt?e=download
Resolving doc-0g-3c-docs.googleusercontent.com
(doc-0g-3c-docs.googleusercontent.com)… 142.250.103.132,
2607:f8b0:4001:c2b::84
Connecting to doc-0g-3c-docs.googleusercontent.com
(doc-0g-3c-docs.googleusercontent.com)|142.250.103.132|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 58400 (57K) [image/jpeg]
Saving to: ‘dog.tif’ dog.tif 100%[===================>] 57.03K –.-KB/s in 0.001s
2021-04-18 14:14:38 (90.6 MB/s) – ‘dog.tif’ saved [58400/58400]
–2021-04-18 14:14:38-https://docs.google.com/uc?export=download&id=1bsKGmmUnVEuQN5XepxjHyQ-qIQjgaZdn
Resolving docs.google.com (docs.google.com)… 142.250.1.138, 142.250.1.102,
142.250.1.139, …
Connecting to docs.google.com (docs.google.com)|142.250.1.138|:443… connected.
HTTP request sent, awaiting response… 302 Moved Temporarily
Location: https://doc-10-3c-docs.googleusercontent.com/docs/securesc/ha0ro937gcu c7l7deffksulhg5h7mbp1/limto7vu1cansl8cj5icebss942gqi11/1618755225000/14943465539 724169100/*/1bsKGmmUnVEuQN5XepxjHyQ-qIQjgaZdn?e=download [following] Warning: wildcards not supported in HTTP.
–2021-04-18 14:14:38– https://doc-10-3c-docs.googleusercontent.com/docs/secur esc/ha0ro937gcuc7l7deffksulhg5h7mbp1/limto7vu1cansl8cj5icebss942gqi11/1618755225
000/14943465539724169100/*/1bsKGmmUnVEuQN5XepxjHyQ-qIQjgaZdn?e=download
Resolving doc-10-3c-docs.googleusercontent.com
(doc-10-3c-docs.googleusercontent.com)… 142.250.103.132,
2607:f8b0:4001:c2b::84
Connecting to doc-10-3c-docs.googleusercontent.com
(doc-10-3c-docs.googleusercontent.com)|142.250.103.132|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 595700 (582K) [application/zip]
Saving to: ‘images.zip’
images.zip 100%[===================>] 581.74K –.-KB/s in 0.005s
2021-04-18 14:14:38 (124 MB/s) – ‘images.zip’ saved [595700/595700]
Archive: images.zip inflating: images/dog.tif inflating: images/hdraw.png inflating: images/hdraw_0.png inflating: images/hdraw_1.png inflating: images/hdraw_2.png inflating: images/hdraw_3.png inflating: images/hdraw_4.png inflating: images/hdraw_5.png inflating: images/hdraw_6.png inflating: images/hdraw_7.png inflating: images/hdraw_9.png inflating: images/Lena.png inflating: images/Lena_left_eye.png
2 Question 1
Download Lena color image convert it to grayscale image and add salt and pepper noise with noise quantity 0.1,0.2 upto 1 and generate 10 noisy images. a) Do average filtering ( by correlating with average filter ) of varying sizes for each image. Filter size can be 33, 55, 77. (In 33 filter all the values are 1/9, in 55 filter all the values are 1/25 and in 77 filter all the values are 1/49) b) Similarly, repeat the question 1.a by replacing the average filter by median filter.

% error: correlation: 0.0

[[0.11111111 0.11111111 0.11111111]
[0.11111111 0.11111111 0.11111111]
[0.11111111 0.11111111 0.11111111]] avg filter window for window size 5:
[[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]
[0.04 0.04 0.04 0.04 0.04]] avg filter window for window size 7:
[[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]
[0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816]]

3 Question 2

3.1 Part a
Consider the image the attached named as hdraw.png and crop each of the characters from the image and consider that as the sub-image. Find the location of the sub-image in the original by using correlation.

3.2 Part b
Download Lena color image convert it to grayscale image and crop the left eye of Lena as subimage and do the cross-correlation (Normalized correlation) to find the location of the left eye in the original image.

4 Question 3
Write a function to implement FFT for 1D signal.

FFT using user-defined function:
[(28+0j), (-4+9.65685424949238j), (-4.000000000000001+4j),
(-4.000000000000002+1.6568542494923797j), (-4+0j),
(-3.9999999999999996-1.6568542494923797j), (-3.999999999999999-4j),
(-3.999999999999998-9.65685424949238j)]
FFT using numpy implementation:
[28.+0.j -4.+9.65685425j -4.+4.j -4.+1.65685425j
-4.+0.j -4.-1.65685425j -4.-4.j -4.-9.65685425j]
% Error = -1.3322676295501878e-13
5 Question 4
Implement DFT function for an image using the FFT for 1D signal using question 3.

[[424. +0.j -80.+80.j -80. +0.j -80.-80.j]
[ -8. +8.j 0. +0.j 0. +0.j 0. +0.j]
[ -8. +0.j 0. +0.j 0. +0.j 0. +0.j]
[ -8. -8.j 0. +0.j 0. +0.j 0. +0.j]]
FFT using numpy implementation:
[[424. +0.j -80.+80.j -80. +0.j -80.-80.j]
[ -8. +8.j 0. +0.j 0. +0.j 0. +0.j]
[ -8. +0.j 0. +0.j 0. +0.j 0. +0.j]
[ -8. -8.j 0. +0.j 0. +0.j 0. +0.j]]
% Error = -8.881784197001252e-14
6 Question 5
Consider the images of lena and dog images attached. Find phase and magnitude of the dog and lena images using DFT function implemented in question 4.
[ ]: import matplotlib.pyplot as plt from google.colab.patches import cv2_imshow
”’
View the fourier transformed image along with phase and magnitude parts of the␣ ,→transform.
Since it is not possible to view complex values in an image, the functions␣
,→default to taking the real part of the pixel values and where ever needed, clip the value to the␣ ,→range [0, 255]
”’ def view_fourier(img):
img_FFT = FFT_2D(img)
fig,elements=plt.subplots(nrows=2,ncols=4,figsize=(30,15))
phase = np.angle(img_FFT, deg=True) magnitude = np.log(np.abs(img_FFT))
elements[0,0].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) elements[0,0].set_title(“Original Image”, fontsize=30) elements[0,1].imshow(np.real(img_FFT.clip(0, 255)), cmap=’gray’) elements[0,1].set_title(“Fourier Transform”, fontsize=30) elements[0,2].imshow(phase, cmap=’gray’) elements[0,2].set_title(“Phase”, fontsize=30) elements[0,3].imshow(magnitude, cmap=’gray’) elements[0,3].set_title(“Magnitude”, fontsize=30)
img_FFT_shifted = np.fft.fftshift(img_FFT) phase_shifted = np.angle(img_FFT_shifted, deg=True)
magnitude_shifted = np.log(np.abs(img_FFT_shifted)) # Normalizing the␣
,→matrix for visual purposes
elements[1,0].imshow(img, cmap=’gray’)
elements[1,0].set_title(“Original Image”, fontsize=30)
elements[1,1].imshow(np.real(img_FFT_shifted.clip(0, 255)), cmap=’gray’) elements[1,1].set_title(“Fourier Transform (center shifted)”, fontsize=30) elements[1,2].imshow(phase_shifted, cmap=’gray’) elements[1,2].set_title(“Phase (center shifted)”, fontsize=30) elements[1,3].imshow(magnitude_shifted, cmap=’gray’) elements[1,3].set_title(“Magnitude (center shifted)”, fontsize=30)
plt.show()
[ ]: lena = cv2.imread(“Lena.png”, 0) view_fourier(lena)

7 Question 6 and 7
Swap phase of dog image and lena image and display the output.

[0 1 2 3 4 5 6 7]
[(28+0j), (-4+9.65685424949238j), (-4.000000000000001+4j),
(-4.000000000000002+1.6568542494923797j), (-4+0j), (-3.9999999999999996-1.6568542494923797j), (-3.999999999999999-4j),
(-3.999999999999998-9.65685424949238j)]
[0.+0.00000000e+00j 1.-2.71834567e-16j 2.+4.44089210e-16j
3.+3.82856870e-16j 4.+0.00000000e+00j 5.-4.97899625e-17j
6.-4.44089210e-16j 7.-6.12323400e-17j]
[ ]: lena = cv2.imread(“Lena.png”, 0) dog = cv2.imread(“dog.tif”, 0) dog = cv2.resize(dog, (512, 512)) # resizing so that dims are equal to␣
,→2^n.
# (512, 512) is chosen since lena image␣ ,→is of size (512, 512)
lena_FFT = FFT_2D(lena) dog_FFT = FFT_2D(dog)
lena_magnitude = np.absolute(lena_FFT) lena_phase = np.angle(lena_FFT)
dog_magnitude = np.absolute(dog_FFT) dog_phase = np.angle(dog_FFT)
lena_mag_dog_phase_FFT = rect_to_polar_vec(lena_magnitude, dog_phase) lena_mag_dog_phase = IFFT_2D(lena_mag_dog_phase_FFT)
dog_mag_lena_phase_FFT = rect_to_polar_vec(dog_magnitude, lena_phase) dog_mag_lena_phase = IFFT_2D(dog_mag_lena_phase_FFT) fig, elements=plt.subplots(nrows=2, ncols=2, figsize=(15, 15))
elements[0,0].imshow(cv2.cvtColor(lena, cv2.COLOR_BGR2RGB)) elements[0,0].set_title(“Lena Image”, fontsize=30) elements[0,1].imshow(cv2.cvtColor(dog, cv2.COLOR_BGR2RGB)) elements[0,1].set_title(“Dog Image”, fontsize=30) elements[1,0].imshow(np.real(lena_mag_dog_phase), cmap=’gray’) elements[1,0].set_title(“Lena Magnitude Dog Phase”, fontsize=30) elements[1,1].imshow(np.real(dog_mag_lena_phase), cmap=’gray’) elements[1,1].set_title(“Dog Magnitude Lena Phase”, fontsize=30) plt.show()

  • Assignment-2-8rdddn.zip