import numpy as np from scipy.misc import imread import matplotlib.pyplot as plt # Oppgave a f = imread('car.png', flatten=True) (N,M) = f.shape F = np.fft.fft2(f) F_r = np.real(F) # Real-delen (cosinusbilde-bidragene) F_i = np.imag(F) # Imagin?r-delen (sinusbilde-bidragene) amp_shifted = np.fft.fftshift(np.log10(abs(F))) phase_shifted = np.fft.fftshift(np.angle(F)) fig = plt.figure() fig.add_subplot(1,2,1) plt.imshow(amp_shifted, vmin=0) plt.colorbar() plt.title('Amplitude spectrum [log10]') fig.add_subplot(1,2,2) plt.imshow(phase_shifted/np.pi*180) plt.colorbar() plt.title('Phase [deg]') plt.show() ## Oppgave b print(F[0,0], sum(sum(f))) # Indreprodukt mellom basis [[1 1 ... 1 1], ..., [1 1 ... 1 1]] ## Oppgave c def sinusBilde(u, v, N, M): bilde = np.zeros((N, M)) for i in range(N): for j in range(M): bilde[i, j] = np.sin( -2*np.pi* ((u*i/np.float(N)) + (v*j/np.float(M))) ) return bilde u = 2 v = 7 sinimage = sinusBilde(u, v, N, N) plt.figure() plt.imshow(sinimage, cmap="gray") plt.colorbar() plt.title('u = {}, v = {}'.format(u, v)) plt.show() ## Oppgave d indreprodukt = np.sum(sinimage * f) print(indreprodukt, F_i[u, v]) # Oppgave e def cosinusBilde(u, v, N, M): bilde = np.zeros((N, M)) for i in range(N): for j in range(M): bilde[i, j] = np.cos( -2*np.pi* ((u*i/np.float(N)) + (v*j/np.float(M))) ) return bilde cosimage = cosinusBilde(u, v, N, N) indreprodukt = np.sum(cosimage * f) print(indreprodukt, F_r[u, v])