def xrdTyp(img,typ,db,trb): '''rdTyp finds the screen area of a digit based on the x y co-ordinates in the tables below. It passes this area to tMatch which returns a number ''' #img = stdSize(imgx,typ) img = erode(img,3) img = dilate(img,5) d = 150 #cvs(1,imgx,'rdTyp input') hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # define range of blue color in HSV h = 46; s = 20; v = 212 lower_blue = np.array([h,s,v]) #np.array([110,50,50]) upper_blue = np.array( [h+d,s+d,v+d]) #np.array([130,255,255]) # Threshold the HSV image to get only blue colors thresh = cv2.inRange(hsv, lower_blue, upper_blue) img = thresh.copy() Y= { # y1 y2 j limit 'wt' : [ 20, 320 , 4 ], 'fat': [ 35, 220 , 3 ], 'h2o': [ 30, 220 , 3 ] } XX = { # 100 10 one tenth 'wt' : [(95, 145) , (165,265) , (280,385 ), (400,510) ], 'fat': [(15, 50 ) , (65,125) , (140,200), (0,0) ], 'h2o': [(5, 55) , (65,120 ) , (135,185), (0,0) ] } n = [] h,w = img.shape #print 'id input w {} h {}'.format(w,h) # look at each digit in the image by xx position for j in range(0,Y[typ][2]): # loops across XX table above y1 = Y[typ][0]; x1 = XX[typ][j][0] y2 = Y[typ][1]; x2 = XX[typ][j][1] digit = img[y1:y2, x1:x2].copy() cv2.imwrite('digTest.png',digit) # save for future debug h,w = digit.shape #print 'xid input w {} h {}'.format(w,h) #cvs(db,digit,'input digit') # n.append( tMatch(digit,typ,db)) # interpret as a number if db: print 'n is ', n n.append(identifyN(digit,trb,0,db,typ) ) # train and test if db: print ' rdTyp n ', n # exit here nn = 0; j = -1 n.reverse() #nn = 100 * n[1] + 10 * n[2] + n[3] + n[4]/10.0 for j, xin in enumerate(n): nn = nn + xin * 10**(j-1) cvs(db,img,typ,50) return(nn)
def findBlobs(imx,ms,mx,erd,db,tval=127): Erd = erd Drd = int(Erd/2) print 'This is find blobs 1.0 ms {} mx {} erd {} drd {}'.format( ms, mx , Erd ,Drd) imgray = cv2.cvtColor(imx,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,tval,255,0) thresh = erode(thresh,Erd) thresh = dilate(thresh,Drd) #print 'db findBlobs' , db cvs(db,thresh) cmask = thresh.copy() im3,cnt, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # note lamda is expression for leftmost extreme of the contour scon = sorted(cnt, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) rvl = [] for con in scon: area = cv2.contourArea(con) #if db: print area, cnt_len = cv2.arcLength(con, True) cntp = cv2.approxPolyDP(con, 0.02*cnt_len, True) # print len(cntp) # look for suitable contours x,y,w,h = cv2.boundingRect(con) asp = abs( w - h ) #if db: print 'abs|w-h| {} w {} h {} '.format(asp,w,h) if ( len(cntp) > 3 # at least 4 and asp > .1 # ie not round or square and area > ms and area < mx ): # note ( ... ) for implied continuation rvl.append(con) cv2.drawContours(imx,[con], 0, (0,255,255), 2) # yellow else: cv2.drawContours(imx,[con], 0, (0,0,255), 2) # red # if db:cvs(db,imx,t=0) return (rvl,cmask)