python opencv dft

import cv2 
import numpy as np
import matplotlib.pyplot as plt
img =cv2.imread('E:/python/sunflower.png',0) 
f=np.fft.fft2(img)
fshift=np.fft.fftshift(f)
#db图
magnitude_spectrum=20*np.log(np.abs(fshift))
plt.subplot(121),plt.imshow(img,'gray'),plt.title('input'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,'gray'),plt.title('magnitude_spectrum'),plt.xticks([]),plt.yticks([])
plt.show

python opencv dft_第1张图片
中心区域为白色说明低频分量较多

rows,cols=img.shape
crows,ccols=np.int8(rows/2),np.int8(cols/2)
#矩形滤波
fshift[crows-50:crows+50,ccols-40:ccols+40]=0
f_ishift=np.fft.ifftshift(fshift)
f_idft=np.fft.ifft2(f_ishift)
image_back=np.abs(f_idft)
plt.subplot(121),plt.imshow(img,'gray'),plt.title('input'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(image_back,'gray'),plt.title('image_back'),plt.xticks([]),plt.yticks([])
plt.show

python opencv dft_第2张图片
矩形窗口滤波之后复原图与梯度,边缘图类似,说明边缘就是高频分量,灰度变化剧烈,一般矩形窗口滤波容易产生振铃效应,一般高斯滤波
opencvdft

import cv2 
import numpy as np
import matplotlib.pyplot as plt
img =cv2.imread('E:/python/sunflower.png',0) 
f=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
fshift=np.fft.fftshift(f)
#db图
magnitude_spectrum=20*np.log(cv2.magnitude(fshift[:,:,0],fshift[:,:,1]))
plt.subplot(121),plt.imshow(img,'gray'),plt.title('input'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,'gray'),plt.title('magnitude_spectrum'),plt.xticks([]),plt.yticks([])
plt.show

python opencv dft_第3张图片

rows,cols=img.shape
crows,ccols=np.int8(rows/2),np.int8(cols/2)
#矩形滤波
mask=np.zeros((rows,cols,2),np.uint8)
mask[crows-50:crows+50,ccols-40:ccols+40]=1
fshift=fshift*mask
f_ishift=np.fft.ifftshift(fshift)
f_idft=cv2.idft(f_ishift)
image_back=cv2.magnitude(f_idft[:,:,0],f_idft[:,:,1])
plt.subplot(121),plt.imshow(img,'gray'),plt.title('input'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(image_back,'gray'),plt.title('image_back'),plt.xticks([]),plt.yticks([])
plt.show

python opencv dft_第4张图片

#性能优化
img =cv2.imread('E:/python/sunflower.png',0) 
rows,cols=img.shape
print(rows,cols)

nrows=cv2.getOptimalDFTSize(rows)
ncols=cv2.getOptimalDFTSize(cols)
print(nrows,ncols)

#测试是否优化
nimg=np.zeros((nrows,ncols),np.uint8)
nimg[:rows,:cols]=img                                                                                                                                                                                                                           

%timeit fft1=np.fft.fft2(img)
710 µs ± 16.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit fft2=np.fft.fft2(nimg)
589 µs ± 26.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit f=cv2.dft(np.float32(img),flags=cv2.DFT_COMPLEX_OUTPUT)
637 µs ± 2.94 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit f=cv2.dft(np.float32(nimg),flags=cv2.DFT_COMPLEX_OUTPUT)
441 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

import cv2 
import numpy as np
import matplotlib.pyplot as plt
mean_filter=np.ones((3,3))
x=cv2.getGaussianKernel(5,10)
gaussian=x*x.T

scharr=np.array([[-3,0,3],
                 [-10,0,10],
                 [-3,0,3]])
sobel_x=np.array([[-1,0,1],
                 [-2,0,2],
                 [-1,0,1]])
sobel_y=np.array([[-1,-2,-1],
                 [0,0,0],
                 [1,2,1]])
laplacian=np.array([[0,1,0],
                 [0,-4,0],
                 [0,1,0]])

filters=[mean_filter,gaussian,scharr,sobel_x,sobel_y,laplacian]
filter_name=['mean_filter','gaussian','scharr','sobel_x','sobel_y','laplacian']

fft_filters=[np.fft.fft2(x,(16,16)) for x in filters]
fftshift=[np.fft.fftshift(x) for x in fft_filters]
mag_spectrum=[np.log(np.abs(x)+1) for x in fftshift]

for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(mag_spectrum[i],'gray')
    plt.title(filter_name[i]),plt.xticks([]),plt.yticks([])
    
plt.show

python opencv dft_第5张图片

你可能感兴趣的:(python)