I try to extract numbers using OCR.
The development environment is run by pycharm (Python version 3).
My problem is how to extract numbers using OCR.
The image looks like this:
In the picture above I want to get the following numeric text:
1 2 3
4 5 6 7
8 9 0
How can I get the results I want?
There a range of libraries to achieve this here is an example of one from: https://pypi.org/project/pytesseract/ https://github.com/madmaze/pytesseract
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
# Simple image to string
print(pytesseract.image_to_string(Image.open('test.png')))
You can Otsu's threshold to obtain a binary image then extract each number. After thresholding we get this
Now we iterate through the contours and extract/save each ROI
Now you can apply your desired OCR tool to read the text on each ROI
import cv2
image = cv2.imread('1.jpg', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c, num in zip(cnts, range(len(cnts))):
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.imwrite('ROI_{}.png'.format(num), ROI)
cv2.imshow('thresh', 255 - thresh)
cv2.waitKey()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.