# Clustering/labeling B/W or true/false 3D image

**URL:** https://discourse.julialang.org/t/clustering-labeling-b-w-or-true-false-3d-image/21004
**Category:** New to Julia
**Created:** [February 20, 2019, 7:55am UTC](https://discourse.julialang.org/t/clustering-labeling-b-w-or-true-false-3d-image/21004 "2019-02-20T07:55:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![KirillGerke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirillgerke/32/11383_2.png) [@KirillGerke](https://discourse.julialang.org/u/KirillGerke)
#### Post date: [February 20, 2019, 7:55am UTC](https://discourse.julialang.org/t/clustering-labeling-b-w-or-true-false-3d-image/21004/1 "2019-02-20T07:55:44Z")

</div>

Dear All,

Is there any functionality in Julia to label (divide into separate clusters) 3D binary (0 and 1 or true/false) images?

I could find two functions in the JuliaImages, but with the lack documentation i was unable to figure out if this is exactly what i need (most likely works only for 2D + it seems that seg below is in some special format):

1. img\_labeled = labels\_map(seg)
2. labels = segment\_labels(seg)

Many thanks in advance for help!

---

<div class="post-metadata">

### Author: ![zygmuntszpak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zygmuntszpak/32/2591_2.png) [@zygmuntszpak](https://discourse.julialang.org/u/zygmuntszpak)
#### Post date: [February 21, 2019, 10:48pm UTC](https://discourse.julialang.org/t/clustering-labeling-b-w-or-true-false-3d-image/21004/2 "2019-02-21T22:48:25Z")

</div>

Hi,

If I understood you correctly you would like to perform [connected component labeling](https://en.wikipedia.org/wiki/Connected-component_labeling) on a 3D volume. If that is the case then the function you seek is `label_components` which is defined in the `Images` package.

The first parameter to the function is the multidimensional array that you would like to label, and the second parameter is a multidimensional array which specifies the _connectivity_ between pixels. For example, in two dimensions 8-connectivity is specified by a 3 by 3 matrix, e.g. `trues(3,3)`. In 3-dimensions, 26-connectivity is specified by a 3 by 3 by 3 matrix, e.g. `trues(3,3,3)`.

The following code snippet will return a multidimensional matrix `labels` where every voxel is given the label `1` since all voxels in `test_volume` constitute a single connected component.

```julia
test_volume = ones(Int, 5, 5, 5)
labels = label_components(test_volume , trues(3,3,3)) 

```

---

<div class="post-metadata">

### Author: ![KirillGerke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirillgerke/32/11383_2.png) [@KirillGerke](https://discourse.julialang.org/u/KirillGerke)
#### Post date: [March 25, 2019, 4:16pm UTC](https://discourse.julialang.org/t/clustering-labeling-b-w-or-true-false-3d-image/21004/3 "2019-03-25T16:16:50Z")

</div>

Hello Zygmunt,

Sorry for late reply - i somehow missed your answer here.  
I actually did it basically exactly as you proposed here. But let it stay for future references.

Thank you very much!  
K.
