OpenCV Python 2 数字识别(K近邻)
我使用OpenCV中的KNearest來(lái)實(shí)現(xiàn)簡(jiǎn)單的文字識(shí)別OCR。下面是我實(shí)現(xiàn)的步驟,學(xué)習(xí)學(xué)習(xí)
以下是我的訓(xùn)練數(shù)據(jù)圖:
?
(訓(xùn)練數(shù)據(jù)量較少。所有的字母都是相同的字體和大小)。
我用OpenCV編寫(xiě)了一個(gè)小代碼處理數(shù)據(jù):
a)加載圖像。
(B)選擇數(shù)字(是通過(guò)輪廓查找并對(duì)字母的面積和高度施加約束來(lái)避免錯(cuò)誤檢測(cè))。
(C)給字母繪制邊框,并自己按數(shù)字鍵與圖中一致
(D)一旦按下相應(yīng)的數(shù)字鍵,它將該框調(diào)整為10x10,并在一個(gè)數(shù)組中保存100個(gè)像素值(此處為示例),并在另一個(gè)數(shù)組中保存相應(yīng)的手動(dòng)輸入數(shù)字
然后將兩個(gè)數(shù)組保存在單獨(dú)的txt文件中。
在手動(dòng)數(shù)字分類結(jié)束時(shí),訓(xùn)練數(shù)據(jù)(TRAIN.png)中的所有數(shù)字都由我們手工標(biāo)記,圖像如下所示:
?
下面是用于上述處理的代碼:
import sysimport numpy as np
import cv2im = cv2.imread('pitrain.png')
im3 = im.copy()gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)################# Now finding Contours ###################contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)samples = np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]for cnt in contours:if cv2.contourArea(cnt)>50:[x,y,w,h] = cv2.boundingRect(cnt)if h>28: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,(10,10))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,100))samples = np.append(samples,sample,0)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)
現(xiàn)在進(jìn)入訓(xùn)練和測(cè)試部分。
在測(cè)試部分,我使用了下面的圖像,它的字母類型是一樣的
?
關(guān)于訓(xùn)練
(A)加載我們前面已經(jīng)保存的txt文件
(B)創(chuàng)建分類器實(shí)例(在這里,它是KNeest)
(C)然后使用KNearest.TRANS函數(shù)對(duì)數(shù)據(jù)進(jìn)行訓(xùn)練。
關(guān)于測(cè)試
(A)我們加載用于測(cè)試的圖像
(B)像以前一樣處理圖像,并使用輪廓法提取每一個(gè)數(shù)字。
c)為其繪制邊框,然后調(diào)整大小為10x10,并像前面所做的那樣將其像素值存儲(chǔ)在數(shù)組中。
(D)然后我們使用 KNearest.find_nearest()函數(shù)的查找最近項(xiàng),以找到與我們提供的項(xiàng)最接近的項(xiàng)。
我將最后兩個(gè)步驟(訓(xùn)練和測(cè)試)放在下面的代碼中:
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.KNearest()
model.train(samples,responses)############################# testing part #########################im = cv2.imread('pi.png')
out = np.zeros(im.shape,np.uint8)
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)for cnt in contours:if cv2.contourArea(cnt)>50:[x,y,w,h] = cv2.boundingRect(cnt)if h>28: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,(10,10))roismall = roismall.reshape((1,100))roismall = np.float32(roismall)retval, results, neigh_resp, dists = model.find_nearest(roismall, k = 1)string = str(int((results[0][0])))cv2.putText(out,string,(x,y+h),0,1,(0,255,0))cv2.imshow('im',im)
cv2.imshow('out',out)
cv2.waitKey(0)
得到結(jié)果:
?
這里,精度為100%
總結(jié)
以上是生活随笔為你收集整理的OpenCV Python 2 数字识别(K近邻)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 红警2Linux版本
- 下一篇: knn 进行手写数字识别