Exemple #1
0
    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
Exemple #2
0
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
Exemple #3
0
    def test_3points_around_origin_unit_radius(self):
        p1 = Point(+1, 0)
        p2 = Point(+0, 1)
        p3 = Point(-1, 0)

        expected_list = list()
        expected_list.append(p1)
        expected_list.append(p2)
        expected_list.append(p3)

        helper = GradientDescentCircleFitting(None, expected_list)
        result: CircleModel = helper.FindBestFittingCircle()
        delta = 0.01
        self.assertAlmostEquals(result.R, 1.0, delta=delta)
        self.assertAlmostEquals(result.X, 0.0, delta=delta)
        self.assertAlmostEquals(result.Y, 0.0, delta=delta)
Exemple #4
0
 def test_5points_around_3_3_and_radius_5(self):
     #Plotted this circle using desmos.com
     #\left(5\cdot\cos\left(t\right)+3,5\cdot\sin\left(t\right)+3\right)
     p1 = Point(7, 0)
     p2 = Point(8, 3)
     p3 = Point(6, 7)
     p4 = Point(-2, 3)
     p5 = Point(0, -1)
     expected_list = list()
     expected_list.append(p1)
     expected_list.append(p2)
     expected_list.append(p3)
     expected_list.append(p4)
     expected_list.append(p5)
     helper = GradientDescentCircleFitting(None, expected_list)
     result: CircleModel = helper.FindBestFittingCircle()
     delta = 0.01
     self.assertAlmostEquals(result.R, 5.0, delta=delta)
     self.assertAlmostEquals(result.X, 3.0, delta=delta)
     self.assertAlmostEquals(result.Y, 3.0, delta=delta)
Exemple #5
0
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
Exemple #6
0
    def test_Constructor_With_Default_Learning_Rate(self):
        p1 = Point(1, 1)
        p2 = Point(2, 2)
        p3 = Point(3, 3)
        expected_list = list()
        expected_list.append(p1)
        expected_list.append(p2)
        expected_list.append(p3)

        expected_lrate = 0.05
        helper = GradientDescentCircleFitting(None, points=expected_list)
        self.assertEqual(expected_lrate, helper._learningrate)
        self.assertEqual(len(expected_list), len(helper._points))
        self.assertTrue(p1 in helper._points)
        self.assertTrue(p2 in helper._points)
Exemple #7
0
    def test_When_GD_Is_Invoked_With_less_than_3_points_Exception_MustBe_Thrown(
            self):
        p1 = Point(+1, 0)
        p2 = Point(+0, 1)
        expected_list = list()
        expected_list.append(p1)
        expected_list.append(p2)

        try:
            helper = GradientDescentCircleFitting(None, expected_list)
            self.fail("Exception was expected")
        except Exception as e:
            #Ok
            message = str(e)
            self.assertEquals(message, "Need 3 or more points")
            pass