@ Fu Xianjun. All Rights Reserved.
前言
随着人工智能的不断发展,OpenCv这门技术也越来越重要,很多人都开始学习OpenCv,本文就介绍了如何使用OpenCv进行数字识别。
提示:以下是本篇文章正文内容,下面案例可供参考
一、数字识别需要应用到哪些内容?
数字识别需要灵活的运用阈值处理,轮廓检测,边缘及模板匹配
二、使用步骤
1.引入库
代码如下(示例):
import cv2
import numpy as py
def cv_show(name,img):#定义一个函数,显示图片
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def sort_contours(cnts,method = 'left-to-right'):
reverse = False
i = 0
if method = 'right-to-left' or method = 'bottom-to-top':
reverse = True
if method = 'top-to-bottom' or method = 'bottom-to-top':
i = 1
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts,boudingBoxes) = zip(*sorted(zip(cnts,boudingBoxes),
key = lambda b:v[1][i],reverse = reverse))
return cnts,boudingBoxes
2.读入数据
代码如下(示例):
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