赞
踩
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) # 二值化,10为阈值,THRESH_BINARY_INV:反转二值 ret, num2Value = cv.threshold(numGray, 10, 255, cv.THRESH_BINARY_INV) # 对字模轮廓检测:cv.RETR_EXTERNAL只检测外轮廓 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。