Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
opencvpython中的数字识别OCR_Python_Opencv_Numpy_Ocr - Fatal编程技术网

opencvpython中的数字识别OCR

opencvpython中的数字识别OCR,python,opencv,numpy,ocr,Python,Opencv,Numpy,Ocr,我的问题是建立一个简单的程序来检测图像中的数字,我做了一些研究,在stack上发现了这个主题,我觉得它很有教育意义,所以我想为我自己的需要使用它 我的训练数据图像如下: 我用来构建数据集的代码是:(我对阿比德·拉赫曼的代码做了一些修改,以便它能够支持我的案例) 我使用了与测试部分相同的训练数据图像,以获得最佳结果准确性,并查看我是否走上了正确的道路: import cv2 import numpy as np import collections ####### training par

我的问题是建立一个简单的程序来检测图像中的数字,我做了一些研究,在stack上发现了这个主题,我觉得它很有教育意义,所以我想为我自己的需要使用它

我的训练数据图像如下:

我用来构建数据集的代码是:(我对阿比德·拉赫曼的代码做了一些修改,以便它能够支持我的案例)

我使用了与测试部分相同的训练数据图像,以获得最佳结果准确性,并查看我是否走上了正确的道路:

import cv2
import numpy as np
import collections

#######   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('one_white_1.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)>20:
        [x,y,w,h] = cv2.boundingRect(cnt)
        if  h>=10:
            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),1,1,(0,255,0))


cv2.imshow('im',im)
cv2.imshow('out',out)
#cv2.waitKey(0)
raw_input('Tape to exit')
结果是:

正如你所看到的,这是完全错误的

我不知道我遗漏了什么,或者我的情况是否更为特殊,无法通过数字OCR系统处理

如果有人能帮我个忙

我注意到我使用的是Python2.7OpenCV2.4.11Numpy1.9和MacOS10.10.4


谢谢

我找到了正确的方法,它只需要更多的定制代码

检测计数前的相同过程:

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
不是

不是

我有99%的准确率,很好的准确率

无论如何谢谢你

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)