.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/gallery/contours/boundary_chan_vese.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_gallery_contours_boundary_chan_vese.py: ===================================== Adding Chan-Vese Contours to an Image ===================================== This example shows how to add a contour based on the chan-vese segmentation of the image. The Chan-vese algorithm is a level set method for image segmentation. It is based on the Mumford-Shah functional and is designed to segment images without edges. In this example, we will the `ripplemapper.analyse` module to segment the image and add the resulting contour to the image. The CV segmentation is a more unreliable way to form the contours. .. GENERATED FROM PYTHON SOURCE LINES 17-18 We can load an image into a RippleImage object using the RippleImage class and then run `add_chan_vese_contours` to add Chan-Vese contours to the image. .. GENERATED FROM PYTHON SOURCE LINES 18-28 .. code-block:: Python import matplotlib.pyplot as plt from ripplemapper.analyse import add_chan_vese_contours from ripplemapper.classes import RippleImage from ripplemapper.data.example import example_data ripple_img = RippleImage(example_data[2]) add_chan_vese_contours(ripple_img) .. GENERATED FROM PYTHON SOURCE LINES 29-30 Plotting the image and its contours .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: Python ripple_img.plot(include_contours=True) plt.show() .. image-sg:: /generated/gallery/contours/images/sphx_glr_boundary_chan_vese_001.png :alt: RippleImage: 1_00375_sample.tif :srcset: /generated/gallery/contours/images/sphx_glr_boundary_chan_vese_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 35-37 We can also add the Chan-Vese contours to a image where we use a combination of the divergence of the image and the regular image. This might improve the segmentation technique .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python add_chan_vese_contours(ripple_img, use_gradients=True, overwrite=True) ripple_img.plot(include_contours=True) plt.show() .. image-sg:: /generated/gallery/contours/images/sphx_glr_boundary_chan_vese_002.png :alt: RippleImage: 1_00375_sample.tif :srcset: /generated/gallery/contours/images/sphx_glr_boundary_chan_vese_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/docs/checkouts/readthedocs.org/user_builds/ripplemapper/envs/latest/lib/python3.12/site-packages/ripplemapper/analyse.py:151: UserWarning: Overwriting Chan-Vese contour for image: /home/docs/checkouts/readthedocs.org/user_builds/ripplemapper/envs/latest/lib/python3.12/site-packages/ripplemapper/data/example/1_00375_sample.tif warnings.warn(f"Overwriting Chan-Vese contour for image: {ripple_image.source_file}") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.568 seconds) .. _sphx_glr_download_generated_gallery_contours_boundary_chan_vese.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: boundary_chan_vese.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: boundary_chan_vese.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: boundary_chan_vese.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_