JuliaImages / ImageSegmentation.jl

Partitioning images into meaningful regions
Other
47 stars 23 forks source link

Added SRG algorithm #1

Closed annimesh2809 closed 7 years ago

annimesh2809 commented 7 years ago

An Implementation of the Seeded Region Growing Algorithm (Improved version) as explained in this paper. It can currently be used with various color models (like RGB, RGBA, GrayA etc) and also works with arrays having custom indices (like OffsetArrays). Multiple points can be given the same label to indicate that they belong to the same region. The srg function can be used as:

img_seg = srg(img, seeds, diff_fn)

Here, img is the input image seeds is a vector of Tuple{CartesianIndex{2}, Int}. Each seed corresponds to a point on the image along with a label (which is of type Int). This label is used to identify a region uniquely in the segmented image. diff_fn is a function to calculate δ between a region and a neighboring point.

Currently, the output is an array having indices similar to that of the input image. Each point of this array contains a label corresponding to the region to which it belongs. The label can be any of the seed labels or a special value (-4) that corresponds to a point that has same δ value for two (or more) neighbors (boundary tied points).

An example:

using Images, ImageSegmentation, ImageView

img = load("img.jpg")
img = centered(img)
imshow(img)
seeds = [(CartesianIndex(-54,-14),1), (CartesianIndex(66,-14), 2), (CartesianIndex(1,77), 3), (CartesianIndex(85,-83),4), (CartesianIndex(-79,96), 4)]
img_s = srg(img, seeds)
imshow(map(i->((i==3)?RGB(0,1,0):(i==2)?RGB(0,0,0):(i==1)?RGB(1,0,0):RGB(1,1,1)), img_s))

Original: org

After segmentation (before changes): seg

After segmentation (after changes): seg

There are a few things which need to be discussed:

Once we settle for an API, the documentation and tests could be added.

annimesh2809 commented 7 years ago

UPDATES:

A nice demo:

Original: org2

After segmentation: seg2

timholy commented 7 years ago

Sweet! That looks really nice!

annimesh2809 commented 7 years ago

Docstring and tests have been added!

The improvement can be seen by comparing these to the previous outputs:

Original: org

After segmentation (7 labels, 7 seeds): segmax


Original: org2

After segmentation (2 labels, 8 seeds): segmax2

The color of the ith segment corresponds to seg.segment_means[i]

codecov[bot] commented 7 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@582387a). Click here to learn what that means. The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##             master     #1   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?      2           
  Lines             ?     95           
  Branches          ?      0           
=======================================
  Hits              ?     95           
  Misses            ?      0           
  Partials          ?      0
Impacted Files Coverage Δ
src/region_growing.jl 100% <100%> (ø)
src/core.jl 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 582387a...32b5c9e. Read the comment docs.

annimesh2809 commented 7 years ago

I think it is ready to merge. The travis and appveyor builds are fine with Julia 0.5, although they fail with Julia 0.7 due to LoadError in MacroTools.jl. Just wanted to ask, is the field-name img of SegmentedImage descriptive enough or should we change it? I could not think of any nice word to describe a map from indices to labels.

timholy commented 7 years ago

labeled_image comes to mind, although if I understand correctly that might be a bit misleading because it's really the indices to the labels. Would it make sense to bundle both img and segment_labels together into an IndirectArray? If you don't want to do that, perhaps image_indexmap?

timholy commented 7 years ago

And by all means hit "merge" when you are ready.