opencv 如何识别支票页数据

hgtggwj0  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(90)

你好。我正试着从下面的支票页上辨认出印刷和手写的文本

这里是预处理后的图像,下面使用的代码

import cv2 
import pytesseract
import numpy as np

img = cv2.imread('Images/cheque_leaf.jpg')

# Rescaling the image (it's recommended if you’re working with images that have a DPI of less than 300 dpi)
img = cv2.resize(img, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
h, w = img.shape[:2]

# By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
# we need to convert from BGR to RGB format/mode:
# it to reduce noise
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]  # perform OTSU threhold
thresh = cv2.rectangle(thresh, (0, 0), (w, h), (0, 0, 0), 2) # draw a rectangle around regions of interest in an image

# Dilates an image by using a specific structuring element.
# enrich the charecters(to large)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2,2))

# The function erodes the source image using the specified structuring element that determines 
# the shape of a pixel neighborhood over which the minimum is taken
erode = cv2.erode(thresh, kernel, iterations = 1)

# To extract the text
custom_config = r'--oem 3 --psm 6'
pytesseract.image_to_string(thresh, config=custom_config)

现在使用pytesseract.image_to_string()方法将图像转换为文本。我得到的是无关输出。在上面的图像中,我想确定日期,分支收款人,数字和文字的金额以及数字签名名称,然后是帐号。
任何OCR技术通过提取如上所述的精确数据来解决上述问题。先谢谢你了

3phpmpom

3phpmpom1#

以下只是几种方法之一。
我建议使用Sauvola阈值技术。使用这里提到的特定公式为图像中的每个像素计算阈值。它涉及计算某个窗口内像素值的平均值和标准偏差。
此功能在skimage库(也称为scikit-image)中提供。
下面是给定图像的工作示例:

from skimage.filters import threshold_sauvola
img = cv2.imread('cheque.jpg', cv2.IMREAD_GRAYSCALE)

# choosing a window size of 13 (feel free to change it and visualize)
thresh_sauvola = threshold_sauvola(img, window_size=13)
binary_sauvola = img > thresh_sauvola

# converting resulting Boolean array to unsigned integer array of 8-bit (0 - 255) 
binary_sauvola_int = binary_sauvola.astype(np.uint8)
result = cv2.normalize(binary_sauvola_int, dst=None, alpha=0, beta=255,norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)

结果:

***注意:***此结果只是一个启动板,用于尝试其他图像处理技术以获得所需的结果。

相关问题