0

I want to extract digits from this image.

enter image description here

I have used opencv to preprocess the image using this code snippet

def inverte(imagem):
    imagem = (255-imagem)
    return imagem

import cv2
image = cv2.imread('5.jpg', 0)
print(image)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
thresh=inverte(thresh)

The output of this code is this

enter image description here

Now after that I am using tesseract to get the text from this image using this code

import pytesseract
image=cv2.imread("output.png")
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
custom_config = r'--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789'
results = pytesseract.image_to_string(rgb,lang='eng',config=custom_config)
print(results)

I have tried all valid psm values and oem values but it not giving correct result

1 Answers1

2

You need to know the followings:

You have already know the page-segmentation-modes, therefore I don't need to recommend it.

One way to recognize the text is applying threshold to the image. You have three-options

You have already applied simple-threshold and couldn't get the desired result. Next you can apply adaptive-threshold which will be:

enter image description here

Now if you read it with your current configuration:

607

Unfortunately digit 9 is not recognized.

If we try inRange threshold:

enter image description here

Now if you read it with your current configuration:

607

Code:

import cv2
import numpy as np
import pytesseract

# Load the image
img = cv2.imread("EkXoW.jpg")

# Convert to the gray-scale
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Convert to the HSV color
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Adaptive-threshold
thr = cv2.adaptiveThreshold(gry, 255,
                            cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY, 13, 10)

# Threshold-in-range
thr_in_range = cv2.inRange(hsv,
                           np.array([0, 0, 0]),
                           np.array([179, 255, 185]))

# OCR
print("Threshold-in-range result")
print(pytesseract.image_to_string(thr_in_range,
                                  config="--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789"))

print("Adaptive threshold result")
print(pytesseract.image_to_string(thr,
                                  config="--psm 13 --oem 1 -c tessedit_char_whitelist=0123456789"))

# Display
cv2.imshow("Threshold-in-range result", thr_in_range)
cv2.imshow("Adaptive threshold result", thr)
cv2.waitKey(0)

You can get the same results in 0.3.7

This answer is more of a small-tutorial for image-processing in text recognition than a complete-answer. The reason is the digit-nine is not recognized. Still, I think the explanation contains brief information and useful links which might encourage you to recognize the digit-nine.

Ahmet
  • 7,527
  • 3
  • 23
  • 47
  • Hello I have tried all the possible image processing techniques but tesseract as mentioned by you even then it is not giving correct result – SACHIN MOHAN Apr 07 '21 at 11:45