Пример #1
0
contour, hier = cv2.findContours(res,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

##create a list with the coordinates sorted 
centroids = []
for cnt in contour:
    
    mom = cv2.moments(cnt)
    (x,y) = int(mom['m10']/mom['m00']), int(mom['m01']/mom['m00'])
    centroids.append((x,y))

if len(centroids) != 100:
    print "Centroids: " + str(len(centroids))
    exit()


c_points = [sorted(i, key = lambda x: x[1]) for i in OCR.split_len(sorted(centroids),10)]

r_points = [ list(i) for i in zip(*c_points)]
r_points = [x for sublist in r_points for x in sublist]


###OCR Stage
print "7) OCR Stage: "
result = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]


for row in range(9):
    print "\tRow: " + str(row+1)
    for column in range(9):

        ## top left corner = x1:y1 | down right corner = x2:y2
Пример #2
0
###Final Stage###
###
result=""
size=630
size=(size,)*2
number=size[0]/9
print("OCR Stage")
import cv2.cv as cv
import OCR
for i,j in enumerate(b):	    
    ri = i/10
    ci = i%10
    if ci != 9 and ri!=9:
        src = bm[ri:ri+2, ci:ci+2 , :].reshape((4,2))
	dst = np.array( [ [ci*number,ri*number],[(ci+1)*number-1,ri*number],[ci*number,(ri+1)*number-1],[(ci+1)*number-1,(ri+1)*number-1] ], np.float32)
	retval = cv2.getPerspectiveTransform(src,dst)
	warp = cv2.warpPerspective(res2,retval,(size))
	whole_digit= warp[ri*number:(ri+1)*number-1 , ci*number:(ci+1)*number-1].copy()
	noborder_digit= whole_digit[5:64,5:64]
	gray_digit = cv2.cvtColor(noborder_digit,cv2.COLOR_BGR2GRAY)
	thresh_digit = cv2.adaptiveThreshold(gray_digit,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,0,151,80)
	image = cv.CreateImageHeader((thresh_digit.shape[1],thresh_digit.shape[0]), cv.IPL_DEPTH_8U, 1)
	cv.SetData(image, thresh_digit.tostring(), thresh_digit.dtype.itemsize*thresh_digit.shape[1])
	result+=OCR.ocr_singledigit(image)

rows = OCR.split_len(result,9)
for row in rows:
	print row
print time.time()-st
Пример #3
0
print("[6]Correcting defects")
CONTOUR, HIER = cv2.findContours(RES, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

POINTS = []
for CNT in CONTOUR:
    mom = cv2.moments(CNT)
    (x, y) = int(mom['m10']/mom['m00']), int(mom['m01']/mom['m00'])
    cv2.circle(IMG, (x, y), 4, (0, 255, 0), -1)
    POINTS.append((x, y))

if len(POINTS) != 100:
    print "[!]Centroids: " + str(len(POINTS))
    print "[!]Exiting"
    sys.exit()
    
C_POINTS = [sorted(i, key = lambda x: x[1]) for i in OCR.split_len(sorted(POINTS), 10)]

R_POINTS = [ list(i) for i in zip(*C_POINTS)]
R_POINTS = [x for sublist in R_POINTS for x in sublist]

###OCR Stage
print "[7]OCR Stage: "
RESULT = [[0]*9 for i in range(9)]


for row in range(9):
    print "\t[+]Row: " + str(row + 1)
    for column in range(9):

    ## top left corner = x1:y1 | down right corner = x2:y2
        x1, x2, y1, y2 = OCR.getcorners((row, column), R_POINTS)