【问题标题】:OpenCV MSER detect text areas - PythonOpenCV MSER 检测文本区域 - Python
【发布时间】:2017-02-25 23:29:55
【问题描述】:

我有一张发票图片,我想检测上面的文字。所以我打算用2个步骤:首先是识别文本区域,然后使用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

现在,我想识别文本框,并删除/取消识别发票上的任何非文本区域。我是 OpenCV 的新手,也是 Python 的初学者。我可以在MATAB exampleC++ example 中找到一些示例,但是如果我将它们转换为python,我会花费很多时间。

有没有使用 OpenCV 的 python 示例,或者有人可以帮助我吗?

【问题讨论】:

  • 嘿,你知道了吗?

标签: python opencv image-processing ocr


【解决方案1】:

下面是代码

# 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)

【讨论】:

  • 这个答案对我很有帮助。但我对为什么使用reshape(-1, 1, 2) 调用的问题感到困扰。它返回一个看似包含二维数据的 3 维数组:[[[38, 30]], [[39, 30]], [[40, 30]], ...]。据我了解,省略该调用会导致convexHull 的相同输出。因此我的问题是:它有什么用?
  • @ideaboxer 你知道这里的.reshape 是做什么的吗?
  • 有没有办法将此答案转换为检测文本框、按钮、单选按钮、复选框、组合框等?这对我真的很有帮助。
【解决方案2】:

这是一篇旧帖子,但如果您尝试从图像中提取所有文本,我想贡献一下,这里是在数组中获取该文本的代码。

import cv2
import numpy as np
import re
import pytesseract
from pytesseract import image_to_string
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
from PIL import Image

image_obj = Image.open("screenshot.png")

rgb = cv2.imread('screenshot.png')
small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

#threshold the image
_, bw = cv2.threshold(small, 0.0, 255.0, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)

# get horizontal mask of large size since text are horizontal components
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)

# find all the contours
contours, hierarchy,=cv2.findContours(connected.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#Segment the text lines
counter=0
array_of_texts=[]
for idx in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[idx])
    cropped_image = image_obj.crop((x-10, y, x+w+10, y+h ))
    str_store = re.sub(r'([^\s\w]|_)+', '', image_to_string(cropped_image))
    array_of_texts.append(str_store)
    counter+=1

print(array_of_texts)

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2018-06-17
    • 1970-01-01
    • 2017-12-11
    • 2011-09-03
    • 1970-01-01
    • 2016-10-12
    • 2014-06-15
    • 2011-10-19
    相关资源
    最近更新 更多