如何使用OpenCV实现标准数字识别功能
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章给大家分享的是有关如何使用OpenCV实现标准数字识别功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。import sysimport numpy as npim
千家信息网最后更新 2026年11月04日如何使用OpenCV实现标准数字识别功能
这篇文章给大家分享的是有关如何使用OpenCV实现标准数字识别功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
import sysimport numpy as npimport cv2 im = cv2.imread('t.png')im3 = im.copy() gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #先转换为灰度图才能够使用图像阈值化 thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) #自适应阈值化 ################## Now finding Contours #################### image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)#边缘查找,找到数字框,但存在误判 samples = np.empty((0,900)) #将每一个识别到的数字所有像素点作为特征,储存到一个30*30的矩阵内responses = [] #labelkeys = [i for i in range(48,58)] #48-58为ASCII码count =0for cnt in contours: if cv2.contourArea(cnt)>80: #使用边缘面积过滤较小边缘框 [x,y,w,h] = cv2.boundingRect(cnt) if h>25 and h < 30: #使用高过滤小框和大框 count+=1 cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2) roi = thresh[y:y+h,x:x+w] roismall = cv2.resize(roi,(30,30)) cv2.imshow('norm',im) key = cv2.waitKey(0) if key == 27: # (escape to quit) sys.exit() elif key in keys: responses.append(int(chr(key))) sample = roismall.reshape((1,900)) samples = np.append(samples,sample,0) if count == 100: #过滤一下过多边缘框,后期可能会尝试极大抑制 breakresponses = np.array(responses,np.float32)responses = responses.reshape((responses.size,1))print ("training complete") np.savetxt('generalsamples.data',samples)np.savetxt('generalresponses.data',responses)#cv2.waitKey()cv2.destroyAllWindows()训练数据为:

测试数据为:
使用openCV自带的ML包,KNearest算法
import sysimport cv2import numpy as np ####### training part ############### samples = np.loadtxt('generalsamples.data',np.float32)responses = np.loadtxt('generalresponses.data',np.float32)responses = responses.reshape((responses.size,1)) model = cv2.ml.KNearest_create()model.train(samples,cv2.ml.ROW_SAMPLE,responses) def getNum(path): im = cv2.imread(path) out = np.zeros(im.shape,np.uint8) gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #预处理一下 for i in range(gray.__len__()): for j in range(gray[0].__len__()): if gray[i][j] == 0: gray[i][j] == 255 else: gray[i][j] == 0 thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2) image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) count = 0 numbers = [] for cnt in contours: if cv2.contourArea(cnt)>80: [x,y,w,h] = cv2.boundingRect(cnt) if h>25: cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2) roi = thresh[y:y+h,x:x+w] roismall = cv2.resize(roi,(30,30)) roismall = roismall.reshape((1,900)) roismall = np.float32(roismall) retval, results, neigh_resp, dists = model.findNearest(roismall, k = 1) string = str(int((results[0][0]))) numbers.append(int((results[0][0]))) cv2.putText(out,string,(x,y+h),0,1,(0,255,0)) count += 1 if count == 10: break return numbers numbers = getNum('1.png')感谢各位的阅读!关于"如何使用OpenCV实现标准数字识别功能"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
数字
边缘
功能
标准
内容
数据
更多
篇文章
阈值
不错
实用
像素
图像
文章
灰度
特征
看吧
知识
矩阵
算法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
高科技互联网
朗新科技与工业互联网
科技服务与互联网
万易互联网科技
九亚互联网科技
互联网女皇 腾讯科技
互联网金融监管科技
互联网巨头投资的硬核科技
上海电发互联网科技有限公司
北京三清互联网科技
互联网系的金融科技公司
宁夏众和广鑫互联网科技有限公司
互联网科技革命什么开始
千方科技属于互联网企业吗
互联网科技扶贫时间
每天都有哪些好的互联网科技
工业互联网科技创业
互联网科技包装股
银川新华互联网科技学校原画
知域互联网科技有限公司官网
互联网科技公司官网
互联网 新科技
卫星互联网科技股票
昭通云南众福互联网科技有限公司
互联网科技商业
广西互联网科技孵化中心
保险科技服务互联网营销体系
互联网金融科技公司的优势
互联网科技数字货币
互联网领先科技活动