[英]Python convert color image to black text on white background for OCR
[英]How to read black text on black background image through tesseract OCR?
您需要做的是在让tesseract发挥作用之前将整个图像变成黑白。
阅读图片
import cv2
im_gray = cv2.imread('your_image_here', cv2.IMREAD_GRAYSCALE)
将其设为灰度
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
“它使用Otsu的方法从图像自动确定阈值,或者如果您已经知道阈值,则可以使用:”
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
写入磁盘
cv2.imwrite('bw_image.png', im_bw)
您可以通过简单的转换将灰色像素转换为白色像素。 如果您不想使用开放式简历,并且图像是一个通道(灰度级)的numpy数组:
threshold = 60 # try something between 30 and 150
vect_func = np.vectorize(lambda x: 0 if x == threshold else 255)
black_white_img = vect_func(gray_scale_image)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.