Extracting text from Image

Barkles picture Barkles · Feb 1, 2017 · Viewed 9.2k times · Source

Two, the type of number I am trying to extract
Two, the type of number I am trying to extract

Another sample

Another sample

Another sample

Another sample

The image above is the output from another image from which I am trying to extract all the numbers. I am selecting each number individually, performing image transformations (thresholding, image contrasting, averaging then contrasting) where none seem to provide a reliable and robust output.

Using thresholding works well but the brightness of the extracted digit is not always the same, and so the threshold values need to be adjusted for it to be accurate. I need something which is going to work every time. The output which I am looking for is something like the image below. After the image transformations are performed, the image is run through Tesseract OCR. When the image below and the like are placed through Tesseract, the output is nearly always correct.

The desired image after image transformations:
The desired image after image transformations

Also, not all the numbers are the same size. I need this to work no matter the size of the digit. Can anyone help?

Answer

ZdaR picture ZdaR · Feb 1, 2017

No man, you may not need to do all that by yourself, OpenCV already has OTSU binarization method implemented, which would exactly suit your case. Basically it assumes the input intensities distribution to be bi-modal. And hence tries to find the optimal threshold. You can read more here. And here is the small code along with outputs generated.

import cv2

img = cv2.imread("/home/abc/Downloads/1ltYB.png", 0)
ret, thresh = cv2.threshold(img, 10, 255, cv2.THRESH_OTSU)

print "Threshold selected : ", ret
cv2.imwrite("./debug.png", thresh)

Input: (I have cropped the image to remove the extra padding)

enter image description here

Output:

enter image description here