【问题标题】:Remove wavy noise from image background using OpenCV使用 OpenCV 从图像背景中去除波浪噪声
【发布时间】:2018-07-14 21:37:46
【问题描述】:

我想去除背景中存在的噪音。噪音不是标准模式。我想去除背景噪音并将文本保留在白色背景上。

这是一个图片示例:

我使用下面的代码非常简单的处理步骤。

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)

这是输出图像:

我怎样才能提高这个结果

【问题讨论】:

  • 请出示原图——您可能已经丢弃了有用的信息。
  • 编辑图片
  • 这是波浪状的噪音。我认为这是这里的关键人物

标签: python opencv


【解决方案1】:

this post 中所述,您可以使用对比度/亮度来移除背景像素。

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)

【讨论】:

  • 这是一个很好的建议,但它的手动任务需要干预才能查看每张图像并尝试最适合alpha beta
【解决方案2】:

这也是基于噪声像素和文本的灰度差异。您可以手动调整阈值。通过考虑噪声和文本像素值的分布,您可能可以达到合理的自动阈值。下面我使用这样的阈值,即mean - std。它适用于给定的图像,但不确定它是否能正常工作。

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

干净的图像:

考虑到像素密度的干净图像:

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

【讨论】:

  • 此解决方案看起来不错,但问题仅适用于此图像。我认为会尝试推广解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-22
  • 1970-01-01
  • 2017-07-05
  • 2017-03-19
  • 2012-07-03
  • 2013-02-25
相关资源
最近更新 更多