图像处理:fourier变换,walsh变换,dct 变换

'''
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()







































 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值