mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-28 04:39:13 +08:00
55f5103dd8
Modified travis_script.sh to account for the new structure of the gallery Added README.txt files in directories of gallery examples Fixed references to gallery images in user guide pages Fixed broken links
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""
|
|
=========================================
|
|
Robust line model estimation using RANSAC
|
|
=========================================
|
|
|
|
In this example we see how to robustly fit a line model to faulty data using
|
|
the RANSAC algorithm.
|
|
|
|
"""
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
|
|
from skimage.measure import LineModel, ransac
|
|
|
|
|
|
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)
|
|
|
|
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()
|