# Hierarchical merge on a Region Adjacency Graph

**URL:** <https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633>\
**Category:** General Usage\
**Tags:** images\
**Created:** [December 10, 2020, 10:30pm UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633 "2020-12-10T22:30:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsundram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsundram/32/20176_2.png) [@jsundram](https://discourse.julialang.org/u/jsundram)\
**Post date:** [December 10, 2020, 10:30pm UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/1 "2020-12-10T22:30:34Z")

</div>

I’m super new to Julia, and am porting over a python program of mine to Julia to get a feel for the language. I’ve constructed a region adjacency graph (RAG) for an image using `ImageSegmentation.region_adjacency_graph`.

I’d like to merge adjacent regions in the graph if they are similarly colored (`Colors.colordiff` of the regions’ mean color under some threshold). In Python’s scikit-image, there’s a handy [merge\_hierarchical function](https://github.com/scikit-image/scikit-image/blob/master/skimage/future/graph/graph_merge.py#L59) – is there something similar in Julia, or should I reimplement/port that function to my project?

I’m looking to accomplish something similar to what is posted about [here](https://vcansimplify.wordpress.com/2014/08/17/hierarchical-merging-of-region-adjacency-graphs/).

My code so far:

```julia
using Colors, Images, TestImages, ImageView, ImageSegmentation

lena = testimage("lena_color_512.tif")
seg = ImageSegmentation.felzenszwalb(lena, 10, 1000)
# imshow(map(i-> ImageSegmentation.segment_mean(segs, i), ImageSegmentation.labels_map(segs)))

weight(i, j) = Colors.colordiff(segment_mean(seg, i), segment_mean(seg, j))
G, vert_map = region_adjacency_graph(seg, weight)

# now, can I do a hierarchical merge of G with some color difference threshold?
```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [December 12, 2020, 9:15pm UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/2 "2020-12-12T21:15:49Z")

</div>

Welcome, @jsundram! I’m afraid I don’t know of such a function, but I bet it would be welcomed. Want to try writing it?

---

<div class="post-metadata">

**Author:** ![jsundram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsundram/32/20176_2.png) [@jsundram](https://discourse.julialang.org/u/jsundram)\
**Post date:** [December 14, 2020, 6:42pm UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/3 "2020-12-14T18:42:47Z")

</div>

Thanks, @tim.holy! I’ll take a crack at it over the next couple of weeks and will probably need some code review from the Julia experts here. 🙂

---

<div class="post-metadata">

**Author:** ![jsundram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsundram/32/20176_2.png) [@jsundram](https://discourse.julialang.org/u/jsundram)\
**Post date:** [January 28, 2021, 1:26am UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/4 "2021-01-28T01:26:35Z")

</div>

> [@tim.holy](#):
>
> Want to try writing it?

I took a stab at it in [this gist](https://gist.github.com/jsundram/64f3b77220ea1f4054bc69ece9d26ff3)– I’m open to thoughts about how to turn this into a PR that would be useful to others.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 28, 2021, 10:42am UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/5 "2021-01-28T10:42:17Z")

</div>

Thanks! I think submitting it as a PR to ImageSegmentation would be fine (and it would be easier to review that way since PRs have line-by-line comments). It would need tests and more documentation. (You’re aware that docstrings go _before_ the function you want to document, right?) A good example of the kind of documentation we strive for is

[https://github.com/JuliaImages/ImageSegmentation.jl/blob/15ea1ffe0dafb2edb28a97ef9ef6fe60cf5b731b/src/region\_growing.jl#L4-L55](https://github.com/JuliaImages/ImageSegmentation.jl/blob/15ea1ffe0dafb2edb28a97ef9ef6fe60cf5b731b/src/region_growing.jl#L4-L55)

The `# Examples` section is quite useful for newbies, and the `# Citation` section is great for those who want to learn more.

---

<div class="post-metadata">

**Author:** ![jsundram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jsundram/32/20176_2.png) [@jsundram](https://discourse.julialang.org/u/jsundram)\
**Post date:** [February 16, 2021, 2:21am UTC](https://discourse.julialang.org/t/hierarchical-merge-on-a-region-adjacency-graph/51633/6 "2021-02-16T02:21:53Z")

</div>

Thanks @tim.holy! It took me a bit of time, but I have fixed and expanded the docstrings and written some tests. I submitted [#64](https://github.com/JuliaImages/ImageSegmentation.jl/pull/64/); look forward to your thoughts.
