欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图像进行傅里叶变换

程序员文章站 2022-05-28 14:26:38
...

参考链接:

https://blog.csdn.net/wumu720123/article/details/89930745

https://blog.csdn.net/can3981132/article/details/52559402

代码:

# -*- coding: utf-8 -*-
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os

def fuliye(img, save_name):
	#快速傅里叶变换算法得到频率分布
	f = np.fft.fft2(img)
	#默认结果中心点位置是在左上角,
	#调用fftshift()函数转移到中间位置
	fshift = np.fft.fftshift(f)       
	#fft结果是复数, 其绝对值结果是振幅
	fimg = np.log(np.abs(fshift))


	# # 1.高通滤波——把中心的低频信息都去掉,保留周围的高频信息
	# area=5
	# rows, cols = img.shape
	# crow, ccol = int(rows/2), int(cols/2)
	# fshift[crow-area:crow+area, ccol-area:ccol+area] = 0


	# 2.低通滤波——保留中心的低频信息,把周围的高频信息都去掉
	area=5
	rows, cols = img.shape
	crow,ccol = int(rows/2), int(cols/2)
	mask = np.zeros((rows, cols), np.uint8)
	mask[crow-area:crow+area, ccol-area:ccol+area] = 1
	fshift = fshift * mask

	#傅里叶逆变换
	ishift = np.fft.ifftshift(fshift)
	iimg = np.fft.ifft2(ishift)
	iimg = np.abs(iimg)

	cv2.imwrite(save_name, iimg)

path="data/data2/eyebrow/"
# path = "E:/data/ng/data/eyebrow/"
save="data/fuliye/test1/eyebrow3/"
for file in os.listdir(path):
	save_name=save+file
	file_name=path+file
	img=cv2.imread(file_name,0)
	# img=cv2.resize(img, (500,500))
	
	fuliye(img, save_name)
	print(file)