【问题标题】:Remove noise from image without losing data in OpenCV从图像中去除噪声而不丢失 OpenCV 中的数据
【发布时间】:2020-03-18 13:22:21
【问题描述】:

我使用了这个代码:

    horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontalsize, 1))
    horizontal = cv2.erode(horizontal, horizontalStructure, (-1, -1))
    horizontal = cv2.dilate(horizontal, horizontalStructure, (-1, -1))

删除线条。

还有一些过滤器可以删除噪音并加粗字体:

 blur = cv2.GaussianBlur(img, (11, 11), 0)
 thresh = cv2.threshold(blur, 80, 255, cv2.THRESH_BINARY)[1]
 kernel = np.ones((2,1), np.uint8)
 dilation = cv2.erode(thresh, kernel, iterations=1)
 dilation = cv2.bitwise_not(dilation)

尽管有阈值和其他方法,但您可以看到仍然存在大量噪音

这是我想要达到的结果:

你知道可以帮助我实现这个结果的 OpenCV 过滤器吗?

【问题讨论】:

  • opencv中有很多方法可以进行图像阈值化。你还没有写出你用过哪一个。你见过这个documentation 吗?
  • 我认为如果没有关于图像数据的特定领域知识,就没有可以应用的开箱即用过滤器。我认为您基本上是在寻找要从图像中删除的垂直线和水平线。如果是这种情况,有一些边缘检测过滤器可以帮助您。也许您可以通过 blob 大小和 blob 的纵横比过滤检测到的边缘?但同样,这种强烈程度取决于图像的结构。我认为您正在尝试从图像中删除某些特征而不是噪声。

标签: python image opencv image-preprocessing


【解决方案1】:

以下解决方案不是完美的,也不是通用的解决方案,但我希望它足以满足您的需求。

对于删除线,我建议使用cv2.connectedComponentsWithStats 来查找集群,并屏蔽宽或长集群。

解决方案使用以下阶段:

  • 将图像转换为灰度。
  • 应用阈值并反转极性。
    通过应用标志 cv2.THRESH_OTSU 来使用自动阈值。
  • 使用“关闭”形态操作来关闭小间隙。
  • 使用统计信息查找连接的组件(集群)。
  • 迭代集群,删除宽高大的集群。
    删除非常小的集群 - 被认为是噪音。
  • “手动”清洁顶部和左侧。

代码如下:

import numpy as np
import cv2

img = cv2.imread('Heshbonit.jpg')  # Read input image

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # Convert to Grayscale.

ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)  # Convert to binary and invert polarity

# Use "close" morphological operation to close small gaps
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.array([1, 1]));
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, np.array([1, 1]).T);

nlabel,labels,stats,centroids = cv2.connectedComponentsWithStats(thresh, connectivity=8)

thresh_size = 100

# Delete all lines by filling wide and long lines with zeros.
# Delete very small clusters (assumes to be noise).
for i in range(1, nlabel):
    #
    if (stats[i, cv2.CC_STAT_WIDTH] > thresh_size) or (stats[i, cv2.CC_STAT_HEIGHT] > thresh_size):
        thresh[labels == i] = 0
    if stats[i, cv2.CC_STAT_AREA] < 4:
        thresh[labels == i] = 0

# Clean left and top margins "manually":
thresh[:, 0:30] = 0
thresh[0:10, :] = 0

# Inverse polarity
thresh = 255 - thresh

# Write result to file
cv2.imwrite('thresh.png', thresh)

【讨论】:

    猜你喜欢
    • 2014-05-22
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多