

.. _sphx_glr_auto_examples_transform_plot_ransac3D.py:


============================================
Robust 3D line model estimation using RANSAC
============================================

In this example we see how to robustly fit a 3D line model to faulty data using
the RANSAC algorithm.





.. image:: /auto_examples/transform/images/sphx_glr_plot_ransac3D_001.png
    :align: center





.. code-block:: python

    import numpy as np
    from matplotlib import pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    from skimage.measure import LineModelND, ransac

    np.random.seed(seed=1)

    # generate coordinates of line
    point = np.array([0, 0, 0], dtype='float')
    direction = np.array([1, 1, 1], dtype='float') / np.sqrt(3)
    xyz = point + 10 * np.arange(-100, 100)[..., np.newaxis] * direction

    # add gaussian noise to coordinates
    noise = np.random.normal(size=xyz.shape)
    xyz += 0.5 * noise
    xyz[::2] += 20 * noise[::2]
    xyz[::4] += 100 * noise[::4]

    # robustly fit line only using inlier data with RANSAC algorithm
    model_robust, inliers = ransac(xyz, LineModelND, min_samples=2,
                                   residual_threshold=1, max_trials=1000)
    outliers = inliers == False

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(xyz[inliers][:, 0], xyz[inliers][:, 1], xyz[inliers][:, 2], c='b',
               marker='o', label='Inlier data')
    ax.scatter(xyz[outliers][:, 0], xyz[outliers][:, 1], xyz[outliers][:, 2], c='r',
               marker='o', label='Outlier data')
    ax.legend(loc='lower left')
    plt.show()

**Total running time of the script:** ( 0 minutes  0.146 seconds)



.. only :: html

 .. container:: sphx-glr-footer


  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_ransac3D.py <plot_ransac3D.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_ransac3D.ipynb <plot_ransac3D.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
