def test_line_model_estimate(): # generate original data without noise model0 = LineModel() model0.params = (10, 1) x0 = np.arange(-100, 100) y0 = model0.predict_y(x0) data0 = np.column_stack([x0, y0]) # add gaussian noise to data np.random.seed(1234) data = data0 + np.random.normal(size=data0.shape) # estimate parameters of noisy data model_est = LineModel() model_est.estimate(data) # test whether estimated parameters almost equal original parameters assert_almost_equal(model0.params, model_est.params, 1)
def test_line_model_estimate(): # generate original data without noise model0 = LineModel() model0._params = (10, 1) x0 = np.arange(-100, 100) y0 = model0.predict_y(x0) data0 = np.column_stack([x0, y0]) # add gaussian noise to data np.random.seed(1234) data = data0 + np.random.normal(size=data0.shape) # estimate parameters of noisy data model_est = LineModel() model_est.estimate(data) # test whether estimated parameters almost equal original parameters assert_almost_equal(model0._params, model_est._params, 1)
def test_line_model_estimate(): # generate original data without noise model0 = LineModel() model0.params = (0, 1) x0 = np.arange(-100, 100) y0 = model0.predict_y(x0) data0 = np.column_stack([x0, y0]) data = data0 + (np.random.random(data0.shape) - 0.5) # estimate parameters of noisy data model_est = LineModel() model_est.estimate(data) assert_almost_equal(model_est.residuals(data0), np.zeros(len(data)), 1) # test whether estimated parameters almost equal original parameters random_state = np.random.RandomState(1234) x = random_state.rand(100, 2) assert_almost_equal(model0.predict_y(x), model_est.predict_y(x), 1)
def RANSAC(image): np.random.seed(seed=1) # generate coordinates of line x = np.arange(-200, 200) y = 0.2 * x + 20 data = np.column_stack([x, y]) # add faulty data faulty = np.array(30 * [(180., -100)]) faulty += 5 * np.random.normal(size=faulty.shape) data[:faulty.shape[0]] = faulty # add gaussian noise to coordinates noise = np.random.normal(size=data.shape) data += 0.5 * noise data[::2] += 5 * noise[::2] data[::4] += 20 * noise[::4] # fit line using all data model = LineModel() model.estimate(data) # robustly fit line only using inlier data with RANSAC algorithm model_robust, inliers = ransac(data, LineModel, min_samples=2, residual_threshold=1, max_trials=1000) outliers = inliers == False # generate coordinates of estimated models line_x = np.arange(-250, 250) line_y = model.predict_y(line_x) line_y_robust = model_robust.predict_y(line_x) plt.plot(data[inliers, 0], data[inliers, 1], '.b', alpha=0.6, label='Inlier data') plt.plot(data[outliers, 0], data[outliers, 1], '.r', alpha=0.6, label='Outlier data') plt.plot(line_x, line_y, '-k', label='Line model from all data') plt.plot(line_x, line_y_robust, '-b', label='Robust line model') plt.legend(loc='lower left') plt.show()
def keep_border_points(cloud_pts, convex_hull_threshold): ''' Return the points that are localized onto the convex hull of the cloud of input points. Input and output are (N,2) numpy array ''' convexHulls = convex_hull(cloud_pts.tolist()) convexHulls[:+1][:] = convexHulls[0][:] nsegments = len(convexHulls) to_keep = np.zeros(len(cloud_pts), dtype=bool) for P1, P2 in zip(convexHulls[:-1][:], convexHulls[1:][:]): data = np.asarray([P1, P2]) model = LineModel() model.estimate(data) to_keep = np.logical_or( to_keep, abs(model.residuals(cloud_pts)) < convex_hull_threshold) return cloud_pts[to_keep, :]
data = np.column_stack([x, y]) # add faulty data faulty = np.array(30 * [(180., -100)]) faulty += 5 * np.random.normal(size=faulty.shape) data[:faulty.shape[0]] = faulty # add gaussian noise to coordinates noise = np.random.normal(size=data.shape) data += 0.5 * noise data[::2] += 5 * noise[::2] data[::4] += 20 * noise[::4] # fit line using all data model = LineModel() model.estimate(data) # robustly fit line only using inlier data with RANSAC algorithm model_robust, inliers = ransac(data, LineModel, min_samples=2, residual_threshold=1, max_trials=1000) outliers = inliers == False # generate coordinates of estimated models line_x = np.arange(-250, 250) line_y = model.predict_y(line_x) line_y_robust = model_robust.predict_y(line_x) fig, ax = plt.subplots() ax.plot(data[inliers, 0], data[inliers, 1], '.b', alpha=0.6, label='Inlier data') ax.plot(data[outliers, 0], data[outliers, 1], '.r', alpha=0.6,
data = np.column_stack([x, y]) # add faulty data faulty = np.array(30 * [(180, -100)]) faulty += 5 * np.random.normal(size=faulty.shape) data[:faulty.shape[0]] = faulty # add gaussian noise to coordinates noise = np.random.normal(size=data.shape) data += 0.5 * noise data[::2] += 5 * noise[::2] data[::4] += 20 * noise[::4] # fit line using all data model = LineModel() model.estimate(data) # robustly fit line only using inlier data with RANSAC algorithm model_robust, inliers = ransac(data, LineModel, min_samples=2, residual_threshold=1, max_trials=1000) outliers = inliers == False # generate coordinates of estimated models line_x = np.arange(-250, 250) line_y = model.predict_y(line_x) line_y_robust = model_robust.predict_y(line_x) plt.plot(data[inliers, 0], data[inliers, 1], '.b', alpha=0.6, label='Inlier data') plt.plot(data[outliers, 0], data[outliers, 1], '.r', alpha=0.6, label='Outlier data')