简体   繁体   English

从图像中去除噪声而不会丢失 OpenCV 中的数据

[英]Remove noise from image without losing data in OpenCV

i used this code:我使用了这个代码:

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

to remove lines.删除线条。

and some filters to delete the noises and bold the font:和一些过滤器来删除噪音并加粗字体:

 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)

Despite the threshold and other methods, as you can see lots of noise remained尽管有阈值和其他方法,如您所见,仍然存在大量噪音

This is the result I want to reach:这是我想要达到的结果:

Do you know an OpenCV filter that will help me achieve this result?您知道可以帮助我实现此结果的 OpenCV 过滤器吗?

The following solution is not a perfect, and not generic solution, but I hope it's good enough for your needs.以下解决方案不是完美的,也不是通用的解决方案,但我希望它足以满足您的需求。

For removing the line I suggest using cv2.connectedComponentsWithStats for finding clusters, and mask the wide or long clusters.为了删除该行,我建议使用cv2.connectedComponentsWithStats来查找集群,并屏蔽宽或长的集群。

The solution uses the following stages:该解决方案使用以下阶段:

  • Convert image to Grayscale.将图像转换为灰度。
  • Apply threshold and invert polarity.应用阈值并反转极性。
    Use automatic thresholding by applying flag cv2.THRESH_OTSU .通过应用标志cv2.THRESH_OTSU使用自动阈值。
  • Use "close" morphological operation to close small gaps.使用“关闭”形态操作来关闭小间隙。
  • Find connected components (clusters) with statistics.使用统计信息查找连接的组件(集群)。
  • Iterate the clusters, and delete clusters with large width and large height.对簇进行迭代,删除宽高大的簇。
    Remove very small clusters - considered to be noise.删除非常小的簇 - 被认为是噪音。
  • The top and left side is cleaned "manually".顶部和左侧是“手动”清洁的。

Here is the code:这是代码:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM