def test_deprecated_params_attribute():
    model = LineModel()
    model.params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    with expected_warnings(['`_params`']):
        assert_equal(model.params, model._params)
Example #2
0
def test_deprecated_params_attribute():
    model = LineModel()
    model.params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    with expected_warnings(['`_params`']):
        assert_equal(model.params, model._params)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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()
Example #7
0
def test_line_model_predict():
    model = LineModel()
    model._params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    assert_almost_equal(x, model.predict_x(y))
Example #8
0
def test_deprecated_params_attribute():
    model = LineModel()
    model.params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    assert_equal(model.params, model._params)
Example #9
0
def test_deprecated_params_attribute():
    model = LineModel()
    model.params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    assert_equal(model.params, model._params)
Example #10
0
def test_line_model_predict():
    model = LineModel()
    model.params = (10, 1)
    x = np.arange(-10, 10)
    y = model.predict_y(x)
    assert_almost_equal(x, model.predict_x(y))
Example #11
0
# 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,
        label='Outlier data')
ax.plot(line_x, line_y, '-k', label='Line model from all data')
ax.plot(line_x, line_y_robust, '-b', label='Robust line model')
ax.legend(loc='lower left')
plt.show()
Example #12
0
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()