如何从图像中获取文本?



我试图从地图上获得坐标,我采取了坐标部分,我使用Pytesseract应用于它的OCR,但我无法获得坐标。这是图片的链接"https://ibb.co/hVynk2b"我试了这个脚本:

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 FilesTesseract-OCRtesseract.exe'
from pytesseract import Output
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])

这似乎对我有用。我运行了你粘贴的代码,但是清理了一下:

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 FilesTesseract-OCRtesseract.exe'
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])

返回'35°21'24°'

我确实注意到,然而,pytesseract不捕捉垂直文本。在调用image_to_data时,您可以添加config参数,或者您可以简单地将图像顺时针旋转90度并再次运行:

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:UsersgunehDesktopa.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 FilesTesseract-OCRtesseract.exe'
d = pytesseract.image_to_data(out_binary, output_type=Output.DICT)
print(d['text'])

返回的10 37°02";">

最新更新