![](/img/trans.png)
[英]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.