Reputation: 105
I am trying to get coordinate from a map, I took the coordinate part and I applied on it OCR using Pytesseract but I can't get the coordinate. this is the link to the image "https://ibb.co/hVynk2b" i tried this script:
import numpy as np
import cv2 as cv
%matplotlib inline
from matplotlib import pyplot as plt
img = cv.imread('a.jpg')
corped = img[460:700, 700:1000]
image=cv2.cvtColor(corped,cv2.COLOR_BGR2GRAY)
se=cv2.getStructuringElement(cv2.MORPH_RECT , (8,8))
bg=cv2.morphologyEx(image, cv2.MORPH_DILATE, se)
out_gray=cv2.divide(image, bg, scale=255)
out_binary=cv2.threshold(out_gray, 0, 255, cv2.THRESH_OTSU )[1]
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
from pytesseract import Output
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])
Upvotes: 0
Views: 347
Reputation: 1128
It seems to work for me. I ran the code you pasted, but cleaned it up a bit:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import pytesseract
from pytesseract import Output
img = cv.imread(r'a.jpg')
cropped = img[460:700, 700:1000]
image = cv.cvtColor(cropped, cv.COLOR_BGR2GRAY)
se = cv.getStructuringElement(cv.MORPH_RECT, (8, 8))
bg = cv.morphologyEx(image, cv.MORPH_DILATE, se)
out_gray = cv.divide(image, bg, scale=255)
out_binary = cv.threshold(out_gray, 0, 255, cv.THRESH_OTSU)[1]
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])
and it returns '35°21'24°'
I did notice however that pytesseract doesn't catch the vertical text. You can either add in and mess around with the config parameter when calling image_to_data or you can simply rotate the image 90 degrees clockwise and run it again:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
import pytesseract
from pytesseract import Output
img = cv.imread(r'C:\Users\guneh\Desktop\a.jpg')
rotate = cv.rotate(cropped, cv.ROTATE_90_CLOCKWISE)
image = cv.cvtColor(rotate, cv.COLOR_BGR2GRAY)
se = cv.getStructuringElement(cv.MORPH_RECT, (8, 8))
bg = cv.morphologyEx(image, cv.MORPH_DILATE, se)
out_gray = cv.divide(image, bg, scale=255)
out_binary = cv.threshold(out_gray, 0, 255, cv.THRESH_OTSU)[1]
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])
returns '10°37'02"'
Upvotes: 1