Region adjacency graph (RAG) Merging
import
matplotlib.pyplot
as
plt
def
_weight_mean_color
(
graph
,
src
,
dst
,
n
):
"""Callback to handle merging nodes by recomputing mean color.
The method expects that the mean color of `dst` is already computed.
Parameters
----------
graph : RAG
The graph under consideration.
src, dst : int
The vertices in `graph` to be merged.
n : int
A neighbor of `src` or `dst` or both.
Returns
-------
data : dict
A dictionary with the `"weight"` attribute set as the absolute
difference of the mean color between node `dst` and `n`.
"""
diff
=
graph
.
nodes
[
dst
][
'mean color'
]
-
graph
.
nodes
[
n
][
'mean color'
]
diff
=
(
diff
)
return
{
'weight'
:
diff
}
def
merge_mean_color
(
graph
,
src
,
dst
):
"""Callback called before merging two nodes of a mean color distance graph.
This method computes the mean color of `dst`.
Parameters
----------
graph : RAG
The graph under consideration.
src, dst : int
The vertices in `graph` to be merged.
"""
graph
.
nodes
[
dst
][
'total color'
]
+=
graph
.
nodes
[
src
][
'total color'
]
graph
.
nodes
[
dst
][
'pixel count'
]
+=
graph
.
nodes
[
src
][
'pixel count'
]
graph
.
nodes
[
dst
][
'mean color'
]
=
(
graph
.
nodes
[
dst
][
'total color'
]
/
graph
.
nodes
[
dst
][
'pixel count'
]
)
=
()
=
(
,
compactness
=
30
,
n_segments
=
400
,
start_label
=
1
)
=
(
,
)
=
(
,
,
thresh
=
35
,
rag_copy
=
False
,
in_place_merge
=
True
,
merge_func
=
merge_mean_color
,
weight_func
=
_weight_mean_color
,
)
=
(
,
,
kind
=
'avg'
,
bg_label
=
0
)
=
(
,
,
(
0
,
0
,
0
))
,
=
()
(
)
()
()
Total running time of the script: (0 minutes 2.808 seconds)