服务器之家

服务器之家 > 正文

OpenCV简单标准数字识别的完整实例

时间:2022-01-04 00:07     来源/作者:huang_nansen

在学习openCV时,看到一个问答做数字识别,里面配有代码,应用到了openCV里面的ml包,很有学习价值。

https://stackoverflow.com/questions/9413216/simple-digit-recognition-ocr-in-opencv-python#

import sys
import numpy as np
import 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 = []                  #label
keys = [i for i in range(48,58)]    #48-58为ASCII码
count =0
for 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:        #过滤一下过多边缘框,后期可能会尝试极大抑制
              break
responses = 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简单标准数字识别的完整实例

测试数据为:

OpenCV简单标准数字识别的完整实例

使用openCV自带的ML包,KNearest算法

import sys
import cv2
import 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简单标准数字识别的完整实例

总结

到此这篇关于OpenCV简单标准数字识别的文章就介绍到这了,更多相关OpenCV标准数字识别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/huang_nansen/article/details/83241143

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部