def test_large_circle_50X50_no_noise_2(self): folder_script = os.path.dirname(__file__) filename_input = "NoisyCircle_x_6_y_-30_r_118.162.png" file_noisy_line = os.path.join(folder_script, "./data/", filename_input) np_image = skimage.io.imread(file_noisy_line, as_gray=True) lst_points = Util.create_points_from_numpyimage(np_image) helper = GradientDescentCircleFitting(None, lst_points, learningrate=0.4, iterations=5000) result: CircleModel = helper.FindBestFittingCircle() # #Superimpose the new line over the image # folder_results = os.path.join(folder_script, "../out/") count_of_files = len(os.listdir(folder_results)) filename_results = ("%s.%d.png" % (__name__, count_of_files)) file_result = os.path.join(folder_results, filename_results) new_points = CircleModel.generate_points_from_circle(result) np_superimposed = Util.superimpose_points_on_image( np_image, new_points, 100, 255, 100) skimage.io.imsave(file_result, np_superimposed) delta = 10 self.assertAlmostEquals(result.R, +118.0, delta=delta) self.assertAlmostEquals(result.X, +06.0, delta=delta) self.assertAlmostEquals(result.Y, -30.0, delta=delta) pass
def test_large_circle_50X50_no_noise_1(self): folder_script=os.path.dirname(__file__) filename_input="NoisyCircle_x_-10_y_-14.png" file_noisy_line=os.path.join(folder_script,"./data/",filename_input) np_image=skimage.io.imread(file_noisy_line,as_gray=True) lst_points=Util.create_points_from_numpyimage(np_image) helper=BullockCircleFitting(lst_points) result:CircleModel =helper.FindBestFittingCircle() # #Superimpose the new line over the image # folder_results=os.path.join(folder_script,"../out/") count_of_files=len(os.listdir(folder_results)) filename_results=("%s.%d.png" % (__name__,count_of_files) ) file_result=os.path.join(folder_results,filename_results) new_points=CircleModel.generate_points_from_circle(result) np_superimposed=Util.superimpose_points_on_image(np_image,new_points,100,255,100) skimage.io.imsave(file_result,np_superimposed) delta=2 self.assertAlmostEquals(result.R, 48.0, delta=delta); self.assertAlmostEquals(result.X, -10.0, delta=delta); self.assertAlmostEquals(result.Y, -14.0, delta=delta); pass
def run_image2matplot(filename): folder_script = os.path.dirname(__file__) absolute_path = os.path.join(folder_script, "./input/", filename) try: np_image = skimage.io.imread(absolute_path, as_gray=True) lst_all_points = Util.create_points_from_numpyimage(np_image) plot_new_points_over_existing_points(lst_all_points, [], "Input data", "Original points", "") lrate = 0.3 iterations = 5000 helper = GradientDescentCircleFitting(None, points=lst_all_points, learningrate=lrate, iterations=iterations) start_time = time.time() model: CircleModel = helper.FindBestFittingCircle() new_points = CircleModel.generate_points_from_circle(model) plot_new_points_over_existing_points( lst_all_points, new_points, "Gradient descent circle fitting", "Original points", "Gradient descent") except Exception as e: tb = traceback.format_exc() print("Error:%s while doing RANSAC on the file: %s , stack=%s" % (str(e), filename, str(tb))) print("------------------------------------------------------------") pass pass
def test_generate_points_from_circle_model_center_at_0_0_radius_1(self): center_x = 0 center_y = 0 radius = 1.0 model = CircleModel(center_x, center_y, radius) points = CircleModel.generate_points_from_circle(model) pt: Point for pt in points: distance_from_center = math.sqrt((pt.X - center_x)**2 + (pt.Y - center_y)**2) self.assertAlmostEqual(distance_from_center, radius, 2)
def run(filename, threshold, inlier, sampling_fraction=0.25, matplot=False): print("Going to process file:%s" % (filename)) folder_script = os.path.dirname(__file__) file_noisy_circle = os.path.join(folder_script, "./input/", filename) try: np_image = skimage.io.imread(file_noisy_circle, as_gray=True) # #Iterate over all cells of the NUMPY array and convert to array of Point classes # lst_all_points = Util.create_points_from_numpyimage(np_image) # #begin RANSAC # helper = RansacCircleHelper() helper.threshold_error = threshold helper.threshold_inlier_count = inlier helper.add_points(lst_all_points) helper.sampling_fraction = sampling_fraction best_model = helper.run() print("RANSAC-complete") if (best_model == None): print( "ERROR! Could not find a suitable model. Try altering ransac-threshold and min inliner count" ) return # #Generate an output image with the model circle overlayed on top of original image # now = datetime.datetime.now() filename_result = ("%s-%s.png" % (filename, now.strftime("%Y-%m-%d-%H-%M-%S"))) file_result = os.path.join(folder_script, "./out/", filename_result) #Load input image into array np_image_result = skimage.io.imread(file_noisy_circle, as_gray=True) new_points = CircleModel.generate_points_from_circle(best_model) np_superimposed = Util.superimpose_points_on_image( np_image_result, new_points, 100, 255, 100) #Save new image skimage.io.imsave(file_result, np_superimposed) print("Results saved to file:%s" % (file_result)) print("------------------------------------------------------------") if (matplot == True): plot_new_points_over_existing_points( lst_all_points, new_points, "Outcome of RANSAC algorithm", "Original points", "RANSAC") except Exception as e: tb = traceback.format_exc() print("Error:%s while doing RANSAC on the file: %s , stack=%s" % (str(e), filename, str(tb))) print("------------------------------------------------------------") pass
def superimpose_circle_over_original_image(self, original_image_file, circle): np_image = skimage.io.imread(original_image_file, as_gray=True) folder_script = os.path.dirname(__file__) folder_results = os.path.join(folder_script, "../out/") count_of_files = len(os.listdir(folder_results)) filename_results = ("%s.%d.png" % (__name__, count_of_files)) file_result = os.path.join(folder_results, filename_results) new_points = CircleModel.generate_points_from_circle(circle, distance=2) np_superimposed = Util.superimpose_points_on_image( np_image, new_points, 255, 255, 0) skimage.io.imsave(file_result, np_superimposed)
def test_generate_points_from_circle_model_center_at_0_0_radius_10(self): center_x = 0 center_y = 0 radius = 100.0 distance = 5 model = CircleModel(center_x, center_y, radius) points = CircleModel.generate_points_from_circle(model, distance=distance) pt: Point for pt in points: distance_from_center = math.sqrt((pt.X - center_x)**2 + (pt.Y - center_y)**2) self.assertAlmostEqual(distance_from_center, radius, 2) #compare distance for index in range(1, len(points)): point1 = points[index] point2 = points[index - 1] actual_distance = Point.euclidean_distance(point1, point2) self.assertAlmostEqual(distance, actual_distance, delta=2)
def run_image2image(filename): print("Going to fit circle in the file:%s" % (filename)) folder_script = os.path.dirname(__file__) absolute_path = os.path.join(folder_script, "./input/", filename) try: np_image = skimage.io.imread(absolute_path, as_gray=True) lst_all_points = Util.create_points_from_numpyimage(np_image) lrate = 0.3 iterations = 5000 helper = GradientDescentCircleFitting(None, points=lst_all_points, learningrate=lrate, iterations=iterations) start_time = time.time() model: CircleModel = helper.FindBestFittingCircle() print("--- %s seconds for gradient descent algo ---" % (time.time() - start_time)) # #Generate an output image with the model circle overlayed on top of original image # now = datetime.datetime.now() filename_result = ("gradient-descent-%s.png" % (filename)) file_result = os.path.join(folder_script, "./out/", filename_result) #Load input image into array np_image_result = skimage.io.imread(absolute_path, as_gray=True) new_points = CircleModel.generate_points_from_circle(model) np_superimposed = Util.superimpose_points_on_image( np_image_result, new_points, 100, 255, 100) #Save new image skimage.io.imsave(file_result, np_superimposed) print("Results saved to file:%s" % (file_result)) print("------------------------------------------------------------") except Exception as e: tb = traceback.format_exc() print("Error:%s while doing RANSAC on the file: %s , stack=%s" % (str(e), filename, str(tb))) print("------------------------------------------------------------") pass pass