0

I want to find a way to detect the red number 3 which is on a red background. I've tried changing the contrast on the image, as well as also trying a blur + adaptive thresholding, which both don't detect anything. What's interesting is I can't detect single numbers, but can detect 2 numbers next to each other at nearly 100% accuracy using the same two methods above. I think it's because the background is lighter when it's just one number, so the OCR is having trouble finding it.

Here's the number 3 from the original image (it's 96 dpi): (https://i.stack.imgur.com/t0VR7.jpg)

I changed the contrast on the image by using the following code, and then cropped it to just show the number.

img = cv2.imread(path_to_img, 0)

alpha = 3 # Contrast control (1.0-3.0) 
beta = 0 # Brightness control (0-100)  

images_contrast = cv2.convertScaleAbs(img, alpha=alpha, beta=beta)

cropped = images_contrast[885:917, 1008:1055]  

cv2.imshow("contrast.jpg", cropped)
cv2.imwrite("contrast_easyOCR.jpg", cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()

reader = easyocr.Reader(['en'], gpu=False, verbose=False)
result_Kripp_Hp = reader.readtext(cropped, allowlist="-0123456789")
print(result_Kripp_Hp)

This is the result: 3hp after changing contrast


I also tried a medianblur + adaptive thresholding, which gets me this: (https://i.stack.imgur.com/ezpVD.jpg)

Code below:

img = cv2.imread(path_to_img, 0)
img = cv2.medianBlur(img, 3) 

adapt_Thresholding = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

images = adapt_Thresholding

cropped_adaptive_thresholding = images[885:917, 1011:1055] 

cv2.imshow("adaptiveThresholding.jpg", cropped_adaptive_thresholding)
cv2.imwrite("adaptThreshold_easyOCR.jpg", cropped_adaptive_thresholding)
cv2.waitKey(0)
cv2.destroyAllWindows()

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'

num = pytesseract.image_to_data(cropped_adaptive_thresholding, config='--psm 11 -c tessedit_char_whitelist=0123456789')

print(num)

Both of the above result in no detection by easyocr and pytesseract.


Lastly, easyocr is finding 37 at 99.9% confidence using the contrast code (near the top of this post), which I find a bit odd. Image here: easyocr detects this as '37' correctly at 99.9% confidence

Another thing I tried was messing around with the image in GIMP, and after adding some black pixels to the perimeter of my '3' and then running it through the 'contrast code' above, it detected the 3 correctly at 99.9% confidence. Here's the image: (https://i.stack.imgur.com/fEZ0i.jpg). I think thickening the black line around the 3 would work, but I couldn't figure out how to do this with opencv / python.

Any tips / suggestions (I'm coding in Python) would be greatly appreciated! Thank you.

stateMachine
  • 5,227
  • 4
  • 13
  • 29
bigfry
  • 1

0 Answers0