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

时间:2018-07-14 21:37:46

标签: python opencv

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

这是一个图像样本:

enter image description here

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

$(document).ready(function() {
  var transEffect = Barba.BaseTransition.extend({
    start: function() {
      this.newContainerLoading.then(val => this.fadeInNewcontent($(this.newContainer)));
    },
    fadeInNewcontent: function(nc) {
      nc.hide();
      var _this = this;
      $(this.oldContainer).fadeOut(1000).promise().done(() => {
        nc.css('visibility', 'visible');
        nc.fadeIn(1000, function() {
          _this.done();
        });
        $('html, body').animate({
          scrollTop: 300
        },1000);
      });
    }
  });
  Barba.Pjax.getTransition = function() {
    return transEffect;
  }
  Barba.Pjax.start();
});

以下是输出图像:

enter image description here

我怎样才能改善这个结果

2 个答案:

答案 0 :(得分:5)

您可以按照对比度/亮度进行操作,以消除背景像素,如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)

result

答案 1 :(得分: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

清洁的图像:

simple

在考虑像素密度的情况下清洁图像:

simple2

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