低通滤波
import numpy as np
import cv2 as cv
# 加载图像
image = cv.imread('www.jpg', 0)
def BLPF(image, D0):
L = np.empty_like(image, float)
h, w = image.shape
for i in range(0, h):
for j in range(0, w):
d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
L[j, i] = 1 / (1 + (d / D0) ** 2)
return L
def fft(image, L):
dft = np.fft.fft2(image)
dftshift = np.fft.fftshift(dft)
rs = dftshift * L
idft_shift = np.fft.ifftshift(rs)
idft = np.fft.ifft2(idft_shift)
result = np.real(idft)
return np.uint8(result)
def GLPF(image, D0):
L = np.empty_like(image, float)
h, w = image.shape
for i in range(0, h):
for j in range(0, w):
d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
L[j, i] = np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
return L
cv.imshow('image', image)
# cv.imshow('BLPF_D0=10', fft(image, BLPF(image, 10)))
# cv.imshow('BLPF_D0=20', fft(image, BLPF(image, 20)))
# cv.imshow('BLPF_D0=40', fft(image, BLPF(image, 40)))
# cv.imshow('BLPF_D0=80', fft(image, BLPF(image, 80)))
cv.imshow('GLPF_D0=10', fft(image, GLPF(image, 10)))
cv.imshow('GLPF_D0=20', fft(image, GLPF(image, 20)))
cv.imshow('GLPF_D0=40', fft(image, GLPF(image, 40)))
cv.imshow('GLPF_D0=80', fft(image, GLPF(image, 80)))
cv.waitKey()
高通滤波
import numpy as np
import cv2 as cv
# 加载图像
image = cv.imread('www.jpg', 0)
def BHPF(image, D0):
L = np.empty_like(image, float)
h, w = image.shape
for i in range(0, h):
for j in range(0, w):
d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
L[j, i] = 1 / (1 + (D0 / d) ** 2)
return L
def fft(image, L):
dft = np.fft.fft2(image)
dftshift = np.fft.fftshift(dft)
rs = dftshift * L
idft_shift = np.fft.ifftshift(rs)
idft = np.fft.ifft2(idft_shift)
result = np.real(idft)
return np.uint8(result)
def GHPF(image, D0):
L = np.empty_like(image, float)
h, w = image.shape
for i in range(0, h):
for j in range(0, w):
d = np.sqrt((j - int(h / 2)) ** 2 + (i - int(w / 2)) ** 2)
L[j, i] = 1-np.exp((-1) * (d ** 2) / (2 * (D0 ** 2)))
return L
cv.imshow('image', image)
cv.imshow('BHPF_D0=10', fft(image, BHPF(image, 10)))
cv.imshow('BHPF_D0=20', fft(image, BHPF(image, 20)))
cv.imshow('BHPF_D0=40', fft(image, BHPF(image, 40)))
cv.imshow('BHPF_D0=80', fft(image, BHPF(image, 80)))
# cv.imshow('GHPF_D0=10', fft(image, GHPF(image, 10)))
# cv.imshow('GHPF_D0=20', fft(image, GHPF(image, 20)))
# cv.imshow('GHPF_D0=40', fft(image, GHPF(image, 40)))
# cv.imshow('GHPF_D0=80', fft(image, GHPF(image, 80)))
cv.waitKey()