0

Extract digit from noisy image

I want to extract text from an image taken by mobile phone camera. First I try to convert the image to greyscale by using this code:

imgg = Image.open('originale.jpg').convert('LA')

Second i try to threshold the grey image to get image with only black and white with this code ::

 retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)

Third i try to extract text with pytesseract but i have not the correct result with this code.

result5 = pytesseract.image_to_string(Image.open("threshold.png"))

This is the image which I want to extract digits number for example: My expected output is: 111 2 11 4 1 23 2 3.

and this is my image :

originale.jpg

threshold.png

And this is my full code:

import cv2
import numpy as np
import pytesseract
from PIL import Image
img = cv2.imread('originale.jpg')
grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)
result = pytesseract.image_to_string(Image.open("threshold.png"))
print(result)
solo_s
  • 1
  • 3
  • I suggest that you convert the image to grayscale and threshold it so that you have black numbers on white background. Then try to extract the digits as text. – fmw42 Oct 23 '19 at 16:41
  • i try to covert my image to grayscale like this `imgg = Image.open('6.jpg').convert('LA') imgg.save('greyscale.png')` then i try to threshold the image `retval, threshold = cv2.threshold(imgg, 49, 255, cv2.THRESH_BINARY)` – solo_s Oct 24 '19 at 08:17

1 Answers1

0

You can use Otsu method to determine optimal threshold value to exact your digits.

import cv2

img # this is your original image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite("threshold.png", threshold)

Result: enter image description here

Piotr Siekański
  • 1,665
  • 8
  • 14
  • i try this code but i have nothing: `import cv2 import numpy as np import pytesseract from PIL import Image img = cv2.imread('3.jpg') grayscaled = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) retval, threshold = cv2.threshold(grayscaled, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) cv2.imwrite("threshold.png", threshold) result6 = pytesseract.image_to_string(Image.open("threshold.png")) print(result6)` – solo_s Oct 25 '19 at 08:55