def detect_by_gf(origin, isdebug=False): if origin is None: return None # Default Size h,w,c = origin.shape size = 200.0 # Resize img = cv2.resize(origin,(int(w*size/h),int(size))) # Extract Good Features corners = refinedGoodFeatures(origin,img, model='LP') mask = checkFeatures(img,corners,True) # Find Candidate bboxes = findBBox(img,mask, model='LP', debug=True) # Resize if bboxes is not None: bboxes = resizeBBoxes(bboxes,h/size) # Check Result if isdebug and bboxes is not None: drawBBox(origin,bboxes,debug=True) # Crop Rois rois = BBoxes2ROIs(origin,bboxes) if isdebug and rois is not None: for i in range(len(rois)): showResult("cropped",rois[i]) return bboxes,rois
def refine_gfimage(img, mask): #time-consuming-mask # Draw BBox using gf bboxes = findBBox(img, mask) if bboxes is None: return mask box_img = np.zeros((img.shape), dtype=np.uint8) #cv2.drawContours(box_img,[bboxes],-1,(255,255,255),1) for bbox in bboxes: bbox_ = resizeBBox(bbox, ratio=1.0) cv2.drawContours(box_img, [bbox_], 0, (255, 255, 255), 1) newmask = cv2.cvtColor(box_img, cv2.COLOR_BGR2GRAY) # #img_contours,contours,hierarchy = cv2.findContours(newmask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) contours, hierarchy = cv2.findContours(newmask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) img_contours = np.zeros((img.shape), dtype=np.uint8) if len(contours) == 0: return mask for contour in contours: random.shuffle(contour) cv2.drawContours(img_contours, [contour], 0, (255, 255, 255), 1) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) newmask = cv2.morphologyEx(img_contours[:, :, 0], cv2.MORPH_CLOSE, kernel) return newmask
def mask2plates(img, finalmask, isdebug=False): #showResult("masktest",finalmask) #ret,binary = cv2.threshold(finalmask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)\ binary = compositeThreshold(finalmask, mode='otsu') closing = close(binary) if isdebug: showResult("mask2plates:masktest", closing) # Find Candidate return findBBox(img, closing, isdebug=isdebug)
def detect_by_seg_gf(origin, isdebug=False): if origin is None: return None # Default Size h,w,c = origin.shape size = 200.0 #origin = opencv2skimage(origin) # Resize img = cv2.resize(origin,(int(w*size/h),int(size))) # Blur blur = cv2.GaussianBlur(img,(5,5),3) # Equalization Hist #origin = equalizehist(origin) # Extract Good Features corners = refinedGoodFeatures(origin,img, model='LP') corners_,handwrite = refinedCorners(img,corners,False) checkFeatures(img,corners,True) # Opencv2Skimage skimg = cv2.cvtColor(blur, cv2.COLOR_BGR2RGB) # Segmentation out,labels = seg(skimg,Debug=False) # Eval Label labels = refineLabels(out,labels,corners_,howmany=20) # Show Result out = drawLabels(skimg,labels,Debug=isdebug) if out is None: return None changebgcolr(out,labels) #showResult("labelout",skimage2opencv(out)) # Find Candidate bboxes = findBBox(img,out, model='LP', debug=isdebug) # Resize if bboxes is not None: bboxes = resizeBBoxes(bboxes,h/size) # Check Candidate if isdebug and bboxes is not None: drawBBox(origin,bboxes,debug=isdebug) # Crop Rois rois = BBoxes2ROIs(origin,bboxes) if isdebug and rois is not None: for i in range(len(rois)): showResult("cropped",rois[i]) ''' if isdebug: #labels2boundaries(labels) contours = labels2contours(labels) drawBBox(img,contours,debug=True) ''' return bboxes,rois
def detect_by_probability(origin, isdebug=False): if origin is None: return None # Default Size h,w,c = origin.shape size = 200.0 # Resize img = cv2.resize(origin,(int(w*size/h),int(size))) #showResult("img",img) # if dayornight(img): # Extract Good Features corners = refinedGoodFeatures(origin,img) mask = checkFeatures(img,corners,False) closing=close(mask) refined_gfmask = refine_gfimage(img,closing) #showResult("refined_gfmask",refined_gfmask) finalmask = mkfinalmask(img,refined_gfmask,isday=True) else: finalmask = mkfinalmask(img,None,isday=False) # #showResult("masktest",finalmask) #ret,binary = cv2.threshold(finalmask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) binary = compositeThreshold(finalmask,mode='otsu') kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) closing=cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel) if isdebug: showResult("masktest",closing) # Find Candidate bboxes = findBBox(img,closing,isdebug=True) # Resize if bboxes is not None: bboxes = resizeBBoxes(bboxes,h/size) # Check Result if isdebug and bboxes is not None: drawBBox(origin,bboxes,debug=True) # Crop Rois rois = BBoxes2ROIs(origin,bboxes) if isdebug and rois is not None: for i in range(len(rois)): showResult("cropped",rois[i]) return bboxes,rois