Esempio n. 1
0
    def test_When_get_inliers_and_3_inliers_and_1_outlier_and_no_exclusion_list(
            self):
        #arrange
        p1 = Point(+1.4, 0.0)
        p2 = Point(+0.0, 1.4)
        p3 = Point(-1.4, 0.0)
        p_outlier = Point(-10, 0)
        list_of_points = list()
        list_of_points.append(p1)
        list_of_points.append(p2)
        list_of_points.append(p3)
        list_of_points.append(p_outlier)

        model = CircleModel(0, 0, 1)
        helper = RansacCircleHelper()
        helper.threshold_error = 0.5
        helper.add_points(list_of_points)

        #act
        inliers, model_score = helper.get_inliers(model, [])

        #assert
        expected_score = ((0.4 / 1.4) + (0.4 / 1.4) + (0.4 / 1.4)) / 3.0
        self.assertAlmostEquals(model_score, expected_score, delta=0.1)
        self.assertEquals(len(inliers), 3)
        self.assertTrue(p1 in inliers)
        self.assertTrue(p2 in inliers)
        self.assertTrue(p3 in inliers)
        self.assertFalse(p_outlier in inliers)
Esempio n. 2
0
 def test_GetRandomPoints(self):
     helper1=RansacLineHelper()
     lst=list()
     lst.append(Point(0,0))
     lst.append(Point(1,1))
     lst.append(Point(2,2))
     lst.append(Point(3,3))
     print("displaying orignal points")
     print("------------")
     for p in lst:
         print(p)
     print("------------")
     helper1.add_points(lst)
     print("displaying points after adding to collection")
     print("------------")
     for p in helper1.points:
         print(p)
     print("------------")
     
     rnd_pts=helper1.select_random_points(2)
     self.assertEquals(len(rnd_pts), 2)
     for rnd_pt in rnd_pts:
         is_member=(rnd_pt in lst)
         print("ID of random pt=%d" % rnd_pt.ID)
         self.assertEqual(is_member,True)
Esempio n. 3
0
 def test_when_points_are_superimposed_over_image_array_and_saved_the_new_image_must_contain_the_new_points(
         self):
     folder_script = os.path.dirname(__file__)
     filename = "Util_unittest.png"
     file_noisy_line = os.path.join(folder_script, "./data/", filename)
     np_image = skimage.io.imread(file_noisy_line, as_gray=True)
     file_result = os.path.join(folder_script, "../out/", filename)
     new_points = list()
     #
     #Superimpose some points
     #
     new_points.append(Point(0, 0))
     new_points.append(Point(2, 2))
     new_points.append(Point(3, 3))
     new_points.append(Point(4, 4))
     color_red = 100
     color_green = 255
     color_blue = 90
     np_newimage = Util.superimpose_points_on_image(np_image, new_points,
                                                    color_red, color_green,
                                                    color_blue)
     skimage.io.imsave(file_result, np_newimage)
     #Read the image back and test the points
     np_newimage2 = skimage.io.imread(file_result, as_gray=False)
     height = np_newimage.shape[0]
     for p in new_points:
         x = p.X
         y = height - p.Y - 1
         self.assertEqual(np_newimage2[y][x][0], color_red)
         self.assertEqual(np_newimage2[y][x][1], color_green)
         self.assertEqual(np_newimage2[y][x][2], color_blue)
     pass
     self.assertGreater(len(new_points), 1)
Esempio n. 4
0
    def test_When_get_inliers_and_all_points_on_circumfrence_and_no_exclusion_list(
            self):
        #arrange
        p1 = Point(+1, 0)
        p2 = Point(+0, 1)
        p3 = Point(-1, 0)
        list_of_points = list()
        list_of_points.append(p1)
        list_of_points.append(p2)
        list_of_points.append(p3)

        model = CircleModel(0, 0, 1)
        helper = RansacCircleHelper()
        helper.threshold_error = 0.2
        helper.add_points(list_of_points)

        #act
        inliers, model_score = helper.get_inliers(model, [])

        #assert
        self.assertAlmostEquals(model_score, 0.0, delta=0.1)
        self.assertEquals(len(inliers), 3)
        self.assertTrue(p1 in inliers)
        self.assertTrue(p2 in inliers)
        self.assertTrue(p3 in inliers)
Esempio n. 5
0
 def test_add_points_to_line(self):
     model = LineModel.create_line_from_2points(0, 0, 100, 100)
     test_point1 = Point(1, 0)
     test_point2 = Point(2, 0)
     model.points.append(test_point1)
     model.points.append(test_point2)
     self.assertTrue(test_point1 in model.points)
     self.assertTrue(test_point2 in model.points)
     self.assertTrue(len(model.points) == 2)
Esempio n. 6
0
    def test_generate_model_from_3points_straight_line(self):
        p_0 = Point(0, 33)
        p_1 = Point(9, 30)
        p_2 = Point(12, 29)

        try:
            c1 = CircleModel.GenerateModelFrom3Points(p_0, p_1, p_2)
            self.fail("Expected exception was not thrown")
        except:
            #Exception was expected
            pass
Esempio n. 7
0
 def test_AddPoints(self):
     helper=RansacLineHelper()
     lst=list()
     lst.append(Point(0,0))
     lst.append(Point(1,1))
     lst.append(Point(2,2))
     lst.append(Point(3,3))
     helper.add_points(lst)
     expected_count=len(lst)
     actual_count=len(helper.points)
     self.assertEqual(expected_count,actual_count)
     pass
Esempio n. 8
0
    def test_GenerateModelFrom3pmodels_PassingThrough_1_1_And_0_2_minus1_1(
            self):
        p_1_0 = Point(1, 1)
        p_0_0 = Point(0, 2)
        p_minus1_0 = Point(-1, 1)

        c1 = CircleModel.GenerateModelFrom3Points(p_0_0, p_1_0, p_minus1_0)
        #Assert on radius
        self.assertAlmostEquals(c1.R, 1.0, 1)
        #Assert on center X,Y
        self.assertAlmostEquals(c1.X, 0.0, 1)
        self.assertAlmostEquals(c1.Y, 1.0, 1)
Esempio n. 9
0
    def test_create_model_vertical_through_x_equal_5(self):
        pass
        helper1=RansacLineHelper()
        lst=list()
        lst.append(Point(5,0))
        lst.append(Point(5,1))
        lst.append(Point(5,2))
        model=helper1.create_model(lst)
        expected_xintercept=5
        actual_xintercept=-model.C/model.A
        self.assertAlmostEqual(actual_xintercept,expected_xintercept)

        self.assertAlmostEqual(model.B,0)
Esempio n. 10
0
 def test_generate_plottable_linear_points_between_twopoints(self):
     pt_start = Point(1, 1)
     pt_end = Point(20, 20)
     unit = Vector.create_vector_from_2points(pt_start, pt_end).UnitVector
     new_points = Util.generate_plottable_points_between_twopoints(
         pt_start, pt_end)
     distance_start_end = Point.euclidean_distance(pt_start, pt_end)
     for new_point in new_points:
         distance_from_start = Point.euclidean_distance(pt_start, new_point)
         self.assertTrue(distance_from_start < distance_start_end)
         new_unit = Vector.create_vector_from_2points(pt_start,
                                                      new_point).UnitVector
         dot_product = Vector.dot_product(new_unit, unit)
         self.assertAlmostEquals(dot_product, 1.0, delta=0.1)
Esempio n. 11
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)
Esempio n. 12
0
    def test_constructor(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)

        algo=BullockCircleFitting(expected_list)
        self.assertIsNotNone(algo)
        self.assertTrue (p1 in algo._points)
        self.assertTrue (p2 in algo._points)
        self.assertTrue (p3 in algo._points)
        self.assertEqual(len(algo._points),3)
        pass
Esempio n. 13
0
 def test_distance_of_origin_from_slope_45degrees_yintercept_is_zero(self):
     x = LineModel(-1, 1, 0)
     test_origin = Point(0, 0)
     distance_actual = x.compute_distance(test_origin)
     distance_expected = 0
     self.assertEqual(distance_actual, distance_expected)
     pass
Esempio n. 14
0
 def test_distance_of_0_0_from_flat_line_yintercept_is_zero(self):
     x = LineModel(0, 1, -3)
     test_point = Point(0, 0)
     distance_actual = x.compute_distance(test_point)
     distance_expected = 3
     self.assertEqual(distance_actual, distance_expected)
     pass
Esempio n. 15
0
 def test_distance_of_1_0_from_slope_45degrees_yintercept_is_zero(self):
     x = LineModel(-1, 1, 0)
     test_point = Point(1, 0)
     distance_actual = x.compute_distance(test_point)
     distance_expected = 1 / math.sqrt(2)
     self.assertEqual(distance_actual, distance_expected)
     pass
Esempio n. 16
0
    def __generate_xy_from_custom_function(self,image_array):
        max_distance=self._max_distance_consecutive_points
        x_start=0
        width=image_array.shape[1]
        height=image_array.shape[0]
        x_end=width
        y_origin=height/2

        delta_x=width*0.25 #an approx gap to being with
        x_last=x_start
        y_last=self.__InvokeAnyFunction(x_last ,width=width,height=height)+y_origin #add a new private functoin and property to decide which type of curve
        pts_new=list();
        while(x_last<x_end):
            gap=delta_x
            while(True):
                x_new=x_last+gap
                y_new=self.__InvokeAnyFunction(x_new,width=width,height=height)+y_origin
                dsquare=(x_new-x_last)**2 + (y_new-y_last)**2
                d=dsquare**0.5
                if (d <= max_distance):
                    pt_new=Point(x_new,y_new)
                    pts_new.append(pt_new)
                    x_last=x_new
                    y_last=y_new
                    break
                else:
                    gap=gap*0.5 #reduce the gap and try again
                    continue
        image_result=Util.superimpose_points_on_image(image_array,pts_new, 0,0,0)
        return image_result
        pass
Esempio n. 17
0
 def test_create_model_horizontal_through_y_equal_5(self):
     pass
     helper1=RansacLineHelper()
     lst=list()
     lst.append(Point(0,5))
     lst.append(Point(1,5))
     lst.append(Point(2,5))
     model=helper1.create_model(lst)
     #y=5
     #0x+1y-5=0
     expected_slope=0.0
     actual_slope=-model.A/model.B
     actual_yintercept=-model.C/model.B
     expected_yintercept=5
     self.assertAlmostEqual(actual_slope,expected_slope)
     self.assertAlmostEqual(actual_yintercept,expected_yintercept)
Esempio n. 18
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)
Esempio n. 19
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
Esempio n. 20
0
 def test_create_model_45_degrees_through_origin(self):
     pass
     helper1=RansacLineHelper()
     lst=list()
     lst.append(Point(0,0))
     lst.append(Point(1,1))
     lst.append(Point(2,2))
     model=helper1.create_model(lst)
     #
     #y=x
     #-x+y+0=0
     expected_slope=1.0
     actual_slope=-model.A/model.B
     actual_yintercept=-model.C/model.B
     expected_yintercept=0
     self.assertAlmostEqual(actual_slope,expected_slope)
     self.assertAlmostEqual(actual_yintercept,expected_yintercept)
Esempio n. 21
0
 def test_5points_around_origin_unit_radius(self):
     p1=Point(+1,0)
     p2=Point(+0,1)
     p3=Point(-1,0)
     p4=Point(+0.707,+0.707)
     p5=Point(-0.707,+0.707)
     expected_list=list()
     expected_list.append(p1)
     expected_list.append(p2)
     expected_list.append(p3)
     expected_list.append(p4)
     expected_list.append(p5)
     helper=BullockCircleFitting(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);
Esempio n. 22
0
    def test_projection_of_points_on_line_with_slope_45degrees_through_origin(
            self):
        line = LineModel(-1, 1, 0)
        test_point1 = Point(4, 0)
        expected_projected1 = Point(2, 2)

        test_point2 = Point(0, 4)
        expected_projected2 = Point(2, 2)

        lst_inputs = [test_point1, test_point2]
        lst_expected = [expected_projected1, expected_projected2]

        lst_actual = LineModel.compute_projection_of_points(line, lst_inputs)

        for index in range(0, len(lst_inputs)):
            actual = lst_actual[index]
            expected = lst_expected[index]
            self.assertAlmostEqual(actual.X, expected.X, 0.1)
            self.assertAlmostEqual(actual.Y, expected.Y, 0.1)
Esempio n. 23
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)
Esempio n. 24
0
def generate_points_for_circle(centerx, centery, r, density_factor):
    pts_on_circle = []
    num_points = int(2 * 3.141 * r *
                     density_factor)  #proportional to circumfrence
    angles = np.linspace(0, 2.0 * 3.141, num_points)
    for angle in angles:
        x = math.sin(angle) * r + centerx
        y = math.cos(angle) * r + centery
        newpoint = Point(x, y)
        pts_on_circle.append(newpoint)
    return pts_on_circle
    pass
Esempio n. 25
0
    def test_get_extreme_colinear_points(self):
        pt1 = Point(1, 1)
        pt2 = Point(2, 2)

        pt3 = Point(3, 3)
        pt4 = Point(4, 4)
        pt5 = Point(5, 5)
        pt6 = Point(6, 6)

        lst_randomsequence = [pt6, pt1, pt5, pt2, pt4, pt3]
        (point1, point2
         ) = Util.get_terminal_points_from_coliner_points(lst_randomsequence)
        results = [point1, point2]
        self.assertTrue(pt1 in results)
        self.assertTrue(pt6 in results)
        pass
Esempio n. 26
0
 def __superimpose(self, patches):
     lst_allpoints_from_all_patches = list()
     image_height = self.image.shape[0]
     for patch in patches:
         try:
             ransac_points: List[Point] = patch.ransacline.points
             plottable_points = Util.generate_plottable_points_from_projection_of_points(
                 patch.ransacline, patch.ransacline.points)
             for plottable_point in plottable_points:
                 new_x = patch.patchinfo.topleft.X + plottable_point.X
                 #new_y=patch.patchinfo.topleft.Y + plottable_point.Y
                 #new_y=(image_height-patch.patchinfo.topleft.Y) + plottable_point.Y
                 new_y = (image_height - patch.patchinfo.topleft.Y -
                          self._cropdimension) + plottable_point.Y
                 translated_point = Point(new_x, new_y)
                 lst_allpoints_from_all_patches.append(translated_point)
         except Exception as e:
             print("Exception while superimposing %s" % (patch.patchinfo))
     pass
     np_superimposed_patches = Util.superimpose_points_on_image(
         self._image, lst_allpoints_from_all_patches, 100, 255, 100)
     skimage.io.imsave(self.OutputImageFile, np_superimposed_patches)
     pass
Esempio n. 27
0
 def test_PointConstructionMultiple_ID_Must_Be_Sequential(self):
     p1 = Point(100, 200)
     p2 = Point(100, 200)
     p3 = Point(100, 200)
     self.assertEqual(p1.ID, p2.ID - 1)
     self.assertEqual(p2.ID, p3.ID - 1)
Esempio n. 28
0
 def test_create_vector(self):
     p1 = Point(1, 1)
     p2 = Point(2, 2)
     v = Vector.create_vector_from_2points(p1, p2)
     self.assertEquals(v.X, 1)
     self.assertEquals(v.Y, 1)
Esempio n. 29
0
 def test_PointConstruction_X_Y_values_Should_Match_Constructor_Params(
         self):
     p1 = Point(100, 200)
     self.assertEqual(p1.X, 100)
     self.assertEqual(p1.Y, 200)