OpenCV数字OCR

时间:2015-08-07 18:50:59

标签: python opencv ocr knn

我正在尝试构建一个简单的数独抓取器来扫描数独图像并按顺序提取数字(0为空白)。我已成功提取了81个盒子。

SCREEN_SHOT单元格9x9

http://i.stack.imgur.com/X2t6U.png

CODE

import cv2
import numpy as np 
import cv2.cv as cv
import tesseract



def rectify(h):
        h = h.reshape((4,2))
        hnew = np.zeros((4,2),dtype = np.float32)

        add = h.sum(1)
        hnew[0] = h[np.argmin(add)]
        hnew[2] = h[np.argmax(add)]

        diff = np.diff(h,axis = 1)
        hnew[1] = h[np.argmin(diff)]
        hnew[3] = h[np.argmax(diff)]

        return hnew

img=cv2.imread('sudoku.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)


contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

biggest = None
max_area = 0
for i in contours:
        area = cv2.contourArea(i)
        if area > 100:
                peri = cv2.arcLength(i,True)
                approx = cv2.approxPolyDP(i,0.02*peri,True)
                if area > max_area and len(approx)==4:
                        biggest = approx
                        max_area = area
cv2.drawContours(img, biggest, -1, (0,255,0), 8)

biggest=rectify(biggest)

h = np.array([ [0,0],[449,0],[449,449],[0,449] ],np.float32)
retval = cv2.getPerspectiveTransform(biggest,h)
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)

warp = cv2.warpPerspective(thresh,retval,(450,450))

x=0
string=''
for x in range (0,9) :
        for y in range (0,9):
            morph=warp[(49*y):((49*y)+50),(49*x):((49*x)+50)]



            cv2.imwrite('sudokuDigits/cell'+str(x)+str(y)+'.jpg',morph)

            print str(x)+str(y)+" >> "+ string


cv2.imshow('frame',thresh)
cv2.imshow('wraped_ROI',warp)

cv2.waitKey(0)
cv2.destroyAllWindows()

我想做的事情:

  1. 如您所见,提取的单元格中有网格的一部分,我想要提取 用于OCR目的的确切数字部分。

  2. 我尝试了kNN但是由于空格而导致大多数结果错误。

  3. 我尝试了tesseract,但也失败了

  4. http://opencvpython.blogspot.in/2012/06/sudoku-solver-part-1.html遵循本教程!

  5. 我想迭代所有81个单元格并提取数字部分。

0 个答案:

没有答案