def writeNumb(n,grp,mask): return() x,y, w, h = boundsBlob(grp) tmplate = mask[y:y+h , x:x+w].copy() fil = '/Train/{}{}.png'.format(xtyp,n) print 'writeNumb n {} to file {}'.format(n,fil) cv2.imwrite(fil,tmplate) cvs(db,tmplate,fil)
def tnumb(grp,img): ''' process a possible digit and output a mask to a file ''' if not len(grp) > 0: return(0) x,y, wg, hg = boundsBlob(grp) # compute bounds for the possible digit top left bottom right tl = (x-10,y-10) ; br = ( x + wg+10, y+ hg+10) # 10 for more room cv2.rectangle(img,(tl),(br),(0,0,0),5) # black rectangle x1= x; y1 = hg/2 + y; x2=x + wg; y2 = y1 cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2) # horizontal center x1= wg/2 + x; y1=y; x2=x1; y2=hg+y cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2) # vertical center #cvs(db,img) zh,zw = img.shape[:2] # print ' w x h db ', w , h, db if db: print 'rectangle tl{} br{} img w{} h{}'.format(tl,br, zw, zh)
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)