佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > OpenCV簡(jiǎn)單標(biāo)準(zhǔn)數(shù)字識(shí)別的完整實(shí)例

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

熱門(mén)標(biāo)簽:美圖手機(jī) 服務(wù)器配置 銀行業(yè)務(wù) 網(wǎng)站文章發(fā)布 檢查注冊(cè)表項(xiàng) 呼叫中心市場(chǎng)需求 鐵路電話系統(tǒng) 智能手機(jī)

在學(xué)習(xí)openCV時(shí),看到一個(gè)問(wèn)答做數(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:     #使用邊緣面積過(guò)濾較小邊緣框
        [x,y,w,h] = cv2.boundingRect(cnt)   
        if  h>25 and h  30:        #使用高過(guò)濾小框和大框
            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:        #過(guò)濾一下過(guò)多邊緣框,后期可能會(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ù)為:

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

您可能感興趣的文章:
  • python opencv實(shí)現(xiàn)信用卡的數(shù)字識(shí)別
  • Python+Opencv實(shí)現(xiàn)數(shù)字識(shí)別的示例代碼
  • python基于OpenCV模板匹配識(shí)別圖片中的數(shù)字
  • 詳解Python OpenCV數(shù)字識(shí)別案例

標(biāo)簽:上海 滄州 沈陽(yáng) 樂(lè)山 新疆 長(zhǎng)治 河南 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《OpenCV簡(jiǎn)單標(biāo)準(zhǔn)數(shù)字識(shí)別的完整實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    类乌齐县| 通化县| 五寨县| 绿春县| 靖边县| 通渭县| 锦屏县| 宁津县| 蓬莱市| 建昌县| 柏乡县| 安塞县| 永胜县| 桑植县| 岳普湖县| 海宁市| 资中县| 吉水县| 晋宁县| 禹城市| 招远市| 奉节县| 武宣县| 仁怀市| 定西市| 涿鹿县| 新昌县| 霸州市| 报价| 屏东市| 沙坪坝区| 中牟县| 三明市| 财经| 黄石市| 类乌齐县| 新邵县| 玉溪市| 南昌县| 菏泽市| 隆回县|