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 writeAnno(self): Annotating.write_anno(self.pts, self.anno_path)
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 writeAnno(self): Annotating.write_anno(self.pts,self.anno_path)