OpenCV MSER检测文本区域 - Python

26

我有一张发票图像,想要检测上面的文字。所以我计划采用两步来完成:首先是识别文本区域,然后使用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示例C ++示例,但如果我将它们转换成Python,那需要很长时间。是否有使用OpenCV的Python示例,或者有人可以帮助我解决这个问题吗?

嘿,你能解决它了吗? - Oer
2个回答

26

以下是代码

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

1
这个答案对我很有帮助。但是,我很困惑为什么要使用 reshape(-1, 1, 2) 调用。它返回一个看似包含二维数据的三维数组:[[[38, 30]], [[39, 30]], [[40, 30]],...]。据我所知,省略该调用会导致与 convexHull 相同的输出结果。因此,我的问题是:它有什么作用? - ideaboxer
@ideaboxer,你明白这里的.reshape是做什么用的吗? - Shreyas Moolya
有没有办法将此答案转换为检测文本框、按钮、单选框、复选框、组合框等的方法?这对我来说真的很有帮助。 - Ashish Johnson

4

这是一篇旧帖子,但我想贡献一下,如果您正在尝试从图像中提取所有文本,这里有一个代码可以将文本存入数组中。

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接