准备
- python环境
- pycharm编译器
- opencv库安装
实战
- 测试用图:


- 代码实现
import cv2 as cv
import numpy as np
card = cv.imread("D:/pics/card.png")
num = cv.imread("D:/pics/num.png")
def show_image(win_name, image_src):
cv.namedWindow(win_name, cv.WINDOW_AUTOSIZE)
cv.imshow(win_name, image_src)
def num_preInit():
show_image("num", num)
numGray = cv.cvtColor(num, cv.COLOR_BGR2GRAY)
ret, num2Value = cv.threshold(numGray, 10, 255, cv.THRESH_BINARY_INV)
contours, hierarchy = cv.findContours(num2Value, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(num, contours, -1, (0, 0, 255), 3)
single_num = {
}
minBox = [cv.boundingRect(c) for c in contours]
nsize = len(minBox)
for i in range(