def process(self, cv_image, info, image2=None): self.image2 = cv.CloneImage(cv_image) crop_rect = (self.cropx, self.cropy, self.cropwidth, self.cropheight) shape_contour = thresholding.get_contour(cv_image, bg_mode=thresholding.GREEN_BG, filter_pr2=True, crop_rect=crop_rect, cam_info=info, listener=self.listener) multiplier = 1 if not self.left_to_right: multiplier = -1 pt = max(shape_contour, key=lambda pt: pt[0] * multiplier) params = {} # Make sure it isn't super close: homogeneity is proportional to the number of pts within 5 pixels of this pt params["homogeneity"] = len([ p for p in shape_contour if abs(p[0] - pt[0]) <= 15 ]) / float(len(shape_contour)) pt_opp = min(shape_contour, key=lambda pt: pt[0] * multiplier) self.highlight_pt(pt, cv.CV_RGB(255, 255, 255)) self.highlight_pt(pt_opp, cv.CV_RGB(0, 0, 0)) pts = [pt, pt_opp] params["tilt"] = multiplier * -pi / 2 return (pts, params, self.image2)
def process(self,cv_image,info,image2=None): self.load_model(self.modelpath) #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(cv_image,bg_mode=thresholding.GREEN_BG,filter_pr2=True ,crop_rect=(116,121,431,347),cam_info=info,listener=self.listener) #Use shapefitting module to fit a triangles model to the data fitter = shape_fitting.ShapeFitter(SYMM_OPT=False,ORIENT_OPT=False,FINE_TUNE=False,num_iters=self.num_iters) image_anno = cv.CloneImage(cv_image) (nearest_pts, final_model, fitted_model) = fitter.fit(self.model,shape_contour,image_anno) #Get 3 samples from the 3 different regions of interest (l_line,r_line) = self.extract_samples(nearest_pts,cv_image,shape_contour) lbp_classification = self.lbp_classify() if lbp_classification == LEFT: left = True elif lbp_classification == RIGHT: left = False else: edge_classification = self.edge_classify(cv_image,l_line,r_line) if edge_classification == LEFT: left = True else: left = False pts = nearest_pts return_pts = [pts[1],pts[4],pts[2],pts[3]] params = {"tilt":0.0} if left: params["left"] = 1.0 self.draw_seg(image_anno,r_line,RED) else: params["left"] = 0.0 self.draw_seg(image_anno,l_line,BLUE) return (return_pts,params,image_anno)
def process(self,cv_image,info,image2=None): self.image2 = cv.CloneImage( cv_image ) shape_contour = thresholding.get_contour(cv_image,bg_mode=self.bg_mode,filter_pr2=True,crop_rect=(self.cropx,self.cropy,self.cropwidth,self.cropheight),cam_info=info,listener=self.listener) moments = cv.Moments(shape_contour,0) pt = get_center(moments) self.highlight_pt(pt,cv.CV_RGB(255,255,255)) pts = [pt] return (pts,{},self.image2)
def __init__(self,filepath,num_pts): self.image_path = filepath self.anno_path = filepath.replace(".png",".anno") self.num_pts = num_pts self.pts = [] self.t = [] self.open = True img = cv.LoadImage(self.image_path) self.background = self.normalize_image(img) self.clearImage() cv.NamedWindow("Annotator",1) self.contour = thresholding.get_contour(self.img,bg_mode=thresholding.GREEN_BG,filter_pr2=False,crop_rect=None,cam_info=None,listener=None) self.showImage() self.img_gray = cv.LoadImage(self.image_path,cv.CV_LOAD_IMAGE_GRAYSCALE) cv.SetMouseCallback("Annotator",self.handleEvents,0)
def process(self, cv_image, info, image2=None): self.image2 = cv.CloneImage(cv_image) shape_contour = thresholding.get_contour( cv_image, bg_mode=self.bg_mode, filter_pr2=True, crop_rect=(self.cropx, self.cropy, self.cropwidth, self.cropheight), cam_info=info, listener=self.listener) moments = cv.Moments(shape_contour, 0) pt = get_center(moments) self.highlight_pt(pt, cv.CV_RGB(255, 255, 255)) pts = [pt] return (pts, {}, self.image2)
def main(): img = take_picture(4) #compute a homography H = get_homography() #unwarped the image. Turn the image into the top view. unw_img = unwrap_image(img,H) # initialization background = thresholding.GREEN_BG #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(unw_img,bg_mode=background,filter_pr2=False,crop_rect=None) #""" cv.NamedWindow("Debug window") img = cv.CloneImage(unw_img) cv.PolyLine(img,[shape_contour],1,cv.CV_RGB(0,0,255),1) cv.ShowImage("Debug window",img) cv.WaitKey() cv.DestroyWindow("Debug window")
def process(self,cv_image,info,image2=None): self.image2 = cv.CloneImage( cv_image ) crop_rect=(self.cropx, self.cropy,self.cropwidth,self.cropheight) shape_contour = thresholding.get_contour(cv_image,bg_mode=thresholding.GREEN_BG,filter_pr2=True,crop_rect=crop_rect,cam_info=info,listener=self.listener) multiplier = 1 if not self.left_to_right: multiplier = -1 pt = max(shape_contour,key=lambda pt: pt[0]*multiplier) params = {} # Make sure it isn't super close: homogeneity is proportional to the number of pts within 5 pixels of this pt params["homogeneity"] = len([p for p in shape_contour if abs(p[0]-pt[0])<=15])/float(len(shape_contour)) pt_opp = min(shape_contour,key=lambda pt: pt[0]*multiplier) self.highlight_pt(pt,cv.CV_RGB(255,255,255)) self.highlight_pt(pt_opp,cv.CV_RGB(0,0,0)) pts = [pt,pt_opp] params["tilt"] = multiplier*-pi/2 return (pts,params,self.image2)
def __init__(self, filepath, num_pts): self.image_path = filepath self.anno_path = filepath.replace(".png", ".anno") self.num_pts = num_pts self.pts = [] self.t = [] self.open = True img = cv.LoadImage(self.image_path) self.background = self.normalize_image(img) self.clearImage() cv.NamedWindow("Annotator", 1) self.contour = thresholding.get_contour(self.img, bg_mode=thresholding.GREEN_BG, filter_pr2=False, crop_rect=None, cam_info=None, listener=None) self.showImage() self.img_gray = cv.LoadImage(self.image_path, cv.CV_LOAD_IMAGE_GRAYSCALE) cv.SetMouseCallback("Annotator", self.handleEvents, 0)
def process(self,cv_image,info,image2=None): self.load_model(self.modelpath) if self.transform: H = cv.Load(self.matrix_location) input_image = cv.CloneImage(cv_image) cv.WarpPerspective(input_image,cv_image,H, cv.CV_INTER_LINEAR+cv.CV_WARP_INVERSE_MAP+cv.CV_WARP_FILL_OUTLIERS) #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(cv_image,bg_mode=self.bg_mode,filter_pr2=self.filter_pr2 ,crop_rect=(self.cropx,self.cropy,self.cropwidth,self.cropheight),cam_info=info,listener=self.listener) #Use the shape_fitting module to fit the model to the contour if self.mode=="tee": #fitter = shape_fitting.ShapeFitter(SYMM_OPT=True,ORIENT_OPT=False,FINE_TUNE=False) fitter = shape_fitting.ShapeFitter(SYMM_OPT=False,ORIENT_OPT=True,FINE_TUNE=False,num_iters=self.num_iters) elif self.mode=="sweater": fitter = shape_fitting.ShapeFitter(SYMM_OPT=False,ORIENT_OPT=False,FINE_TUNE=False,num_iters=self.num_iters) elif self.mode=="folded": fitter = shape_fitting.ShapeFitter(SYMM_OPT=False,ORIENT_OPT=False,FINE_TUNE=False,INITIALIZE=False,num_iters=self.num_iters) else: fitter = shape_fitting.ShapeFitter(SYMM_OPT=False,ORIENT_OPT=False,FINE_TUNE=False,num_iters=self.num_iters) image_anno = cv.CloneImage(cv_image) (nearest_pts, final_model, fitted_model) = fitter.fit(self.model,shape_contour,image_anno) pts = nearest_pts params = {} if self.mode == "triangles": return_pts = [pts[1],pts[4],pts[2],pts[3]] self.highlight_pt(pts[1],cv.CV_RGB(255,0,0),image_anno) font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX,1.0,1.0) cv.PutText(image_anno,"(l)eft",(pts[1][0]-20,pts[1][1]-15),font,cv.CV_RGB(255,0,0)) self.highlight_pt(pts[4],cv.CV_RGB(0,0,255),image_anno) cv.PutText(image_anno,"(r)ight",(pts[4][0]-20,pts[4][1]-15),font,cv.CV_RGB(0,0,255)) params = {"tilt":0.0} elif self.mode == "towel": return_pts = pts elif self.mode == "tee" or self.mode == "sweater": return_pts = pts[0:5]+pts[8:] params = {} elif self.mode == "folded": return_pts = [final_model.fold_bottom(),final_model.fold_top()] else: return_pts = pts params = {} if self.transform: H_inv = cv.CloneMat(H) cv.Invert(H,H_inv) anno_unchanged = cv.CloneImage(image_anno) cv.WarpPerspective(anno_unchanged,image_anno,H_inv, cv.CV_INTER_LINEAR+cv.CV_WARP_INVERSE_MAP+cv.CV_WARP_FILL_OUTLIERS) new_return_pts = self.transform_pts(return_pts,H_inv) for i,pt in enumerate(new_return_pts): return_pts[i] = pt if self.mode != "triangles": for pt in return_pts: self.highlight_pt(pt,cv.CV_RGB(255,0,0),image_anno) if self.mode != "folded": fitted_model.set_image(None) pickle.dump(fitted_model,open("%s/last_model.pickle"%self.save_dir,'w')) else: model_pts = final_model.vertices_full() new_model = Models.Point_Model_Contour_Only_Asymm(*model_pts) pickle.dump(new_model,open("%s/last_model.pickle"%self.save_dir,'w')) score = final_model.score(shape_contour) #fitter.energy_fxn(final_model,shape_contour) params["score"] = score return (return_pts,params,image_anno)
def main(args): #Properly set phases orient_opt = 'O' in args.phases symm_opt = 'S' in args.phases asymm_opt = 'A' in args.phases fine_tuning_opt = 'F' in args.phases #Properly set background colors if args.background_color == 'GREEN': background = thresholding.GREEN_BG elif args.background_color == 'WHITE': background = thresholding.WHITE_BG elif args.background_color == 'BLACK': background = thresholding.BLACK_BG else: raise Exception("Invalid background color: %s"%args.background_color) #Translate silent/verbose silent = not args.verbose #Translate output- directory and prefix if args.output_directory: directory = args.output_directory else: directory = os.path.dirname(args.input_image) if not os.path.exists(directory): print "Making dir %s"%directory os.system("mkdir -p %s"%directory) if args.output_prefix: prefix = args.output_prefix else: prefix = os.path.splitext(os.path.basename(args.input_image))[0]+"_fit" #Load model and image models = [pickle.load(open(modelpath)) for modelpath in args.models] image_raw = cv.LoadImage(args.input_image,cv.CV_LOAD_IMAGE_COLOR) best_model = None best_nearest_pts = None best_fitted_model = None best_image_out = None best_index = 0 best_score = 100 scores = [] for i,model in enumerate(models): print "On model %d of %d"%(i+1,len(models)) #Create an image to output image_out = cv.CloneImage(image_raw) #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(image_raw,bg_mode=background,filter_pr2=False,crop_rect=None) fitter = shape_fitting.ShapeFitter( ORIENT_OPT=orient_opt, SYMM_OPT=symm_opt, ASYMM_OPT=asymm_opt, FINE_TUNE=fine_tuning_opt, SILENT=silent, SHOW=args.show_graphics, num_iters=args.num_iters ) (nearest_pts, final_model, fitted_model) = fitter.fit(model,shape_contour,image_out,image_raw) final_model.set_image(cv.CloneImage(image_raw)) score = final_model.score(shape_contour,image_raw) final_model.set_image(None) scores.append(score) if not best_model or score <= best_score: best_score = score best_model = model best_nearest_pts = nearest_pts best_fitted_model = fitted_model best_image_out = image_out best_index = i final_model = best_model nearest_pts = best_nearest_pts fitted_model = best_fitted_model image_out = best_image_out #Optionally save the nearest points in a .anno file, for comparison with my own annotations full_prefix = directory + '/' + prefix if args.save_anno: anno_path = full_prefix+".anno" print "Saving anno to " + anno_path Annotating.write_anno(nearest_pts,anno_path) #Optionally save the model, for reuse later if args.save_model: #Remove the image to make pickling possible final_model.set_image(None) save_model_path = full_prefix+".pickle" print "Saving model to " + save_model_path model_dest = open(save_model_path,'w') pickle.dump(final_model,model_dest) model_dest.close() #Optionally save scores if args.save_scores: savepath = full_prefix+".scores" print "Saving scores to " + savepath savefile = open(savepath,'w') for i in range(len(scores)): savefile.write("%s\t%f\n"%(args.models[i],scores[i])) savefile.close() #Optionally save the classification if args.save_classification: savepath = full_prefix+".classif" print "Saving classification to " + savepath savefile = open(savepath,'w') savefile.write(models[best_index].name()+"\n") savefile.close() #Optionally save the image if args.save_image: savepath = full_prefix+".png" print "Saving annotated image to " + savepath cv.SaveImage(savepath,image_out) return else: cv.NamedWindow("Result") cv.ShowImage("Result",image_out) cv.WaitKey() return
def fit_model_to_image(model,image,iteration): show_message("FIT_MODEL_TO_IMAGE - begin", MsgTypes.debug) # initialization background = thresholding.WHITE_BG silent = False # true = silent, false = verbose show_graphics = True num_iters = 100 # towel #num_iters = 15 #Properly set phases orient_opt = True symm_opt = True asymm_opt = True fine_tuning_opt= True #if(iteration == 0): # different optimalization parameters for first fitting # #asymm_opt = False # #fine_tuning_opt = False # orient_opt = False # num_iters = 17 #Create an image to output image_out = cv.CloneImage(image) #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(image,bg_mode=background,filter_pr2=False,crop_rect=None) #""" Show object contur cv.NamedWindow("Shape contocur of the observed object") img = cv.CloneImage(image) cv.PolyLine(img,[shape_contour],1,cv.CV_RGB(0,0,255),1) #""" #Use the shaper fitter module to fit the model to image fitter = shape_fitting.ShapeFitter( ORIENT_OPT=orient_opt, SYMM_OPT=symm_opt, ASYMM_OPT=asymm_opt, FINE_TUNE=fine_tuning_opt, SILENT=silent, SHOW=show_graphics, num_iters=num_iters) before = datetime.now() final_model = fitted_model = None #if(iteration > 0): # (nearest_pts, final_model, fitted_model) = fitter.fit(model,shape_contour,image_out,image) (nearest_pts, final_model, fitted_model) = fitter.fit(model,shape_contour,image_out,image) show_message("Fitting time %s"%str(datetime.now()-before), MsgTypes.info) """ save fitted model to the file if(final_model != None): final_model.set_image(None) modelPath = "/media/Data/models/tShirt_F_%0.1d.pickle" %iteration pickle.dump(final_model, open(modelPath,'w')) #""" """ load fitted model from the file if(iteration < 1): modelPath = "/media/Data/models/tShirt_F_%0.1d.pickle" %iteration #modelPath = "/media/Data/models/tShirt_paper_F_%0.1d.pickle" %iteration final_model = pickle.load(open(modelPath)) #""" """ visualisation print "/**************Test****************/" #im1 = cv.CloneImage(image) #cv.NamedWindow("Fitted model") #if(fitted_model != None): # cv.PolyLine(im1,[fitted_model.polygon_vertices_int()],1,cv.CV_RGB(0,255,0),1) #cv.ShowImage("Fitted model",im1) #cv.WaitKey() im2 = cv.CloneImage(image) cv.NamedWindow("Final model") if(final_model != None): cv.PolyLine(im2,[final_model.polygon_vertices_int()],1,cv.CV_RGB(0,255,0),1) cv.ShowImage("Final model",im2) cv.WaitKey() cv.SaveImage("/media/Data/im%d.png"%iteration,im2); #cv.DestroyWindow("Fitted model") cv.DestroyWindow("Final model") print "/************EndOfTest*************/" """ show_message("FIT_MODEL_TO_IMAGE - end", MsgTypes.debug) return (final_model,final_model)
def main(args): #Properly set phases orient_opt = 'O' in args.phases symm_opt = 'S' in args.phases asymm_opt = 'A' in args.phases fine_tuning_opt = 'F' in args.phases #Properly set background colors if args.background_color == 'GREEN': background = thresholding.GREEN_BG elif args.background_color == 'WHITE': background = thresholding.WHITE_BG elif args.background_color == 'BLACK': background = thresholding.BLACK_BG else: raise Exception("Invalid background color: %s" % args.background_color) #Translate silent/verbose silent = not args.verbose #Translate output- directory and prefix if args.output_directory: directory = args.output_directory else: directory = os.path.dirname(args.input_image) if not os.path.exists(directory): print "Making dir %s" % directory os.system("mkdir -p %s" % directory) if args.output_prefix: prefix = args.output_prefix else: prefix = os.path.splitext(os.path.basename( args.input_image))[0] + "_fit" #Load model and image models = [pickle.load(open(modelpath)) for modelpath in args.models] image_raw = cv.LoadImage(args.input_image, cv.CV_LOAD_IMAGE_COLOR) best_model = None best_nearest_pts = None best_fitted_model = None best_image_out = None best_index = 0 best_score = 100 scores = [] for i, model in enumerate(models): print "On model %d of %d" % (i + 1, len(models)) #Create an image to output image_out = cv.CloneImage(image_raw) #Use the thresholding module to get the contour out shape_contour = thresholding.get_contour(image_raw, bg_mode=background, filter_pr2=False, crop_rect=None) fitter = shape_fitting.ShapeFitter(ORIENT_OPT=orient_opt, SYMM_OPT=symm_opt, ASYMM_OPT=asymm_opt, FINE_TUNE=fine_tuning_opt, SILENT=silent, SHOW=args.show_graphics, num_iters=args.num_iters) (nearest_pts, final_model, fitted_model) = fitter.fit(model, shape_contour, image_out, image_raw) final_model.set_image(cv.CloneImage(image_raw)) score = final_model.score(shape_contour, image_raw) final_model.set_image(None) scores.append(score) if not best_model or score <= best_score: best_score = score best_model = model best_nearest_pts = nearest_pts best_fitted_model = fitted_model best_image_out = image_out best_index = i final_model = best_model nearest_pts = best_nearest_pts fitted_model = best_fitted_model image_out = best_image_out #Optionally save the nearest points in a .anno file, for comparison with my own annotations full_prefix = directory + '/' + prefix if args.save_anno: anno_path = full_prefix + ".anno" print "Saving anno to " + anno_path Annotating.write_anno(nearest_pts, anno_path) #Optionally save the model, for reuse later if args.save_model: #Remove the image to make pickling possible final_model.set_image(None) save_model_path = full_prefix + ".pickle" print "Saving model to " + save_model_path model_dest = open(save_model_path, 'w') pickle.dump(final_model, model_dest) model_dest.close() #Optionally save scores if args.save_scores: savepath = full_prefix + ".scores" print "Saving scores to " + savepath savefile = open(savepath, 'w') for i in range(len(scores)): savefile.write("%s\t%f\n" % (args.models[i], scores[i])) savefile.close() #Optionally save the classification if args.save_classification: savepath = full_prefix + ".classif" print "Saving classification to " + savepath savefile = open(savepath, 'w') savefile.write(models[best_index].name() + "\n") savefile.close() #Optionally save the image if args.save_image: savepath = full_prefix + ".png" print "Saving annotated image to " + savepath cv.SaveImage(savepath, image_out) return else: cv.NamedWindow("Result") cv.ShowImage("Result", image_out) cv.WaitKey() return