繁体   English   中英

OpenCV 将所有文本处理为白底黑字(分割)

[英]OpenCV process all text to be black on white (segmentation)

是否有可能以某种方式使文档中的所有文本在阈值化后都是黑底白字。 我一直在网上寻找很多,但我一直无法找到解决方案。 我当前的阈值图像是: https : //i.ibb.co/Rpqcp7v/thresh.jpg

该文档需要由 OCR 读取,为此我需要将当前为黑底白字的区域反转。 我该怎么做呢? 我目前的代码:

# thresholding
def thresholding(image):
    # thresholds the image into a binary image (black and white)
    return cv2.threshold(image, 120, 255, cv2.THRESH_BINARY)[1]

使用中值滤波器来估计主色(背景)。

然后从中减去图像......你会得到黑色背景上的白色文本。 我正在使用绝对差异。 反转为白底黑字。

im = cv.imread("thresh.jpg", cv.IMREAD_GRAYSCALE)
im = cv.pyrDown(cv.pyrDown(im)) # picture too large for stack overflow
bg = cv.medianBlur(im, 51) # suitably large kernel to cover all text
out = 255 - cv.absdiff(bg, im)

在此处输入图片说明

在此处输入图片说明 在此处输入图片说明

暂无
暂无

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

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