[英]Extract a white with black border text in an image with a complex background
[英]How to binarize an image when image has white text on black background and vice versa?
我想對 OCR 的圖像進行二值化。 我附上了將圖像數據作為輸入並返回二進制圖像的代碼,此方法適用於大多數圖像。
例如,
def preprocessing(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blured1 = cv2.medianBlur(image, 3)
blured2 = cv2.medianBlur(image, 51)
divided = np.ma.divide(blured1, blured2).data
normed = np.uint8(255 * divided / divided.max())
th, image = cv2.threshold(normed, 100, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
image = cv2.erode(image, np.ones((3, 3), np.uint8))
image = cv2.dilate(image, np.ones((3, 3), np.uint8))
return image
但是當我在下面附加的圖像上應用相同的方法時,它不會按預期工作。 它應該提供具有用於 tesseract 輸入的可讀文本的圖像。
您可能應該嘗試自己拆卸圖像。 我認為 Bradley-Roth 算法( Bradley-Roth Adaptive Thresholding Algorithm - How do I get better performance? )可以幫助您稍作修改 - 如果鄰域比 128 亮,那么如果鄰域是比 128 暗,那么亮的部分會突出顯示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.