'''
Implementation of the secend class's image process methods, which contains:
1. furior transform
2. walsh transform
3. DCT transform
'''
import cv2
import numpy as np
import matplotlib.pyplot as plt
# load test img and transform it to grey scale
image = cv2.imread('test.jpeg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (512, 512))
print(np.shape(image))
plt.subplot(221), plt.imshow(image, cmap='gray')
plt.title('origin'), plt.xticks([]), plt.yticks([])
# 1. furior transform
# do dft and show the magnitude spectrum
dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)
magnitude_spectrum1 = 20*np.log(cv2.magnitude(dft[:, :, 0], dft[:, :, 1]))
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum2 = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
#plt.subplot(121), plt.imshow(magnitude_spectrum1, cmap='gray')
#plt.title('dft'), plt.xticks([]), plt.yticks([])
plt.subplot(222), plt.imshow(magnitude_spectrum2, cmap='gray')
plt.title('dft_shift'), plt.xticks([]), plt.yticks([])
'''
# simple averaging filter without scaling parameter
mean_filter = np.ones((3,3))
# creating a guassian filter
x = cv2.getGaussianKernel(5,10)
gaussian = x*x.T
# different edge detecting filters
# scharr in x-direction
scharr = np.array([[-3, 0, 3],
[-10,0,10],
[-3, 0, 3]])
# sobel in x direction
sobel_x= np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# sobel in y direction
sobel_y= np.array([[-1,-2,-1],
[0, 0, 0],
[1, 2, 1]])
# laplacian ( high pass )
laplacian=np.array([[0, 1, 0],
[1,-4, 1],
[0, 1, 0]])
filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr]
filter_name = ['mean_filter', 'gaussian','laplacian', 'sobel_x', \
'sobel_y', 'scharr_x']
fft_filters = [np.fft.fft2(x) for x in filters]
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
mag_spectrum = [np.log(np.abs(z)+1) for z in fft_shift]
for i in range(6):
plt.subplot(2, 3, i+1), plt.imshow(mag_spectrum[i], cmap = 'gray')
plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])
plt.show()
'''
# 2. walsh transform
# something wrong with this part
def joint(h):
up = np.hstack((h, h))
down = np.hstack((h, -h))
H = np.vstack((up, down))
return H
def gen_walsh_matrix(size=512):
H = np.array([[1,1],[1,-1]])
iter = int(np.log2(size))-1
for i in range(iter):
H = joint(H)
return H
size = np.shape(image)[0]
H = gen_walsh_matrix(512)
print(image)
image_wt = H.T.dot(image).dot(H)/ 9
print(image_wt)
plt.subplot(223), plt.imshow(image_wt, cmap='gray')
plt.title('walsh transform'), plt.xticks([]), plt.yticks([])
# 3. dct with opencv
img_dct = cv2.dct(image.astype(np.float32))
img_dct_log = np.log(abs(img_dct))
plt.subplot(224), plt.imshow(img_dct_log, cmap='gray')
plt.title('dct transform'), plt.xticks([]), plt.yticks([])
plt.show()