问题描述
我正在执行一项任务,目的是从支票图像
中提取帐号
.我目前的方法可以分为2个步骤
I am working on a task to extract the account number
from cheque images
. My current approach can be divided into 2 steps
- 本地化帐号数字(印刷数字)
- 使用像
Tesseract OCR
这样的OCR库执行OCR
- Localize account number digits (Printed digits)
- Perform OCR using OCR libraries like
Tesseract OCR
第二步很简单,假设我们已经正确定位了帐号数字
The second step is straight forward assuming we have properly localized the account number digits
我尝试使用 OpenCV
等高线方法和 MSER
(最大稳定的末梢区域)来定位帐号数字,但是没有得到有用的结果.很难归纳模式,因为
I tried to localize account number digits using OpenCV
contours methods and using MSER
(Maximally stable extremal regions) but didn’t get useful results. It’s difficult to generalize pattern because
- 不同的银行支票的模板有所不同
- 帐号位置不固定
我们如何解决这个问题.我是否需要寻找一些基于 Deep learning
的方法.
How can we approach this problem. Do I have to look for some deep learning
based approaches.
样本图像
推荐答案
假设帐号具有唯一的紫色文本颜色,我们可以使用颜色阈值设置.这个想法是将图像转换为HSV颜色空间,然后定义一个较低/较高的颜色范围,并使用 cv2.inRange()
进行颜色阈值处理.从这里,我们按轮廓区域过滤以消除小噪声.最后我们将图像反转,因为我们希望文本为黑色,背景为白色.最后一步是在将图像丢入Pytesseract之前对图像进行高斯模糊处理.结果如下:
Assuming the account number has the unique purple text color, we can use color thresholding. The idea is to convert the image to HSV color space then define a lower/upper color range and perform color thresholding using cv2.inRange()
. From here we filter by contour area to remove small noise. Finally we invert the image since we want the text in black with the background in white. One last step is to Gaussian blur the image before throwing it into Pytesseract. Here's the result:
Pytesseract的结果
Result from Pytesseract
30002010108841
代码
import numpy as np
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image = cv2.imread('1.png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([103,79,60])
upper = np.array([129,255,255])
mask = cv2.inRange(hsv, lower, upper)
cnts = cv2.findContours(mask, 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 < 10:
cv2.drawContours(mask, [c], -1, (0,0,0), -1)
mask = 255 - mask
mask = cv2.GaussianBlur(mask, (3,3), 0)
data = pytesseract.image_to_string(mask, lang='eng',config='--psm 6')
print(data)
cv2.imshow('mask', mask)
cv2.waitKey()
这篇关于如何在支票/支票图像中提取帐号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!