Exemplo n.º 1
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)
Exemplo n.º 2
0
 def test_run_with_100x100_image(self):
     #
     #get a list of points
     #
     folder_script=os.path.dirname(__file__)
     filename_input="Line_100x100.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)
     #
     #initialize RansalHelper
     #
     helper1=RansacLineHelper()
     helper1.add_points(lst_points)
     helper1.max_iterations=20
     helper1.min_points_for_model=2
     helper1.threshold_error=10
     helper1.threshold_inlier_count=3
     result_model=helper1.run()
     print("RANSAC-complete")    
     print("Found model %s , polar=%s" % (result_model,result_model.display_polar()))
     #
     #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.Run.%d.png" % (filename_input,count_of_files) )
     file_result=os.path.join(folder_results,filename_results)
     x_lower=0
     x_upper=np_image.shape[1]-1
     y_lower=0
     y_upper=np_image.shape[0]-1
     #
     #Superimpose a line over the inliers only
     #
     new_points=Util.generate_plottable_points_from_projection_of_points(result_model,result_model.points)
     np_superimposed=Util.superimpose_points_on_image(np_image,new_points,100,255,100)
     skimage.io.imsave(file_result,np_superimposed)
     #11 inlier points in total which give us the good model
     self.assertEqual(len(result_model.points),11)
     #
     #No of detected inliers must be more than or equal to threshold
     #
     self.assertTrue(len(result_model.points) >= helper1.threshold_inlier_count,"Number of inliers should be >= threshold")
     #
     #There should be no-duplicates in the RANSAC inlier points
     #
     set_ids=set(map(lambda x: x.ID, result_model.points))
     list_ids=list(map(lambda x: x.ID, result_model.points))
     self.assertEqual(len(set_ids),len(list_ids),"Inliers should be unique")
     #
     #All the RANSAC linlier points must be within the threshold distance from the RANSAC line
     #
     for inlier_pt in result_model.points:
         distance=result_model.compute_distance(inlier_pt)
         self.assertTrue(distance < helper1.threshold_error,"Distance of inlier from RANSAC line must be less than threshold")
Exemplo n.º 3
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
Exemplo n.º 4
0
 def test_run_with_very_simple_image(self):
     #
     #get a list of points
     #
     folder_script=os.path.dirname(__file__)
     filename_input="Line_50x30.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)
     #
     #initialize RansalHelper
     #
     helper1=RansacLineHelper()
     helper1.add_points(lst_points)
     helper1.max_iterations=1000
     #10000 did not work
     helper1.min_points_for_model=2
     helper1.threshold_error=3 #10
     helper1.threshold_inlier_count=3
     result_model=helper1.run()
     print("RANSAC-complete")    
     print("Found model %s , polar=%s" % (result_model,result_model.display_polar()))
     #
     #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=("Line_50x30.%d.png" % (count_of_files) )
     file_result=os.path.join(folder_results,filename_results)
     x_lower=0
     x_upper=np_image.shape[1]-1
     y_lower=0
     y_upper=np_image.shape[0]-1
     new_points=LineModel.generate_points_from_line(result_model,x_lower,y_lower,x_upper,y_upper)
     np_superimposed=Util.superimpose_points_on_image(np_image,new_points,100,255,100)
     skimage.io.imsave(file_result,np_superimposed)
     #
     #Asserts!
     #
     x_intercept=result_model.xintercept()
     y_intercept=result_model.yintercept()
     self.assertTrue ( x_intercept > 30,"X intercept below threshold")
     self.assertTrue ( x_intercept < 50,"X intercept above threshold")
     self.assertTrue ( y_intercept > 30,"Y intercept above threshold")
     self.assertTrue ( y_intercept < 45,"Y intercept below threshold")
     self.assertTrue(len(result_model.points),5)
     for pt in result_model.points:
         distance_from_line=result_model.compute_distance(pt)
         self.assertTrue(distance_from_line <= helper1.threshold_error)
Exemplo n.º 5
0
    def find_line_using_ransac(self, points_in_patch: List[Point],
                               np_patchregion: np.ndarray):

        count_of_points_in_patch = len(points_in_patch)

        #create thresholds for RANSAC
        ransac_threshold_distance = (
            self._cropdimension) * 1 / 10  #should be adaptive
        ransac_mininliers = 0.5 * count_of_points_in_patch  #automatically try out various inliers

        helper = RansacLineHelper()
        helper.max_iterations = (count_of_points_in_patch) * (
            count_of_points_in_patch - 1) / 2 * 5  #mpy by 5
        helper.min_points_for_model = 2
        helper.threshold_error = ransac_threshold_distance
        helper.threshold_inlier_count = ransac_mininliers
        helper.add_points(points_in_patch)
        model = helper.run()
        return model
        pass
Exemplo n.º 6
0
def run_ransac(filename):
    folder_script = os.path.dirname(__file__)

    #Images which did not generate good results:
    #   NoisyImage_3.png
    #   NoisyLine-Gaussian-sp-0.80.111.png
    file_noisy_line = os.path.join(folder_script, "./input/", filename)
    np_image = skimage.io.imread(file_noisy_line, 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
    #
    ransac_maxiterations = 12000
    #12000
    #6000
    #12000 worked well
    ransac_minpoints = 5
    #5 worked well
    #2 gave very bad results
    #20 worked well
    ransac_threshold = 5
    #25 worked well for 'NoisyLine-Gaussian-sp-0.80.104.png' 15 and 5 did not
    #Nothing worked well for 'NoisyLine-Gaussian-sp-0.80.111.png" , tried increasing to 35
    #3 for first set when points were much closer
    #5 produced too much deviation

    ransac_mininliers = 10

    helper = RansacLineHelper()
    helper.max_iterations = ransac_maxiterations
    helper.min_points_for_model = ransac_minpoints
    helper.threshold_error = ransac_threshold
    helper.threshold_inlier_count = ransac_mininliers
    helper.add_points(lst_all_points)
    model = helper.run()

    #Display the model , you could render over the original picture
    print("-------------------------------------")
    print("RANSAC-complete")
    print("Found model %s , polar=%s" % (model, model.display_polar()))
    #
    #Generate an output image with the model line
    #
    filename_noextension = no_extension = os.path.splitext(filename)[0]
    now = datetime.datetime.now()
    filename_result = ("%s-%s.result.png") % (
        filename_noextension, 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_line, as_gray=True)
    new_points = LineModel.generate_points_from_line(
        model, 0, 0, np_image_result.shape[1] - 1,
        np_image_result.shape[0] - 1)
    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)