<jupyter_start><jupyter_text>Review of Computer Vision Lecture 1 / concepts: 1.1 1D sine waves:* What parameters define a sine wave?* What is a "Fourier transform" in words? 1.2 1D reciprocal space:* What is the difference between "analog" and "digital" signal?* What is the fundamental frequency? A harmonic? Nyquist frequency?* What is reciprocal space? What are the axis (for 1D)?* What does a plot of the FFT of a function in reciprocal space tell you?  Computer Vision Lecture 2 2.1.0) 2D Waves & Images Create a complicated 2D "image" with only 2 sine waves<jupyter_code>Ax = 2 # x-axis amplitude
Ay = 1 # y-axis amplitude

px = 24 # x-axis phase
py = 0 # y-axis phase

fx = 3 # x-axis frequency of the signal
fy = 1 # y-axis frequency of the signal

f_s = 100 # sample rate / "resolution"

x = np.arange(0,(f_s)) # points for x-axis
y = np.arange(0,(f_s)) # points for y-axis
X,Y = np.meshgrid(x,y)

f_xy = Ax*np.sin(2*np.pi*fx*(X/f_s) + px*np.pi/180) + Ay*np.sin(2*np.pi*fy*(Y/f_s) + py*np.pi/180)

plt.contour(X,Y,f_xy, 990)
plt.show()<jupyter_output><empty_output><jupyter_text>Take-away: Any 2D image can be described by a sum of sine-waves.----  2.2.0) 2D-Transforms  Assume some grayscale 2D-image of 10-by-10 pixels|   |   |   |   |   |   |   |   |   |   ||---|---|---|---|---|---|---|---|---|---|| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 || 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 || 0 | 0 | 0 | 4 | 3 | 4 | 8 | 0 | 0 | 0 || 0 | 0 | 3 | 9 | 9 | 9 | 9 | 3 | 0 | 0 || 0 | 0 | 5 | 9 | 9 | 9 | 9 | 3 | 0 | 0 || 0 | 0 | 6 | 9 | 9 | 9 | 9 | 2 | 0 | 0 || 0 | 0 | 7 | 9 | 9 | 9 | 9 | 1 | 0 | 0 || 0 | 0 | 2 | 5 | 7 | 8 | 6 | 0 | 0 | 0 || 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 || 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Same image in reciprocal space (A_hk)|    |       |       |      |      |      |      |      |      |      |      |       |      ||----|-------|-------|------|------|------|------|------|------|------|------|-------|------|| 5  | A_05  | P_05  |      |      |      |      |      |      |      |      | A_55  | P_55 || 4  |       |       |      |      |      |      |      |      |      |      |       |      || 3  |       |       |      |      |      |      |      |      |      |      |       |      || 2  |       |       |      |      |      |      |      |      |      |      |       |      || 1  | A_01  |       |      |      |      |      |      |      | A_41 |      |       |      || 0  | A_00  | P_00  | A_10 | P_10 | A_20 | P_20 | A_30 | P_30 | A_40 | P_40 |       |      || -1 |       |       |      |      |      |      |      |      |      |      |       |      || -2 |       |       |      |      |      |      |      |      |      |      |       |      || -3 |       |       |      |      |      |      |      |      |      |      |       |      || -4 |       |       |      |      |      |      |      |      |      |      |       |      || -5 | A_0-5 | P_0-5 |      |      |      |      |      |      |      |      | A_5-5 | P5-5 ||    | 0     |       | 1    |      | 2    |      | 3    |      | 4    |      | 5     |      |-----  Given these real space 2D sine-wave 'images' plot them in reciprocal space ![image.png](attachment:image.png) ---- Now, on to real images<jupyter_code>url ='https://i.stack.imgur.com/Je4Tk.png' # url to grayscale image
img = io.imread(url) # read image from url

f = np.fft.fft2(img) # take 2D-fft of grayscale image
fshift = np.fft.fftshift(f) # shift the DC component to be in the center of the power spectrum
power_spectrum = 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(power_spectrum, cmap = 'gray')
plt.title('Power Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
url ='https://i.stack.imgur.com/Je4Tk.png' # url to grayscale image
img = io.imread(url) # read image from url


f = np.fft.fft2(img) # fft to convert the image to freq domain
fshift = np.fft.fftshift(f) # shift the center

rows, cols = img.shape
crow,ccol = round(rows/2) , round(cols/2)

print(fshift.shape)

# remove the low frequencies by masking with a rectangular window of size 60x60
# High Pass Filter (HPF)
fshift[crow-30:crow+30, ccol-30:ccol+30] = 0

power_spectrum = np.log(np.abs(fshift))

f_ishift = np.fft.ifftshift(fshift) # shift back (we shifted the center before)
img_back = np.fft.ifft2(f_ishift) # inverse fft to get the image back 
img_back = np.abs(img_back)

plt.subplot(131),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])

plt.subplot(132),plt.imshow(power_spectrum, cmap = 'gray')
plt.title('Power Spectrum'), plt.xticks([]), plt.yticks([])

plt.subplot(133),plt.imshow(img_back, cmap = 'gray')
plt.title('filtered image'), plt.xticks([]), plt.yticks([])

plt.show()<jupyter_output>(512, 512)
{'fraction_non_alphanumeric': 0.18525441329179648, 'fraction_numerical': 0.05150571131879543, 'mean_word_length': 1.6607734806629835, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '585868', 'license': 'MIT', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1792, 'n_words': 473, 'path': 'LAM_ML_Computer_Viz_2.ipynb', 'repo_name': 'scr9035/ML_lectures', 'size': '4815'}