简体   繁体   English

如何在不损坏文本的情况下去除图像中的背景噪声?

[英]How to remove background noise in image without damaging text?

I am processing images for tesseract's ocr. 我正在为tesseract的ocr处理图像。 I need help to get rid of the background noise without damaging the text. 我需要帮助摆脱背景噪音而不损坏文字。

Example input image 输入图像示例

这是一个示例图像

I have tried median blurring and removing small connected components ( How do I remove the dots / noise without damaging the text? ). 我尝试过中值模糊和删除小的连接组件( 如何在不损坏文本的情况下删除点/噪声? )。 The problem with connected components is that the noise can have larger connections and I cannot get rid of it without also removing the minus sign. 连接组件的问题是噪声可能有更大的连接,我不能删除它而不删除减号。 Any suggestion how to move forward? 有什么建议如何前进?

Since your image is only black/white, you can do simple thresholding and morphological transformations to filter the image. 由于您的图像只是黑/白,您可以进行简单的阈值处理和形态转换来过滤图像。 If your image input was not black and white, you could do blurring techniques such as cv2.medianBlur() or cv2.GaussianBlur() to smooth the image as a preprocessing step. 如果您的图像输入不是黑白图像,则可以使用模糊技术(如cv2.medianBlur()cv2.GaussianBlur()来平滑图像作为预处理步骤。 Then you could perform morphological operations with various kernel sizes or construct custom kernels with cv2.getStructuringElement() . 然后,您可以使用各种内核大小执行形态学操作,或使用cv2.getStructuringElement()构造自定义内核。 Generally, a larger kernel size ( 7x7 or 9x9 ) will remove more noise but also remove the desired details as opposed to a smaller kernel ( 3x3 or 5x5 ). 通常,较大的内核大小( 7x79x9 )将消除更多噪声,但也会删除所需的细节,而不是较小的内核( 3x35x5 )。 There is a trade off depending on how much noise you want to remove while balancing the amount of details to preserve. 根据您想要移除多少噪音进行权衡,同时平衡要保留的细节数量。 Take a look at this answer for colored captchas. 看看这个答案彩色验证码。


Threshold

在此输入图像描述

Morph close 变形关闭

在此输入图像描述

Invert image for Tesseract 反转Tesseract的图像

在此输入图像描述

Result 结果

-63 164 -63 164

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image = cv2.imread('1.png',0)
thresh = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY_INV)[1]

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

result = 255 - opening
cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('result', result)

print(pytesseract.image_to_string(result))
cv2.waitKey()

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

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