Ami*_*dan 21 python ocr opencv image-processing
我有发票图片,我想检测上面的文字.所以我计划使用两个步骤:首先是识别文本区域,然后使用OCR识别文本.
我在python中使用OpenCV 3.0.我能够识别文本(包括一些非文本区域),但我还想从图像中识别文本框(也不包括非文本区域).
img = cv2.imread('/home/mis/Text_Recognition/bill.jpg')
mser = cv2.MSER_create()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Converting to GrayScale
gray_img = img.copy()
regions = mser.detectRegions(gray, None)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(gray_img, hulls, 1, (0, 0, 255), 2)
cv2.imwrite('/home/mis/Text_Recognition/amit.jpg', gray_img) #Saving
Run Code Online (Sandbox Code Playgroud)
现在,我想识别文本框,并删除/取消识别发票上的任何非文本区域.我是OpenCV的新手,也是Python的初学者.我能够在MATAB示例和C++示例中找到一些示例,但如果我将它们转换为python,则需要花费大量时间.
有没有使用OpenCV的python的例子,或者任何人都可以帮助我吗?
小智 15
下面是代码导入包
# Import packages
import cv2
import numpy as np
#Create MSER object
mser = cv2.MSER_create()
#Your image path i-e receipt path
img = cv2.imread('/home/rafiullah/PycharmProjects/python-ocr-master/receipts/73.jpg')
#Convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
#detect regions in gray scale image
regions, _ = mser.detectRegions(gray)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))
cv2.imshow('img', vis)
cv2.waitKey(0)
mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
for contour in hulls:
cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)
#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("text only", text_only)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)