2016-10-17 167 views
9

私は請求書イメージを持っており、その上にテキストを検出したい。だから私は2つのステップを使用する予定です:まずテキスト領域を特定し、OCRを使用してテキストを認識します。OpenCV MSERがテキスト領域を検出する - Python

私はOpenCV 3.0をPythonで使っています。私はテキスト(非テキスト領域を含む)を特定することができますが、イメージからテキストボックスを特定したい(非テキスト領域も除く)。 、私はテキストボックスを識別し、かつ/ unidentify以外を削除したい今

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 

Originalと出力は次のようになります:

私の入力画像はありProcessed と私は、このために以下のコードを使用しています請求書のテキスト領域。私はOpenCVを初め、Pythonの初心者です。私はMATAB exampleC++ exampleでいくつかの例を見つけることができますが、私はそれらをpythonに変換すると、時間がかかります。

OpenCVを使用したPythonの例はありますか?誰かがこれを手伝ってくれますか?以下は

+0

のですか? – Oer

答えて

2

は、あなたがそれを把握することができましたねえコード インポートパッケージ

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) 
関連する問題