欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV簡單標(biāo)準(zhǔn)數(shù)字識(shí)別的完整實(shí)例

 更新時(shí)間:2021年09月09日 11:40:51   作者:huang_nansen  
這篇文章主要給大家介紹了關(guān)于OpenCV簡單標(biāo)準(zhǔn)數(shù)字識(shí)別的相關(guān)資料,要通過opencv 進(jìn)行數(shù)字識(shí)別離不開訓(xùn)練庫的支持,需要對(duì)目標(biāo)圖片進(jìn)行大量的訓(xùn)練,才能做到精準(zhǔn)的識(shí)別出目標(biāo)數(shù)字,需要的朋友可以參考下

在學(xué)習(xí)openCV時(shí),看到一個(gè)問答做數(shù)字識(shí)別,里面配有代碼,應(yīng)用到了openCV里面的ml包,很有學(xué)習(xí)價(jià)值。

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)   #先轉(zhuǎn)換為灰度圖才能夠使用圖像閾值化
 
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)  #自適應(yīng)閾值化
 
##################      Now finding Contours         ###################
# 
image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
#邊緣查找,找到數(shù)字框,但存在誤判
 
samples =  np.empty((0,900))    #將每一個(gè)識(shí)別到的數(shù)字所有像素點(diǎn)作為特征,儲(chǔ)存到一個(gè)30*30的矩陣內(nèi)
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:        #過濾一下過多邊緣框,后期可能會(huì)嘗試極大抑制
                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()

訓(xùn)練數(shù)據(jù)為:

測試數(shù)據(jù)為:

使用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)
    
    #預(yù)處理一下
    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')

總結(jié)

到此這篇關(guān)于OpenCV簡單標(biāo)準(zhǔn)數(shù)字識(shí)別的文章就介紹到這了,更多相關(guān)OpenCV標(biāo)準(zhǔn)數(shù)字識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python模塊中pip命令的基本使用

    python模塊中pip命令的基本使用

    這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的示例解析,在同樣在進(jìn)行python機(jī)器學(xué)習(xí)的同學(xué)可以借鑒參考下,希望能夠有所幫助
    2021-10-10
  • python中的字典操作及字典函數(shù)

    python中的字典操作及字典函數(shù)

    本篇文章給大家介紹了python中的字典,包括字典的操作,字典函數(shù)實(shí)現(xiàn)代碼,需要的朋友參考下吧
    2018-01-01
  • python3調(diào)用ansible?api使用實(shí)例例說明

    python3調(diào)用ansible?api使用實(shí)例例說明

    這篇文章主要為大家介紹了python3?調(diào)用ansible?api使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 在python帶權(quán)重的列表中隨機(jī)取值的方法

    在python帶權(quán)重的列表中隨機(jī)取值的方法

    今天小編就為大家分享一篇在python帶權(quán)重的列表中隨機(jī)取值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python每天必學(xué)之bytes字節(jié)

    Python每天必學(xué)之bytes字節(jié)

    Python每天必學(xué)之bytes字節(jié),針對(duì)Python中的bytes字節(jié)進(jìn)行學(xué)習(xí)理解,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Pytorch中torch.repeat_interleave()函數(shù)使用及說明

    Pytorch中torch.repeat_interleave()函數(shù)使用及說明

    這篇文章主要介紹了Pytorch中torch.repeat_interleave()函數(shù)使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • python?PyQt5中QButtonGroup的詳細(xì)用法解析與應(yīng)用實(shí)戰(zhàn)記錄

    python?PyQt5中QButtonGroup的詳細(xì)用法解析與應(yīng)用實(shí)戰(zhàn)記錄

    在PyQt5中,QButtonGroup是一個(gè)用于管理按鈕互斥性和信號(hào)槽連接的類,它可以將多個(gè)按鈕劃分為一個(gè)組,管理按鈕的選中狀態(tài)和ID,本文詳細(xì)介紹了QButtonGroup的創(chuàng)建、使用方法和實(shí)際應(yīng)用案例,適合需要在PyQt5項(xiàng)目中高效管理按鈕組的開發(fā)者
    2024-10-10
  • 詳解Python中靜態(tài)類型語言的寫法

    詳解Python中靜態(tài)類型語言的寫法

    眾所周知,python是一種動(dòng)態(tài)類型語言,但是,從v3.6版本開始,開始支持靜態(tài)類型的寫法,下面小編就來和大家聊聊Python中靜態(tài)類型語言的具體寫法吧
    2023-07-07
  • Centos5.x下升級(jí)python到python2.7版本教程

    Centos5.x下升級(jí)python到python2.7版本教程

    這篇文章主要介紹了Centos5.x下升級(jí)python到python2.7版本教程,本文使用編譯安裝方式,并配置了一系列需要更改的配置項(xiàng),需要的朋友可以參考下
    2015-02-02
  • 簡單了解Django模板的使用

    簡單了解Django模板的使用

    這篇文章主要介紹了簡單了解Django模板的使用,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12

最新評(píng)論