Python+OpenCV:傅里叶变换(Fourier Transform)
Python+OpenCV:傅里叶变换(Fourier Transform)
####################################################################################################
# 图像傅里叶变换(Image Fourier Transform)
def lmc_cv_image_fourier_transform():"""函数功能: 图像傅里叶变换(Image Fourier Transform)。"""# Fourier Transform in Numpyimage = lmc_cv.imread('D:/99-Research/Python/Image/messi.jpg', flags=lmc_cv.IMREAD_GRAYSCALE)fft_image = np.fft.fft2(image)fft_shift_image = np.fft.fftshift(fft_image)magnitude_spectrum = 20 * np.log(np.abs(fft_shift_image))pyplot.figure('Fourier Transform in Numpy')pyplot.subplot(1, 2, 1)pyplot.imshow(image, cmap='gray')pyplot.title('Original Image')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(1, 2, 2)pyplot.imshow(magnitude_spectrum, cmap='gray')pyplot.title('Magnitude Spectrum')pyplot.xticks([])pyplot.yticks([])pyplot.show()# high pass filtering and reconstruct the image in Numpyrows, cols = image.shapecrow, ccol = rows // 2, cols // 2fft_shift_image[crow - 30:crow + 31, ccol - 30:ccol + 31] = 0fft_ishift = np.fft.ifftshift(fft_shift_image)inverse_image_complex = np.fft.ifft2(fft_ishift)inverse_image_real = np.real(inverse_image_complex)pyplot.figure('HPF Fourier Transform in Numpy')pyplot.subplot(1, 3, 1)pyplot.imshow(image, cmap='gray')pyplot.title('Original Image')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(1, 3, 2)pyplot.imshow(inverse_image_real, cmap='gray')pyplot.title('Image after HPF')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(1, 3, 3)pyplot.imshow(inverse_image_real)pyplot.title('High Pass Filtering Image')pyplot.xticks([])pyplot.yticks([])pyplot.show()# Fourier Transform in OpenCVdft_image = lmc_cv.dft(np.float32(image), flags=lmc_cv.DFT_COMPLEX_OUTPUT)dft_shift_image = np.fft.fftshift(dft_image)magnitude_spectrum = 20 * np.log(lmc_cv.magnitude(dft_shift_image[:, :, 0], dft_shift_image[:, :, 1]))pyplot.figure('Fourier Transform in OpenCV')pyplot.subplot(1, 2, 1)pyplot.imshow(image, cmap='gray')pyplot.title('Original Image')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(1, 2, 2)pyplot.imshow(magnitude_spectrum, cmap='gray')pyplot.title('Magnitude Spectrum')pyplot.xticks([])pyplot.yticks([])pyplot.show()# low pass filtering and reconstruct the image in OpenCVrows, cols = image.shapecrow, ccol = rows // 2, cols // 2# create a mask first, center square is 1, remaining all zerosmask = np.zeros((rows, cols, 2), np.uint8)mask[crow - 30:crow + 30, ccol - 30:ccol + 30] = 1# apply mask and inverse DFTfft_shift_image = dft_shift_image * maskfft_ishift = np.fft.ifftshift(fft_shift_image)inverse_image = lmc_cv.idft(fft_ishift)inverse_image = lmc_cv.magnitude(inverse_image[:, :, 0], inverse_image[:, :, 1])pyplot.figure('LPF Fourier Transform in OpenCV')pyplot.subplot(1, 2, 1)pyplot.imshow(image, cmap='gray')pyplot.title('Original Image')pyplot.xticks([])pyplot.yticks([])pyplot.subplot(1, 2, 2)pyplot.imshow(inverse_image, cmap='gray')pyplot.title('Low Pass Filtering Image')pyplot.xticks([])pyplot.yticks([])pyplot.show()# Performance Optimization of DFTprint("{} {}".format(rows, cols))nrows = lmc_cv.getOptimalDFTSize(rows)ncols = lmc_cv.getOptimalDFTSize(cols)print("{} {}".format(nrows, ncols))# pad zeros method 1new_image = np.zeros((nrows, ncols))new_image[:rows, :cols] = image# pad zeros method 2right = ncols - colsbottom = nrows - rowsbordertype = lmc_cv.BORDER_CONSTANT # just to avoid line breakup in PDF filenew_image = lmc_cv.copyMakeBorder(image, 0, bottom, 0, right, bordertype, value=0)# calculate the DFT performance comparison of Numpy functionnumber = 1000start_time = time.time()for i in range(number):np.fft.fft2(image)print(f"{number} loops, best of {(time.time() - start_time) / number} ms per loop")start_time = time.time()for i in range(number):np.fft.fft2(image, [nrows, ncols])print(f"{number} loops, best of {(time.time() - start_time) / number} ms per loop")# calculate the DFT performance comparison of OpenCV functionstart_time = time.time()for i in range(number):lmc_cv.dft(np.float32(image), flags=lmc_cv.DFT_COMPLEX_OUTPUT)print(f"{number} loops, best of {(time.time() - start_time) / number} ms per loop")start_time = time.time()for i in range(number):lmc_cv.dft(np.float32(new_image), flags=lmc_cv.DFT_COMPLEX_OUTPUT)print(f"{number} loops, best of {(time.time() - start_time) / number} ms per loop")# High Pass Filter or Low Pass Filter# simple averaging filter without scaling parametermean_filter = np.ones((3, 3))# creating a gaussian filterx = lmc_cv.getGaussianKernel(5, 10)gaussian = x * x.T# different edge detecting filters# scharr in x-directionscharr = np.array([[-3, 0, 3],[-10, 0, 10],[-3, 0, 3]])# sobel in x directionsobel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])# sobel in y directionsobel_y = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]])# laplacianlaplacian = 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]magnitude_spectrum = [20 * np.log(np.abs(z) + 1.00) for z in fft_shift]pyplot.figure('High Pass Filter or Low Pass Filter')for i in range(6):pyplot.subplot(2, 3, i + 1)pyplot.imshow(magnitude_spectrum[i], cmap='gray')pyplot.title(filter_name[i])pyplot.xticks([])pyplot.yticks([])pyplot.show()# 根据用户输入保存图像if ord("q") == (lmc_cv.waitKey(0) & 0xFF):# 销毁窗口pyplot.close('all')return
Performance Optimization of DFT
581 739
600 750
calculate the DFT performance comparison of Numpy function:
1000 loops, best of 0.057367880821228026 ms per loop
1000 loops, best of 0.022089494943618775 ms per loop
calculate the DFT performance comparison of OpenCV function:
1000 loops, best of 0.010666451215744019 ms per loop
1000 loops, best of 0.005198104381561279 ms per loop
Python+OpenCV:傅里叶变换(Fourier Transform)相关推荐
- 数字图像的傅里叶变换(Fourier Transform)及其展示: 频率中心化
本文通过理论和MATLAB实际例子验证如下结论: 1. 大部分图片的有效信息集中在低频部分: 2. 图像傅里叶变换后低频在四周.高频在中心: 3. 图像经过傅里叶变换.频率中心化后能够容易地复原: 从 ...
- 使用 scipy.fft 进行Fourier Transform:Python 信号处理
摘要:Fourier transform 是一个强大的概念,用于各种领域,从纯数学到音频工程甚至金融. 本文分享自华为云社区<使用 scipy.fft 进行Fourier Transform:P ...
- Python+OpenCV:尺度不变特征变换 (SIFT, Scale-Invariant Feature Transform)
Python+OpenCV:尺度不变特征变换 (SIFT, Scale-Invariant Feature Transform) 理论 A corner in a small image within ...
- Python+OpenCV:Hough圆检测(Hough Circle Transform)
Python+OpenCV:Hough圆检测(Hough Circle Transform) ##################################################### ...
- Python+OpenCV:Hough直线检测(Hough Line Transform)
Python+OpenCV:Hough直线检测(Hough Line Transform) 理论 A line can be represented as or in a parametric fo ...
- 短时傅里叶变换原理及其MATLAB实现(Short Time Fourier Transform,STFT)
短时傅里叶变换原理及其MATLAB实现(Short Time Fourier Transform,STFT) 1.短时Fourier变换原理(STFT原理) 信号x(t)短时Fourier变换定义为: ...
- 【OI向】快速傅里叶变换(Fast Fourier Transform)
[OI向]快速傅里叶变换(Fast Fourier Transform) 转存于本人博客园 地址 FFT的作用 在学习一项算法之前,我们总该关心这个算法究竟是为了干什么. (以下应用只针对OI ...
- 傅里叶变换轮廓术Fourier Transform Profilometry(FTP)
小钟第一次写博客,希望把读研期间学习的结构光三维重建的知识还有实验记录下来,并且分享出去,一是希望能帮助跟我研一的时候一样的小伙伴,当时痛苦地读大量文献,从仿真到实验,一个人在奋斗捣鼓,迷茫的时候超级 ...
- Python+OpenCV实用案例应用教程:基于OpenCV的图像处理
在进行图像处理时,你迟早会发现需要转换图像--一般通过应 用艺术滤镜.推断某些部分.混合两幅图像,或者任何你能够想到的 方法完成.本章将介绍一些可以转换图像的技术.最后,你还能够执 行图像锐化.标记主 ...
最新文章
- python写一个文件下载器_Python3使用TCP编写一个简易的文件下载器
- 专题之六:ckk拉链厂信息化实施记
- React进阶—性能优化
- 开发、测试与QA的区别以及其他
- immutable.js笔记
- python3 random模块操作
- 小说的逻辑与反逻辑_以理性的数学逻辑构筑推理小说
- js中eval 详解
- python怎么绘制坐标图像_如何使用给定的坐标和python opencv在图像中绘制点?
- iOS上应用Static Framework
- Unix/Linux编程:Unix文件系统回顾
- ie11 java提示升级,解决IE11安装升级失败和在安装前需要更新的问题
- java面试①整体流程
- (专升本)PowerPoint(插入超链接和动作)
- bat怎么发起网络请求_批处理命令教程之网络入侵
- 华东师范计算机科学与技术考研难吗,华东师范大学计算机考研难不难
- 【鸿蒙应用开发】-入门
- 天涯上令人肝肠寸断的100个签名!
- 使用pandas sns绘图
- 解决远程windows server12桌面复制文件大于2G出错问题