python - OpenCV digit OCR -
i trying build simple sudoku grabber scans sudoku image , extracts digits in order (0
blank). have extracted 81 box approximately.
screen_shot of cells 9x9
code
import cv2 import numpy np import cv2.cv 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 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 , 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='' x in range (0,9) : 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()
things want do:
as see there parts of grid in extracted cells, want extract exact digit part ocr purpose.
i tried knn results wrong due blank spaces.
i tried tesseract failed too
http://opencvpython.blogspot.in/2012/06/sudoku-solver-part-1.html followed tutorial!
i want iterate 81 cells , extract digit parts.
Comments
Post a Comment