level 1
nmHker
楼主
有个问题向贴吧的大佬请教,我想把带通滤波的图像用cv2.imwrite()保存下来,但是保存后的图片一片黑,请问该怎么解决这个问题?
这是带通的代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
def gaussian_bandstop_kernel(img, D0, W):
assert img.ndim == 2
r, c = img.shape[1], img.shape[0]
u = np.arange(r)
v = np.arange(c)
u, v = np.meshgrid(u, v)
low_pass = np.sqrt((u - r / 2) ** 2 + (v - c / 2) ** 2)
kernel = 1.0 - np.exp(-0.5 * (((low_pass ** 2 - D0 ** 2) / (low_pass * W + 1.0e-5)) ** 2))
return kernel
def gaussian_bandpass_filter(img, D0=5, W=10):
assert img.ndim == 2
kernel = 1.0 - gaussian_bandstop_kernel(img, D0, W)
gray = np.float64(img)
gray_fft = np.fft.fft2(gray)
gray_fftshift = np.fft.fftshift(gray_fft)
dst = np.zeros_like(gray_fftshift)
dst_filtered = kernel * gray_fftshift
dst_ifftshift = np.fft.ifftshift(dst_filtered)
dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.abs(np.real(dst_ifft))
dst = np.clip(dst, 0, 255)
return np.uint8(dst)
# 读取图像
img = cv2.imread('01_14_11_03_06_Capture.jpg', 0)
new_image1 = gaussian_bandpass_filter(img, D0=28, W=10)
new_image2 = gaussian_bandpass_filter(img, D0=38, W=10)
new_image3 = gaussian_bandpass_filter(img, D0=48, W=10)
cv2.imwrite('new_image2.jpg',new_image2)
# # 显示原始图像和带通滤波处理图像
# title = ['Source Image', 'D0=28', 'D0=38', 'D0=48']
# images = [img, new_image1, new_image2, new_image3]
# for i in np.arange(4):
# plt.su
bp
lot(2, 2, i + 1), plt.imshow(images[i], 'gray')
# plt.title(title[i])
# plt.xticks([]), plt.yticks([])
# plt.show()

2022年03月15日 10点03分
1
这是带通的代码:
import cv2
import numpy as np
from matplotlib import pyplot as plt
def gaussian_bandstop_kernel(img, D0, W):
assert img.ndim == 2
r, c = img.shape[1], img.shape[0]
u = np.arange(r)
v = np.arange(c)
u, v = np.meshgrid(u, v)
low_pass = np.sqrt((u - r / 2) ** 2 + (v - c / 2) ** 2)
kernel = 1.0 - np.exp(-0.5 * (((low_pass ** 2 - D0 ** 2) / (low_pass * W + 1.0e-5)) ** 2))
return kernel
def gaussian_bandpass_filter(img, D0=5, W=10):
assert img.ndim == 2
kernel = 1.0 - gaussian_bandstop_kernel(img, D0, W)
gray = np.float64(img)
gray_fft = np.fft.fft2(gray)
gray_fftshift = np.fft.fftshift(gray_fft)
dst = np.zeros_like(gray_fftshift)
dst_filtered = kernel * gray_fftshift
dst_ifftshift = np.fft.ifftshift(dst_filtered)
dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.abs(np.real(dst_ifft))
dst = np.clip(dst, 0, 255)
return np.uint8(dst)
# 读取图像
img = cv2.imread('01_14_11_03_06_Capture.jpg', 0)
new_image1 = gaussian_bandpass_filter(img, D0=28, W=10)
new_image2 = gaussian_bandpass_filter(img, D0=38, W=10)
new_image3 = gaussian_bandpass_filter(img, D0=48, W=10)
cv2.imwrite('new_image2.jpg',new_image2)
# # 显示原始图像和带通滤波处理图像
# title = ['Source Image', 'D0=28', 'D0=38', 'D0=48']
# images = [img, new_image1, new_image2, new_image3]
# for i in np.arange(4):
# plt.su
bp
lot(2, 2, i + 1), plt.imshow(images[i], 'gray')
# plt.title(title[i])
# plt.xticks([]), plt.yticks([])
# plt.show()


