Complete the code to compute the 2D Fourier transform of an image array.
import numpy as np image = np.array([[1, 2], [3, 4]]) ft_image = np.fft.[1](image) print(ft_image)
The np.fft.fft2 function computes the 2D Fourier transform of the image.
Complete the code to shift the zero-frequency component to the center of the spectrum.
import numpy as np image = np.array([[1, 2], [3, 4]]) ft_image = np.fft.fft2(image) shifted = np.fft.[1](ft_image) print(shifted)
The np.fft.fftshift function moves the zero-frequency component to the center.
Fix the error in the code to compute the magnitude spectrum of the shifted Fourier transform.
import numpy as np image = np.array([[1, 2], [3, 4]]) ft_image = np.fft.fft2(image) shifted = np.fft.fftshift(ft_image) magnitude = np.abs([1]) print(magnitude)
The magnitude should be computed from the shifted Fourier transform, stored in shifted.
Fill both blanks to create a dictionary comprehension that maps each pixel value to its squared magnitude if the magnitude is greater than 5.
import numpy as np image = np.array([[1, 2], [3, 4]]) magnitude = np.abs(np.fft.fftshift(np.fft.fft2(image))) squared_map = {pixel: pixel[1] for pixel in magnitude.flatten() if pixel [2] 5}
The comprehension squares each pixel value using **2 and filters pixels greater than 5 using >.
Fill all three blanks to create a dictionary comprehension that maps the pixel's row index to its magnitude if the magnitude is less than 10 and the row index is even.
import numpy as np image = np.array([[1, 2], [3, 4]]) magnitude = np.abs(np.fft.fftshift(np.fft.fft2(image))) result = { [1]: [2] for i, row in enumerate(magnitude) if i [3] 2 == 0 and np.all(row < 10) }
The comprehension maps the row index i to the row magnitude row. The condition checks if i % 2 == 0 for even rows.
