# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
import cv2
img = cv.imread('C:/Users/Administrator/Desktop/41.png',0)
f = np.fft.fft2(img)
#F(u,v)频域变换
fshift = np.fft.fftshift(f)
#将低频移动到图像的中心
fshift_ima=np.abs(fshift)
#A(u,v)=|F(u,v)|求频幅
magnitude_spectrum = 20*np.log(np.abs(fshift))
#拉伸小取对数L(u,v)=log(A(u,v))
laplacian_image=cv2.Laplacian(img,cv2.CV_64F)
plt.subplot(221),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('im_copy_max laplacian_image'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(laplacian_image, cmap = 'gray')
plt.title('im_copy_max laplacian_image'), plt.xticks([]), plt.yticks([])
plt.show()
###########想给它一个模板MASK滤波
#####################################################################
rows, cols = img.shape
crow,ccol = rows//2 , cols//2
im_copy_max=np.zeros([rows,cols],dtype=bool)
def max_filte(x,y,step):
sum_s=[]
for k in range(-int(step/2),int(step/2)+1):
for m in range(-int(step/2),int(step/2)+1):
sum_s.append(magnitude_spectrum[x+k][y+m])
sum_s.sort()
if(max(sum_s)>140):
value=1
else:
value=0
return value
# return sum_s[(int(step*step/2)+1)]
def test(Step):
for i in range(int(Step/2),img.shape[0]-int(Step/2)):
for j in range(int(Step/2),img.shape[1]-int(Step/2)):
im_copy_max[i][j]=max_filte(i,j,Step)
test(3)
plt.subplot(221),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(222),plt.imshow(magnitude_spectrum, cmap = 'gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(223),plt.imshow(im_copy_max, cmap = 'gray')
plt.title('im_copy_max Spectrum'), plt.xticks([]), plt.yticks([])
plt.subplot(224),plt.imshow(laplacian_image, cmap = 'gray')
plt.title('im_copy_max laplacian_image'), plt.xticks([]), plt.yticks([])
plt.show()
img_bad = cv.imread('C:/Users/Administrator/Desktop/00.jpg',0)
rows, cols
img_bad = cv2.resize(img_bad,(cols,rows),interpolation=cv2.INTER_CUBIC)
#im_copy_max=im_copy_max.reshape((rows,cols))
f = np.fft.fft2(img_bad)
fshift = np.fft.fftshift(f)
fshift=fshift*im_copy_max
#平移逆变换
f_ishift = np.fft.ifftshift(fshift)
#傅里叶反变换
img_back = np.fft.ifft2(f_ishift)
# 取绝对值
img_back = np.abs(img_back)
fshift_image=np.abs(fshift)
ret,thresh1=cv2.threshold(img_bad,180,255,cv2.THRESH_BINARY)
plt.subplot(222),plt.imshow(img_back, cmap = 'gray')
plt.title('Input img_back'), plt.xticks([]), plt.yticks([])
plt.subplot(221),plt.imshow(img_bad, cmap = 'gray')
plt.title('Input img_bad'), plt.xticks([]), plt.yticks([])
plt.subplot(223),plt.imshow(thresh1, cmap = 'gray')
plt.title('Input thresh1'), plt.xticks([]), plt.yticks([])
plt.show()
#小波去噪
import pywt
#小波分解
##返回 (cA, (cH, cV, cD)), 分别是逼近、水平细节、垂直细节和对角线细节
#hard data中绝对值小于阈值2的替换为6,大于2的不替换
img_back_new=pywt.threshold(img_back,150, "hard",0)
#ret,thresh1=cv2.threshold(img_bad,180,255,cv2.THRESH_BINARY)
plt.subplot(121),plt.imshow(img_back_new, cmap = 'gray')
plt.show()
coeffs = pywt.dwt2(img_back, "haar")
cA, (cH, cV, cD) = coeffs
print (cA)
# 2D一阶重构idwt2
idwt2(coeffs, wavelet, mode="sym")
#
# magnitude_spectrum = 20*np.log(np.abs(fshift))
#
#
#
#
# rows, cols = img.shape
# crow,ccol = rows//2 , cols//2
# #设计一个高通滤波器对应0, 低频对应1
# # fshift[crow-30:crow+30, ccol-30:ccol+30] = 0
#
#
#
#
#
# im_copy_max
# im_10=np.zeros([rows,cols],dtype=float)
# for i in range(0,rows):
# for j in range(0,cols):
# im_10[i][j]=10**(im_copy_max[i][j]/20)
#
#
# Y=im_10
#
# im_copy_max=np.zeros([rows,cols],dtype=float)
# # im_copy_max=fshift
# def m_filter(x, y, step):
# sum_s=[]
# for k in range(-int(step/2),int(step/2)+1):
# for m in range(-int(step/2),int(step/2)+1):
# sum_s.append(magnitude_spectrum[x+k][y+m])
# sum_s.sort()
# return max(sum_s)
# def test(Step):
# for i in range(int(Step/2),img.shape[0]-int(Step/2)):
# for j in range(int(Step/2),img.shape[1]-int(Step/2)):
# im_copy_max[i][j]=m_filter(i,j,Step)
#
#
#
# test(3)
#
# # magnitude_spectrum = 20*np.log(np.abs(fshift))
# Y= np.power(10, im_copy_max/20)
# fshift=Y
#
#
# ##########################################################
#
#
#
# #保留高频信号
# # MASK1=np.ones([rows,cols],dtype=bool)
# # MASK1[crow-20:crow+20, ccol-20:ccol+20] = 0
# # #保留低频信号
# # MASK2=np.zeros([rows,cols],dtype=bool)
# # MASK2[crow-5:crow+5, ccol-5:ccol+5] = 1
# #
# # MASK=MASK1+MASK2
# # fshift=fshift*MASK1
#
#
#
# # #保留低频信号
# # MASK2=np.zeros([rows,cols],dtype=bool)
# # MASK2[crow-5:crow+5, ccol-5:ccol+5] = 1
# # fshift=fshift*MASK2
#
#
#
# #平移逆变换
# f_ishift = np.fft.ifftshift(fshift)
# #傅里叶反变换
# img_back = np.fft.ifft2(f_ishift)
# # 取绝对值
# img_back = np.abs(img_back)
# fshift_image=np.abs(fshift)
#
#
# plt.subplot(322),plt.imshow(fshift_image, cmap = 'gray')
# plt.title('Input fshift_image'), plt.xticks([]), plt.yticks([])
#
# plt.subplot(323),plt.imshow(img, cmap = 'gray')
# plt.title('Input Image'), plt.xticks([]), plt.yticks([])
#
# plt.subplot(324),plt.imshow(img_back, cmap = 'gray')
# plt.title('Image after HPF'), plt.xticks([]), plt.yticks([])
#
# plt.subplot(325),plt.imshow(img_back)
# plt.title('Result in JET'), plt.xticks([]), plt.yticks([])
# plt.show()
#
# # # fft in cv2
# # dft = cv2.dft(np.float32(img),flags = cv2.DFT_COMPLEX_OUTPUT)
# # dft_shift = np.fft.fftshift(dft)
# # magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))
# # plt.subplot(121),plt.imshow(img, cmap = 'gray')
# # plt.title('Input Image'), plt.xticks([]), plt.yticks([])
# # plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray')
# # plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
# # plt.show()