Python 如何从输入图像中去除类似棋盘的噪声

Python 如何从输入图像中去除类似棋盘的噪声,python,opencv,computer-vision,Python,Opencv,Computer Vision,我正在做一件事,这需要我增强一个输入图像,使其具有一致的棋盘一样的噪声,并均匀投影 这是预期的输出图像 您可以尝试使用Weiner过滤器。它不会像你在问题中显示的那样给你带来尖锐的结果,但它可以消除棋盘上的噪音 from skimage import color, data, restoration from scipy.signal import convolve2d import matplotlib.pyplot as plt img = cv2.imread('chessboard.

我正在做一件事,这需要我增强一个输入图像,使其具有一致的棋盘一样的噪声,并均匀投影

这是预期的输出图像
您可以尝试使用Weiner过滤器。它不会像你在问题中显示的那样给你带来尖锐的结果,但它可以消除棋盘上的噪音

from skimage import color, data, restoration
from scipy.signal import convolve2d
import matplotlib.pyplot as plt

img = cv2.imread('chessboard.jpg', 0)
psf = np.ones((9, 9)) / 81
img = convolve2d(img, psf, 'same')
img += 0.1 * img.std() * np.random.standard_normal(img.shape)
deconvolved_img = restoration.wiener(img, psf, 1, clip=False)

plt.imshow(deconvolved_img, cmap='gray')


如果您想在此结果上进一步使用OpenCV,请使用
res=deconvolved\u img.astype(np.uint8)
将其转换为
uint8
,这里有一种在Python/OpenCV中使用陷波过滤的方法

  • 将输入读取为灰度
  • 对复实部和虚部进行DFT
  • 将直流点移到中心
  • 将实部和虚部转换为幅值和相位
  • 根据震级计算光谱图像
  • 对光谱图像进行阈值化,以提取与规则棋盘格图案对应的网格线
  • 使中心DC点周围的白色区域变黑
  • 倒置面具
  • 将幅值乘以遮罩
  • 将新幅值和旧相位转换为实部和虚部
  • 结合实部和虚部
  • 将直流点移到左上角
  • 执行DIFT并将其转换为8位范围作为陷波滤波器结果
  • 拉伸动态范围以提高可见性
  • 保存结果

输入:


光谱:

遮罩:

陷波滤波器结果:

凹口滤波器对比度增强:


你能举个例子吗?到目前为止你都做了些什么?我把图像添加到了原图中post@AhmedElshamy您是否也有一个预期输出的示例?您的图像大小是否恒定?棋盘图案的大小/位置是否恒定?如果是这样,你可以考虑只减去一个纯粹的棋盘。pattern@MLavrentyev棋盘图案和图像都是常量大小是的
import numpy as np
import cv2
import skimage.exposure

# read input as grayscale
# opencv fft only works on grayscale
img = cv2.imread('checkerboard_noise.jpg', 0)
hh, ww = img.shape[:2]

# convert image to floats and do dft saving as complex output
dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)

# apply shift of origin from upper left corner to center of image
dft_shift = np.fft.fftshift(dft)

# extract magnitude and phase images
mag, phase = cv2.cartToPolar(dft_shift[:,:,0], dft_shift[:,:,1])

# get spectrum for viewing only
spec = (np.log(mag) / 20)

# threshold spectrum as mask
mask = cv2.threshold(spec, 0.5, 1, cv2.THRESH_BINARY)[1]

# blacken out center DC region from mask
cx = ww // 2
cy = hh // 2
mask = cv2.circle(mask, (cx,cy), 10, 0, -1)

# invert mask
mask = 1 - mask

# apply mask to magnitude image
mag_notch = mask*mag

# convert magnitude and phase into cartesian real and imaginary components
real, imag = cv2.polarToCart(mag_notch, phase)

# combine cartesian components into one complex image
complex = cv2.merge([real, imag])

# shift origin from center to upper left corner
complex_ishift = np.fft.ifftshift(complex)

# do idft with normalization saving as real output
img_notch = cv2.idft(complex_ishift, flags=cv2.DFT_SCALE+cv2.DFT_REAL_OUTPUT)
img_notch = img_notch.clip(0,255).astype(np.uint8)

# stretch contrast
img_notch_stretched = skimage.exposure.rescale_intensity(img_notch, in_range=(75,150), out_range=(0,255))

# write result to disk
cv2.imwrite("checkerboard_noise_spectrum.png", (255*spec).clip(0,255).astype(np.uint8))
cv2.imwrite("checkerboard_noise_mask.png", (255*mask).clip(0,255).astype(np.uint8))
cv2.imwrite("checkerboard_noise_notched.png", img_notch)
cv2.imwrite("checkerboard_noise_notched_stretched.png", img_notch_stretched)

# show results
cv2.imshow("ORIGINAL", img)
cv2.imshow("MAG", mag)
cv2.imshow("PHASE", phase)
cv2.imshow("SPECTRUM", spec)
cv2.imshow("MASK", mask)
cv2.imshow("NOTCH", img_notch)
cv2.imshow("NOTCH_STRETCHED", img_notch_stretched)
cv2.waitKey(0)
cv2.destroyAllWindows()