def rdNumber(img, tval=160, ex=2, ms=1.0,mx=3, dz = 60): ''' analyse the img and pass a likely set of features to qnumb for interpretation ''' global db, tfile fz = 0; h= 0; v = 0; tgrp = [] ;numb = [] dx = dz print '-----rdNumber tval',tval,' ex',ex,'ms', ms, 'mxsize', mx fs,mask = findBlobs(img, ms, mx ,ex, db,tval=tval ) if not fs: if db: print ' no features found' cvs(db,img,'nothing in rdnumbr') return ([0,0,0,0],0,0) else: # fs = sorted(fs, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) fs = sorted(fs, key = lambda cnt:cv2.contourArea(cnt)) cv2.drawContours( img, fs, -1, (0, 0, 255), 5 ) rmx = cv2.contourArea(fs[0]) # left most area rmn = rmx fz = 0 fs = sorted(fs, key = lambda cnt: tuple(cnt[cnt[:,:,0].argmin()][0])) # sorted left to right for ix, f in enumerate( fs): x,y,w,h = cv2.boundingRect(f) #fx = x if cv2.contourArea(f) < rmn: rmn = cv2.contourArea(f) if cv2.contourArea(f) > rmx: rmx = cv2.contourArea(f) xd = x - fz # xd is horizontal dist between blobs if xd > dx: # dx is parm here is where the number is detected numb.append(qnumb(tgrp,mask)) #,mask,img)) # process the number tnumb(tgrp,img) # black rectangle and training output tgrp = [] # start a new group if db: print 'ix {} w {} h {} dx {} xd {} area {}'\ .format(ix,w,h,dx,xd,cv2.contourArea(f)) tgrp.append(f) ## cv2.drawContours( img, [f], 0, (0, 255, 255), 5 ) ## cvs(db,img) fz = x numb.append(qnumb(tgrp,mask)) #,mask,img)) # process the last group tnumb(tgrp,img ) print 'last numb', numb tpat = numb if all(i == 'p' for i in tpat): return([0,0,0,0],rmx,rmn) # nfg return True zero if len(tpat)>3: while tpat[-1] == 'p': tpat.pop() while( tpat[0] == 'p'): tpat.pop(0) # if db: cpause(['end rd',tpat],Gd) if 'p' in tpat: return([0,0,0,0],rmx,rmn) # nfg return True zero else: return(tpat,rmx,rmn) # maybe good numeric value
cv2.createTrackbar('min','image',0,100,nothing) cv2.createTrackbar('max','image',0,1000,nothing) cv2.createTrackbar('tval','image',0,255,nothing) cv2.createTrackbar('erd','image',0,20,nothing) while(1): cv2.imshow('image',imgx) k = cv2.waitKey(1) & 0xFF if k == 27: cvd() break if k == ord('s'): fil = "img3c.png" fil = 'fatTest.png' db = False img = cv2.imread(fil) blobs = findBlobs(img,ms,mx,erd,db,tval) # 500 3000 2 160 if blobs is not None : print '{} blobs found '.format(len(blobs)) for i,f in enumerate (blobs): if i > 20: break cv2.drawContours(img,[f],0,(0,0,0),2) cvs(0,img,'contours') else: print 'no blobs found', ms = cv2.getTrackbarPos('min','image') mx = cv2.getTrackbarPos('max','image') erd = cv2.getTrackbarPos('erd','image') tval = cv2.getTrackbarPos('tval','image') cv2.destroyAllWindows()