使用OpenCV去除图像背景中的波浪噪声

6
我希望能够去除背景中存在的噪声,但这些噪声并非具有标准模式。我希望能够去除背景噪声并保留白色背景上的文本。
以下是一个图像示例:

enter image description here 我使用了以下代码进行简单的处理步骤。
import cv2 
import numpy as np

img = cv2.imread("noisy.PNG")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.subtract(255,gray)
ret,thresh = cv2.threshold(gray,5,255,cv2.THRESH_TOZERO)


noisy_removal = cv2.fastNlMeansDenoising(thresh, None, 65, 5, 21)
cv2.imwrite("finalresult.jpg",255-noisy_removal)

这是输出图像:

enter image description here 我该如何改进这个结果?

请展示原始图像 - 你可能已经丢弃了有用的信息。 - Mark Setchell
编辑了图片。 - ahmed osama
这是波浪噪声。我认为这是关键因素。 - ahmed osama
2个回答

5
您可以根据这篇文章中讨论的方法,调整对比度/亮度来消除背景像素。
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

alpha = 2.5
beta = -0.0

denoised = alpha * gray + beta
denoised = np.clip(denoised, 0, 255).astype(np.uint8)

denoised = cv2.fastNlMeansDenoising(denoised, None, 31, 7, 21)

result


1
这是一个很好的建议,但手动操作需要进行干预,以查看每个图像并尝试最佳拟合alpha beta - ahmed osama

2

这也基于噪声像素和文本的灰度差异。您可以手动调整阈值。通过考虑噪声和文本像素值的分布,您可能会得出合理的自动阈值。以下我使用这样的阈值:平均值 - 标准差。它对于给定的图像有效,但不确定是否适用于一般情况。

im = cv2.imread('XKRut.jpg')
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

# calculate auto-threshold value
# noise and text pixels
_, bw = cv2.threshold(cv2.GaussianBlur(gray, (3, 3), 0), 0, 255, cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
# find mean and std of noise and text pixel values
mu, sd = cv2.meanStdDev(gray, mask=bw)

simple = gray.copy()
# apply the auto-threshold to clean the image
thresh = mu - sd
simple[simple > thresh] = 255
simple[simple <= thresh] = 0

simple2 = simple.copy()
# clean it further considering text-like pixel density in a 3x3 window
# using avg = ~cv2.blur(simple, (3, 3)) is more intuitive, but Gaussian
# kernel gives more weight to center pixel
avg = ~cv2.GaussianBlur(simple, (3, 3), 0)
# need more than 3 text-like pixels in the 3x3 window to classify the center pixel
# as text. otherwise it is noise. this definition is more relevant to box kernel
thresh2 = 255*3/9
simple2[avg < thresh2] = 255

清晰的图像:

simple

考虑像素密度的清晰图像:

simple2

如果您的图像像素值没有太大变化,可以预先计算最佳阈值或zindarod解决方案的alphabeta对。


这个解决方案看起来不错,但问题是它只适用于这张图像。我想尝试将其推广化。 - ahmed osama

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接