python理想低通滤波、巴特沃斯低通滤波、高斯低通滤波实现
代码代码如下(示例):import numpy as npimport cv2 as cvimage = cv.imread('2.PNG')# print(image.shape)image = cv.cvtColor(image,cv.COLOR_BGR2GRAY)#图像灰度图# print(image.shape)def frequency_filter(image ,filter):"""
·
代码
代码如下(示例):
import numpy as np
import cv2 as cv
image = cv.imread('2.PNG')
# print(image.shape)
image = cv.cvtColor(image,cv.COLOR_BGR2GRAY)#图像灰度图
# print(image.shape)
def frequency_filter(image ,filter):
"""
:param image:
:param filter: 频域变换函数
:return:
"""
fftImg = np.fft.fft2(image) #对图像进行傅里叶变换
fftImgShift = np.fft.fftshift(fftImg)#傅里叶变换后坐标移动到图像中心
handle_fftImgShift1 = fftImgShift*filter#对傅里叶变换后的图像进行频域变换
handle_fftImgShift2 = np.fft.ifftshift(handle_fftImgShift1)
handle_fftImgShift3 = np.fft.ifft2(handle_fftImgShift2)
handle_fftImgShift4 = np.real(handle_fftImgShift3)#傅里叶反变换后取频域
return np.uint8(handle_fftImgShift4)
def ILPF(image,d0,n):#理想低通滤波器
H = np.empty_like(image,dtype=float)
M,N = image.shape
mid_x = int(M/2)
mid_y = int(N/2)
for y in range(0, M):
for x in range(0,N):
d = np.sqrt((x - mid_x) ** 2 + (y - mid_y) ** 2)
if d <= d0:
H[y, x] = 1**n
else:
H[y, x] = 0**n
return H
def BLPF(image,d0,n):#巴特沃斯低通滤波器
H = np.empty_like(image,float)
M,N = image.shape
mid_x = int(M/2)
mid_y = int(N/2)
for y in range(0, M):
for x in range(0, N):
d = np.sqrt((x - mid_x) ** 2 + (y - mid_y) ** 2)
H[y,x] = 1/(1+(d/d0)**(n))
return H
def GLPF(image,d0,n):#高斯低通滤波器
H = np.empty_like(image,float)
M, N = image.shape
mid_x = M/2
mid_y = N/2
for x in range(0, M):
for y in range(0, N):
d = np.sqrt((x - mid_x)**2 + (y - mid_y) ** 2)
H[x, y] = np.exp(-d**n/(2*d0**n))
return H
def image_arrage(image,W,H,n,d0,step,filter):#图像绘制
"""
:param image: 原始图像
:param W: 每列图像个数
:param H: 每行图像个数
:param n: 阶数
:param d0: 初始截止频率
:param step: 截止频率步距
:return: None
"""
imageHstack = {}
for i in range(H):
hStack = 'H'+str(i)
flag = 0
for i in range(W):
if flag ==0:
imageHstack[hStack] = frequency_filter(image,filter(image,d0,n))
d0 += step
flag +=1
else:
imageHstack[hStack] = np.hstack((imageHstack[hStack], frequency_filter(image, filter(image, d0, n))))
d0 += step
flag += 1
flag = 0
for i in imageHstack.values():
if flag == 0:
imageStack = i
flag += 1
else:
imageStack = np.vstack((imageStack,i))
# print(imageStack)
return imageStack
# cv.namedWindow('Img')
# cv.resizeWindow('Img',(20,20))
# cv.imshow('Img',frequency_filter(image,ILPF(image,60)))
# cv.namedWindow('Img2')
# cv.resizeWindow('Img2',(20,20))
# cv.imshow('Img2',frequency_filter(image,BLPF(image,40,n=2)))
# cv.namedWindow('Img3')
# cv.resizeWindow('Img3',(20,20))
# imghstack = np.hstack((imgroi, imgwomen))
# # 垂直组合
# imgvstack = np.vstack((imgroi, imgwomen))
cv.imshow('Img3',image_arrage(image,4,2,2,30,20,BLPF))
# cv.resizeWindow('Img3',(20,20))
# cv.imshow('Img3',frequency_filter(image,GLPF(image,80,n=2)))
cv.waitKey()
高斯低通滤波结果
更多推荐
已为社区贡献1条内容
所有评论(0)