0
点赞
收藏
分享

微信扫一扫

OpenCV-对银行卡字符识别

前言

#案例来源Fu Xianjun. All Rights Reserved,对银行卡字符识别

一、原图

 二、识别

import cv2
import numpy as np
def sort_contours(cnts, method = "left-to-right"):
    reverse = False
    i = 0
    if method == "left-to-right" or method == "bottom-to-top":
        reverse = True
    if method == "top-to-bottom" or method == "bottom-to-top":
        i = 1
    boundingBoxes = [cv2.boundingRect(cnt) for cnt in cnts]
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),key = lambda b: b[1][i],reverse = reverse))
    return cnts,boundingBoxes
def cv_show(name,img):
    cv2.imshow(name,img) 
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
template = cv2.imread("ocr_a_reference.png")
# cv_show("template",template)
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) cv_show("template_gray",template_gray)
ret, template_binary = cv2.threshold(template_gray,127,255,1)
 
cv_show("template_binary",template_binary)
cnts, he = cv2.findContours(template_binary, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
 
template_rect = cv2.drawContours(template, cnts, -1,(0,0,255),2)
 
cv_show("template_rect",template_rect)
cnts = sort_contours(cnts)[0]
number = {}
 
for (i,cnt) in enumerate(cnts):
    (x,y,w,h) = cv2.boundingRect(cnt)
    roi = template_rect[y:y+h, x:x+w]
    roi = cv2.resize(roi, (57,88))
    number[i] = roi
   
cv_show(f"number1",number[3])

 

举报

相关推荐

0 条评论