.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/2d_tv_segmentation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_2d_tv_segmentation.py: 07. Segmentation ================ This example shows the interface usage for class-based segmentation of a 2D image. First, depending on the size of the weighting lambda, a piecewise constant image is generated before the assignment to certain classes is done. .. GENERATED FROM PYTHON SOURCE LINES 10-12 TV based segmentation .. GENERATED FROM PYTHON SOURCE LINES 12-45 .. code-block:: default import skimage.data as skd import numpy as np import matplotlib.pyplot as plt from recon.interfaces import Segmentation def rgb2gray(rgb): r, g, b = rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2] gray = 0.2989 * r + 0.5870 * g + 0.1140 * b return gray gt = rgb2gray(skd.coffee())[:, 80:481] gt = gt/np.max(gt) gt = gt/np.max(gt) classes = [0, 50/255, 120/255, 190/255, 220/255] segmentation = Segmentation(gt.shape, classes=classes, lam=5, tau='calc') result, _ = segmentation.solve(gt, max_iter=4000) f = plt.figure(figsize=(8, 4)) f.add_subplot(1, 2, 1) plt.axis('off') plt.imshow(gt) plt.title("GT") f.add_subplot(1, 2, 2) plt.imshow(result) plt.title("TV-based segmentation") plt.axis('off') plt.show(block=False) .. image:: /tutorials/images/sphx_glr_2d_tv_segmentation_001.png :alt: GT, TV-based segmentation :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Early stopping. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 40.695 seconds) .. _sphx_glr_download_tutorials_2d_tv_segmentation.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 2d_tv_segmentation.py <2d_tv_segmentation.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 2d_tv_segmentation.ipynb <2d_tv_segmentation.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_