# -*- coding: utf-8 -*- """Copy of Lecture10_Exercises.ipynb Automatically generated by Colaboratory. ##Lecture 10 Take-home Exercises. Discussion is allowed. But you must turn your own independent...

1 answer below »
please see attached .py file for instructions


# -*- coding: utf-8 -*- """Copy of Lecture10_Exercises.ipynb Automatically generated by Colaboratory. ##Lecture 10 Take-home Exercises. Discussion is allowed. But you must turn your own independent work! """ import matplotlib.pyplot as plt import numpy as np from scipy import fftpack import imageio """Exericse 0 Basic Image FFT. """ random_img = np.random.random((256,256)) Z = np.random.random((256,256)) # Test data # compute the Fast Fourier Transform, finish the code below f = np.fft.fft2(random_img) # convert into Fourier spectrum fshift = np.fft.fftshift(f) # shifting the origin to 0 magnitude_spectrum = 20*np.log(np.abs(fshift)) # display the original image and FFT plt.subplot(121),plt.imshow(random_img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() # diagonal image diag_img = 100*np.identity(255) #To do compute the FFT f = np.fft.fft2(diag_img) # convert into Fourier spectrum fshift = np.fft.fftshift(f) # shifting the origin to 0 magnitude_spectrum2 = np.abs(fshift) # display the original image and FFT plt.subplot(121),plt.imshow(diag_img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum2, cmap = 'gray') plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() """### TODO: Create an image wiith a white suqare of different sizes, display the original iamge and the magnitude of FFT. how does the size of the white square affects its FFT spetrum? What about adding random noise? """ # create an image with a white square in the middle. square = np.zeros((32, 32)) square[10:-10, 10:-10] = 1 # your code here # Todo: Display the original and FFT image # Todo: add random noise to the image and plot the FFT image again. Add random white pixels into the image. np.random.seed(2) x, y = (32*np.random.random((2, 20))).astype(np.int) square[x, y] = 1 """## Exercise 1 Can you manipulate the fourier spectrum and remiove low-frequenty of the image. Can you modify it to remove high frequnecy? """ img = imageio.imread('imageio:astronaut.png',as_gray=True) plt.figure() plt.imshow(img, cmap='gray') f = np.fft.fft2(img) fshift = np.fft.fftshift(f) magnitude_spectrum = 30*np.log(np.abs(fshift)) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() # To do. Read in your own image (e.g. einstein or kandinsky). # #To do. How do you remove the low frequencies Display the DFT magnitutde and the resulting image here # To do. how do you remove the high frequency? Display the DFT magnitutde and the resulting image here ## The following is the code for inverse FFT ## f_isfhit = np.fft.ifftshift(fshift) ## img_back = np. fft.ifft2(f_ishift) ##. img_back = np.abs(img_back) """## Exercise 2. Swich Phase and Magnitude. read in the Cheetha and Zebra image. Download the Cheeta and Zerbra Images. DFT both iamges in Fourier domain and compute magnitude and phase. YOu can compute phase and magnitude as this: magnitude_zebra = 30*np.log(np.abs(fshit) phase_zabera = np.angle(fshift) """ # Todo read in cheeta and zebra images # FFT cheeta image # compute magnitude f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # compute magnitude magnitude_cheetah = 30*np.log(np.abs(fshift)) # compute phase phase_cheetah = np.angle(fshift) # Todo: repeat above with Zebra # Todo: Display the original image and the DFT components # Todo: Please reconstruct the images, please switch phase of zebra and cheeta, and display the final image here """# Exercise 3: Simple Image De-nosing Read in the provided image moonlanding.png, which is heavily contaiinated with periorid noise. In this exercise, we aim to clean up the noise using FFT. 1. load the image with your favourite lib. 2. Use the 2DFFT in numpy.fft and plot the spectrum 3. The specdtrum consists of high and low frequency components. The noise is contained in the high frequency part of the spectrum. So set some of those components to zero. 4. Apply the inverse FFT to see the resulting image. ## Exercise 4: Filtering in FFT. Build a Gaussian filter. Then show you can FFT the filter and the signal, multiply them togheter to low-pass the original image. """ img = imageio.imread('imageio:astronaut.png',as_gray=True) plt.figure() plt.imshow(img, cmap='gray') # image size, square side length, number of squares ncols, nrows = img.shape # FFT the original image ftimage = np.fft.fft2(img) ftimage = np.fft.fftshift(ftimage) plt.imshow(np.log(np.abs(ftimage)),cmap='gray') plt.show() # Build and apply a Gaussian filter and make sure the size is correct sigmax, sigmay = 30, 30 cy, cx = nrows/2, ncols/2 x = np.linspace(0, nrows, nrows) y = np.linspace(0, ncols, ncols) X, Y = np.meshgrid(x, y) plt.imshow(X,cmap='gray') plt.show() plt.imshow(Y,cmap='gray') plt.show() gmask = np.exp(-(((X-cx)/sigmax)**2 + ((Y-cy)/sigmay)**2)) plt.imshow(gmask,cmap='gray') plt.show() # TODO: filtering in Fourier Domain # TDDo : # Finally, take the inverse transform and show the blurred image """## Exercise 5, how do you highpass filter the image in FFT domain? ## step one: create a high pass filter in FD and then perform convolution in FD? # Hint: many ways to create high pass filter 1) sobel, 2) subtract from low-pass # 3) or something like this: #kernel = np.array([[-1, -1, -1], # [-1, 8, -1], # [-1, -1, -1]]) """ # TOdo: use the above image example, finish the high-passing Lect10_exercises/Filtering_FFT_Demo.py """ @author: bxiao Here is a new comment """ import numpy as np import pylab import imageio import matplotlib.pyplot as plt # Take the 2-dimensional DFT and centre the frequencies #img = misc.imread('einstein.png',flatten=1) img = imageio.imread('einstein.png') print(img.shape) # image size, square side length, number of squares ncols, nrows = img.shape # # FFT the original image # ftimage = np.fft.fft2(img) # ftimage = np.fft.fftshift(ftimage) # pylab.imshow(np.log(np.abs(ftimage)),cmap='gray') # pylab.show() f = np.fft.fft2(img) fshift = np.fft.fftshift(f) magnitude_spectrum = 30*np.log(np.abs(fshift)) plt.subplot(121),plt.imshow(img, cmap = 'gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show() # Build and apply a Gaussian filter and make sure the size is correct sigmax, sigmay = 30, 30 cy, cx = nrows/2, ncols/2 x = np.linspace(0, nrows, nrows) y = np.linspace(0, ncols, ncols) X, Y = np.meshgrid(x, y) pylab.imshow(X,cmap='gray') pylab.show() pylab.imshow(Y,cmap='gray') pylab.show() # Gaussian mask gmask = np.exp(-(((X-cx)/sigmax)**2 + ((Y-cy)/sigmay)**2)) pylab.imshow(gmask,cmap='gray') pylab.show() # # here is filtering in Fourier Domain ftimagep = fshift * gmask pylab.imshow(30*np.log(np.abs(ftimagep)),cmap='gray') pylab.show() # Finally, take the inverse transform and show the blurred image imagep = np.fft.ifft2(ftimagep) imageback = np.fft.ifftshift(imagep) pylab.imshow(np.abs(imagep),cmap='gray') pylab.show() # # Exercise, how do you highpass filter the image in FFT domain? # # step one: create a high pass filter in FD and then perform convolution in FD? # # Hint: many ways to create high pass filter 1) sobel, 2) subtract from low-pass # # 3) or something like this: # #kernel = np.array([[-1, -1, -1], # # [-1, 8, -1], # # [-1, -1, -1]]) __MACOSX/Lect10_exercises/._Filtering_FFT_Demo.py Lect10_exercises/.DS_Store __MACOSX/Lect10_exercises/._.DS_Store Lect10_exercises/zebra.png __MACOSX/Lect10_exercises/._zebra.png Lect10_exercises/einstein.png __MACOSX/Lect10_exercises/._einstein.png Lect10_exercises/cheetah.png __MACOSX/Lect10_exercises/._cheetah.png Lect10_exercises/moonlanding.png __MACOSX/Lect10_exercises/._moonlanding.png Lect10_exercises/Kondinsky.jpeg __MACOSX/Lect10_exercises/._Kondinsky.jpeg __MACOSX/._Lect10_exercises
Answered 1 days AfterOct 13, 2021

Answer To: # -*- coding: utf-8 -*- """Copy of Lecture10_Exercises.ipynb Automatically generated by...

Sathishkumar answered on Oct 15 2021
118 Votes
# -*- coding: utf-8 -*-
"""Copy of Lecture10_Exercises.ipynb
Automatically generated by Colaboratory.
##Lecture 10 Take-home Exercises. Discussion is allowed. But you must turn your own independent work!
"""
import matplotlib.pyplot as plt
import numpy as np
from scipy import fftpack
import imageio
"""Exericse 0 Basic Image FFT. """
random_img = np.
random.random((256,256))
Z = np.random.random((256,256)) # Test data
# compute the Fast Fourier Transform, finish the code below
f = np.fft.fft2(random_img) # convert into Fourier spectrum
fshift = np.fft.fftshift(f) # shifting the origin to 0
magnitude_spectrum = 20*np.log(np.abs(fshift))
# display the original image and FFT
plt.subplot(121),plt.imshow(random_img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
# diagonal image
diag_img = 100*np.identity(255)
#To do compute the FFT
f = np.fft.fft2(diag_img) # convert into Fourier spectrum
fshift = np.fft.fftshift(f) # shifting the origin to 0
magnitude_spectrum2 = np.abs(fshift)
# display the original image and FFT
plt.subplot(121),plt.imshow(diag_img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum2, cmap = 'gray')
plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
"""### TODO: Create an image wiith a white suqare of different sizes, display the original iamge and the magnitude of FFT. how does the size of the white square affects its FFT spetrum? What about adding random noise? """
# create an image with a white square in the middle.
square = np.zeros((64, 64))
square[10:-10, 10:-10] = 1
# your code here
# Todo: Display the original and FFT image
# Todo: add random noise to the image and plot the FFT image again. Add random white pixels into the image.
np.random.seed(2)
x, y = (64*np.random.random((2, 20))).astype(np.int)
square[x, y] = 1
random_img=square
f = np.fft.fft2(random_img) # convert into Fourier spectrum
fshift = np.fft.fftshift(f) # shifting the origin to 0
magnitude_spectrum = 20*np.log(np.abs(fshift))
# display the original image and FFT
plt.subplot(121),plt.imshow(random_img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
# diagonal image
diag_img = 100*np.identity(255)
#To do compute the FFT
f = np.fft.fft2(diag_img) # convert into Fourier spectrum
fshift = np.fft.fftshift(f) # shifting the origin to 0
magnitude_spectrum2 = np.abs(fshift)
# display the original image and FFT
plt.subplot(121),plt.imshow(diag_img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum2, cmap = 'gray')
plt.title('Log Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
"""## Exercise 1 Can you manipulate the fourier spectrum and remiove low-frequenty of the image. Can you modify it to remove high frequnecy? """
img = imageio.imread('einstein.png',as_gray=True)
plt.figure()
plt.imshow(img, cmap='gray')
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 30*np.log(np.abs(fshift))
##############################################################
import numpy as np
import numpy.fft as fp
import matplotlib.pyplot as plt
def padwithzeros(vector, pad_width, iaxis, kwargs):
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Submit New Assignment

Copy and Paste Your Assignment Here