OpenCV--对银行卡背景处理

一、 前言

二、 操作步骤

1.原图

在这里插入图片描述

2.处理图片

#案例来源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])

3.展示结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值