Hierarchical merge on a Region Adjacency Graph

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 – 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.

My code so far:

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?

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?


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. :slight_smile:

1 Like

I took a stab at it in this gist– I’m open to thoughts about how to turn this into a PR that would be useful to others.

1 Like

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

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

1 Like

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; look forward to your thoughts.