def rdWeight(imgx,typ,db): img = stdSize(imgx,typ) imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) img = thresh.copy() cvs(1,img,'x') y1 = 45; y2 = y1 + 322; x1 = 152; x2=200 wtx00 = img[y1:y2, x1:x2].copy() w100 = tMatch(wtx00,typ,db) # search for a number # cvs(1,wtx00,'y') y1 = 45; y2 = y1 + 322; x1 = 240; x2=360 wtx01 = img[y1:y2, x1:x2].copy() # cvs(1,wtx01,'y') w010 = tMatch(wtx01,typ,db) # search for a number y1 = 45; y2 = y1 + 322; x1 = 405; x2=535 wtx02 = img[y1:y2, x1:x2].copy() cvs(0,wtx02,'yy') w001 = tMatch(wtx02,typ,db) y1 = 45; y2 = y1 + 322; x1 = 580; x2=720 wtx03 = img[y1:y2, x1:x2].copy() cvs(0,wtx03,'yy') wp1 = tMatch(wtx03,typ,db) print w100, w010, w001, wp1 n = 100 * w100 + 10 * w010 + w001 + float(wp1 / 10.0) print n
def rdTyp(imgx,typ,db): '''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) 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' : [ 45, 367 , 4 ], 'fat': [ 84, 344 , 3 ], 'h2o': [ 35, 230 , 3 ] } XX = { # 100 10 one tenth 'wt' : [(152, 200) , (240,360) , (415,530), (580,720) ], 'fat': [(20, 80) , (100,220) , (250,350), (0,0) ], 'h2o': [(10, 90) , (110,210) , (235,320), (0,0) ] } n = [] # 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]; y2 = Y[typ][1]; x1 =XX[typ][j][0]; x2=XX[typ][j][1] digit = img[y1:y2, x1:x2].copy() cv2.imwrite('digTest.png',digit) # save for future debug n.append( tMatch(digit,typ,db)) # interpret as a number 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,5000) return(nn) # the decoded number
def qnumb(grp,mask): # what number does this group encode?? ''' we count horizontal bars. The we create an encoding of the four possible vertical bars upper left and right lower left and right An 8 has three horizontal bars and all four vertical bars for an encoding of 3 1 1 1 1. ''' #xd = 999 global tfile # mask = stdSize(mask,xtyp) #cv2.resize(img, (1040,410)) if not len(grp) > 0: if db: ( 'zero len group. -- quitting') return(0) ## x,y, w, h = boundsBlob(grp) ## #w = 110; h = 300 # nasty hack w and h ## tmsk = mask[y:y+h , x:x+w].copy() ## pxls = tMatch(tmsk,xtyp,db) # search for a number ## print 'number is',pxls ## cvs(db,tmsk,'tmsk') ## return(pxls) xll =0; xlh=0; xrl=0; xrh = 0 # pixel counts v=0 ; h=0; # pixel groups x, y, wg, hg = boundsBlob(grp) for b in grp: bx,by,bw,bh = cv2.boundingRect(b) if bh < 1.1 * bw: h = h + 1 # add to the horizontal count else: #v v = v + 1 if (bx < wg/2 + x) : # if left side if (by > hg/2 + y): # if low xll = 1 # count vertical left low else: xlh = 1 # high else: # must be right side if (by > hg/2 + y): # so if low xrl = 1 # count vertical right low else: xrh = 1 # else high #if db: print( 'p so far is h {} v {} xll {} xlh {} xrl {} xrh {}' # .format(h, v, xll, xlh, xrl, xrh) ) p = h * 10000 + xll * 1000 + xlh * 100 + xrl * 10 + xrh ''' ptrn translates the results of the pattern analysis into numbers ''' ptrn = { 1100: 1, # 31001: 2, 30011: 3, 10111: 4, 30110: 5, 31110: 6, 10011: 7, #? 11001: 7, 31111: 8, 30111: 9, 21111: 0 } if p in ptrn: # HOO RAY we found a digit if db: print '>>>>>>found a {} {} '.format( ptrn[p] , p) writeNumb(ptrn[p],grp,mask) # keep the mask image for future use return ptrn[p] else: # we don't seem to have a number print '<<<<<<<qptrn - pattern problem p is >{}<'.format( p) #return('p') x,y, w, h = boundsBlob(grp) w = 110; h = 300 # nasty hack w and h tmsk = mask[y:y+h , x:x+w].copy() pxls = tMatch(tmsk,xtyp,db) # search for a number print 'number is',pxls cvs(db,tmsk,'tmsk') return(pxls)