.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/segmentation/plot_rag.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. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_segmentation_plot_rag.py: ============================== Region Adjacency Graphs (RAGs) ============================== This example demonstrates the use of the `merge_nodes` function of a Region Adjacency Graph (RAG). The `RAG` class represents an undirected weighted graph which inherits from :obj:`networkx.Graph` class. When a new node is formed by merging two nodes, the edge weight of all the edges incident on the resulting node can be updated by a user defined function `weight_func`. The default behaviour is to use the smaller edge weight in case of a conflict. The example below also shows how to use a custom function to select the larger weight instead. .. GENERATED FROM PYTHON SOURCE LINES 17-85 .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_rag_001.png :alt: Original Graph :srcset: /auto_examples/segmentation/images/sphx_glr_plot_rag_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_rag_002.png :alt: Merged with default (min) :srcset: /auto_examples/segmentation/images/sphx_glr_plot_rag_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/segmentation/images/sphx_glr_plot_rag_003.png :alt: Merged with max without in_place :srcset: /auto_examples/segmentation/images/sphx_glr_plot_rag_003.png :class: sphx-glr-multi-img .. code-block:: Python import skimage as ski import networkx as nx from matplotlib import pyplot as plt import numpy as np def max_edge(g, src, dst, n): """Callback to handle merging nodes by choosing maximum weight. Returns a dictionary with `"weight"` set as either the weight between (`src`, `n`) or (`dst`, `n`) in `g` or the maximum of the two when both exist. Parameters ---------- g : RAG The graph under consideration. src, dst : int The vertices in `g` to be merged. n : int A neighbor of `src` or `dst` or both. Returns ------- data : dict A dict with the "weight" attribute set the weight between (`src`, `n`) or (`dst`, `n`) in `g` or the maximum of the two when both exist. """ w1 = g[n].get(src, {'weight': -np.inf})['weight'] w2 = g[n].get(dst, {'weight': -np.inf})['weight'] return {'weight': max(w1, w2)} def display(g, title): """Displays a graph with the given title.""" pos = nx.circular_layout(g) plt.figure() plt.title(title) nx.draw(g, pos) nx.draw_networkx_labels(g, pos) nx.draw_networkx_edge_labels(g, pos, font_size=20) g = ski.graph.RAG() g.add_edge(1, 2, weight=10) g.add_edge(2, 3, weight=20) g.add_edge(3, 4, weight=30) g.add_edge(4, 1, weight=40) g.add_edge(1, 3, weight=50) # Assigning dummy labels. for n in g.nodes(): g.nodes[n]['labels'] = [n] gc = g.copy() display(g, "Original Graph") g.merge_nodes(1, 3) display(g, "Merged with default (min)") gc.merge_nodes(1, 3, weight_func=max_edge, in_place=False) display(gc, "Merged with max without in_place") plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.405 seconds) .. _sphx_glr_download_auto_examples_segmentation_plot_rag.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.24.0?filepath=notebooks/auto_examples/segmentation/plot_rag.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rag.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rag.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_