【问题标题】:How do I remove the dots / noise without damaging the text?如何在不损坏文本的情况下去除点/噪音?
【发布时间】:2018-02-08 09:03:03
【问题描述】:

我正在使用 OpenCV 和 Python 处理图像。我需要去除图像中的点/噪点。
我尝试了膨胀,使点变小,但是文本被损坏了。我还尝试了两次循环膨胀和一次腐蚀。但这并没有给出令人满意的结果。
还有其他方法可以实现吗?
谢谢你:)

编辑:
我是图像处理的新手。我目前的代码如下

image = cv2.imread(file)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
kernel = np.ones((2, 2), np.uint8)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
gray = cv2.erode(gray, kernel, iterations=1)
gray = cv2.dilate(gray, kernel, iterations=1)
cv2.imwrite(file.split('.'[0]+"_process.TIF", gray))

编辑 2:
我尝试了中值模糊。它已经解决了 90% 的问题。我一直在使用 gaussianBlurring。
谢谢

【问题讨论】:

  • 也许是中值过滤器?
  • @api55 该死。这带来了巨大的变化! (虽然还有一点点)。我一直在使用 gaussianBlurring。谢谢!
  • 中值滤波器通常适用于这种周围像素为白色的噪声(椒盐噪声)。小噪音,可能你必须用其他方式过滤,比如腐蚀膨胀,只要记住模糊(用高斯)可能会使点变大,效果不好,还会模糊字母
  • 也许你应该读一本关于图像处理基础的书......至少前几章......否则你只是浪费时间试图以错误的方式解决问题
  • @Piglet 可以。!????

标签: python image opencv image-processing


【解决方案1】:

如何使用connectedComponentsWithStats删除小的连接组件

import cv2
import numpy as np

img = cv2.imread('path_to_your_image', 0)
_, blackAndWhite = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)

nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(blackAndWhite, None, None, None, 8, cv2.CV_32S)
sizes = stats[1:, -1] #get CC_STAT_AREA component
img2 = np.zeros((labels.shape), np.uint8)

for i in range(0, nlabels - 1):
    if sizes[i] >= 50:   #filter small dotted regions
        img2[labels == i + 1] = 255

res = cv2.bitwise_not(img2)

cv2.imwrite('res.png', res)

这里是 c++ 示例:

Mat invBinarized;

threshold(inputImage, invBinarized, 127, 255, THRESH_BINARY_INV);
Mat labels, stats, centroids;

auto nlabels = connectedComponentsWithStats(invBinarized, labels, stats, centroids, 8, CV_32S, CCL_WU);

Mat imageWithoutDots(inputImage.rows, inputImage.cols, CV_8UC1, Scalar(0));
for (int i = 1; i < nlabels; i++) {
    if (stats.at<int>(i, 4) >= 50) {
        for (int j = 0; j < imageWithoutDots.total(); j++) {
            if (labels.at<int>(j) == i) {
                imageWithoutDots.data[j] = 255;
            }
        }
    }
}
cv::bitwise_not(imageWithoutDots, imageWithoutDots);

编辑:
另见

OpenCV documentation for connectedComponentsWithStats

How to use openCV's connected components with stats in python

Example from learning opencv3

【讨论】:

  • @DmitriiZ 工作出色,但速度极慢 - 您是否知道可能更快的更 numpy-esque 方式(无 for 循环),还是我应该将其作为问题提出?
  • @jtlz2 我找不到任何合理的方法来使 python 代码更快,也许你可以在 codereview.stackexchange.com 或者甚至在这里试试运气,我想应该有办法使这个问题成为主题。也就是说,如果性能真的很重要,我不会对非缩放图像使用基于 connectedComponentsWithStats 的方法,因为您实际上是在重新创建整个图像。
  • @DmitriiZ。谢谢回复。我花了一段时间 - numba 是我最终发现加速它的唯一方法 - 即并行化 - 所有路由都需要 npix * npix * nregions 操作。感谢您的快速回复 - 非常感谢
  • 它适用于我的文本图像文件,但它的速度很慢!!!!我还必须在打开时添加一个标志。 image1 = cv.imread(fname, cv.IMREAD_GRAYSCALE)。也许是因为我的图像是白色背景上的黑色文本。
  • @QuentinJS 如今,人们倾向于训练神经网络来完成执行相当好的清洁任务。
猜你喜欢
  • 2019-12-06
  • 2012-10-27
  • 2020-05-24
  • 2011-02-19
  • 1970-01-01
  • 2014-12-11
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多