OpenCVのとOCR用のテキストボックスを削除します

Suhail Prasathong:

私は、次の形式の文書にOCR(使用してGoogleのたTesseract)を実行しようとしています:

ここでは、画像の説明を入力します。

しかし、たTesseractは、文字/数字(LまたはIまたは1)との間でショートバーを前提としています。

前処理手段として、私は、次のコードを使用して、垂直方向と水平方向の行を削除しようとしました。

import cv2

from pdf2image import convert_from_path

pages = convert_from_path('..\\app\\1.pdf', 500)
for page in pages:
    page.save('..\\app\\out.jpg', 'JPEG')

image = cv2.imread('out.jpg')
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(result, [c], -1, (255,255,255), 5)

# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(result, [c], -1, (255,255,255), 5)

cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.imwrite('result.png', result)
cv2.waitKey()

私は、この文書の出力は、間に小さなバー下のではなく、画像の左側と右側にも、スタートとフィニッシュライン文書中の垂直および水平線のほとんどが削除され、問題に遭遇します。

私はプロセスを事前と行を削除しようとすることで、この間違っについてんだ場合、私は思ったんだけど。より良い前処理する方法や、この問題を解決するための別の方法はありますか?

nathancy:

フォームフィールドは、文字とは別であることを観察すると、あなたは単にテキスト文字を隔離するために輪郭領域を使用してフィルタリングすることができます。アイデアはあるにガウスぼかし、その後、大津のしきい値バイナリイメージを取得します。ここからは、輪郭見つけ使用して、フィルター輪郭領域をいくつかの所定の閾値とを。私たちは、効果的に等高線に描画することによって行を削除することができますcv2.drawContours


バイナリイメージ

ここでは、画像の説明を入力します。

削除された行

ここでは、画像の説明を入力します。

OCRのための準備ができて反転

ここでは、画像の説明を入力します。

使用してOCR結果Pytesseractを

HELLO

コード

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

# Load image, grayscale, blur, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours and filter using contour area
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 500:
        cv2.drawContours(thresh, [c], -1, 0, -1)

# Invert image and OCR
invert = 255 - thresh
data = pytesseract.image_to_string(invert, lang='eng',config='--psm 6')
print(data)

cv2.imshow('thresh', thresh)
cv2.imshow('invert', invert)
cv2.waitKey()

注:あなたはまだ、水平/垂直線が近づいて削除して行きたい場合は、垂直カーネルのサイズを変更する必要があります。例えば、変更(1,40)します(1,10)これは、小さな行を削除するのに役立ちますが、それはまた、同様にテキストの縦のラインの一部を削除することができますL

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=14701&siteId=1