一、银行卡号提取
先将导入的图片进行高斯模糊,二值化 膨胀 和腐蚀等操作。
再根据银行卡号的样式,从照片中筛选出卡号。
二、代码
1.引入库
代码如下(示例):
import cv2 as cv
import numpy as np
def image_process(file_path):
img = cv.imread(file_path, 0)
blur = cv.GaussianBlur(img, (3, 3), 0) #高斯模糊
ret, binary = cv.threshold(blur, 50, 255, cv.THRESH_BINARY) #二值化
kernel = np.ones((1, 50), np.uint8)
erosion = cv.erode(binary, kernel) # 膨胀
dilation = cv.dilate(erosion, kernel) # 腐蚀
contours, hierarchy = cv.findContours(dilation, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
sp = dilation.shape
for i in range(0, len(contours)):
x, y, w, h = cv.boundingRect(contours[i])
if h > sp[0]*0.05 and w > sp[1]*0.5 and y > sp[0]*0.2 and y < sp[0]*0.8 and w/h > 5:
img = binary[y:y + h, x:x + w]
cv.imshow("kahao", img)
break
src = cv.imread("D:\\1111.png")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input iamge",src)
image_process("D:\\1111.png")
cv.waitKey(0)
cv.destroyAllWindows()
总结
在卡号筛选时,要将筛选的样式设置为高度大于整体图片的0.05,宽度大于照片的0.5,且小于整体照片的0.8,宽比高大于5.这样才能从照片中筛选出银行卡号。