# How to extract labels of connected components?

**URL:** https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469
**Category:** Beginner Questions
**Created:** [September 18, 2020, 10:24am UTC](https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469 "2020-09-18T10:24:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Enr\_Brunetti](https://discourse.itk.org/user_avatar/discourse.itk.org/enr_brunetti/32/1709_2.png) [@Enr\_Brunetti](https://discourse.itk.org/u/Enr_Brunetti)
#### Post date: [September 18, 2020, 10:24am UTC](https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469/1 "2020-09-18T10:24:15Z")

</div>

I have used the itk::ConnectedComponentImageFilter. For each slice I have a certain number of connected component. I want to obtain a label associated to each of these ones, but with the following code I obtain only one label for each slice.

using ConnectedComponentImageFilterType = itk::ConnectedComponentImageFilter\<ImageTypeUC, ImageTypeUC, ImageTypeUC\>;  
ConnectedComponentImageFilterType::Pointer connesso= ConnectedComponentImageFilterType::New();  
connesso-\>SetInput(ITKIMAGE);  
connesso-\>GetFullyConnected();  
connesso-\>GetObjectCount();  
connesso-\>Update();  
connesso-\>GetOutput();  
cout \<\< "Number of connected components: " \<\< connesso-\>GetObjectCount() \<\< ‘\t’;  
cout \<\< "Labels associated: " \<\< connesso-\>GetOutput() \<\< ‘\t’;

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [September 18, 2020, 2:49pm UTC](https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469/2 "2020-09-18T14:49:40Z")

</div>

Output image type (second template parameter) needs to be large enough to accommodate all the components. You are currently using `ImageTypeUC` for it. Try US or UL. If your `connesso->GetObjectCount()` is greater than 255 that is almost certainly the problem.

---

<div class="post-metadata">

### Author: ![PS1997](https://discourse.itk.org/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@PS1997](https://discourse.itk.org/u/PS1997)
#### Post date: [March 20, 2021, 7:51pm UTC](https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469/3 "2021-03-20T19:51:19Z")

</div>

So what is the solution to get the labels of all the connected components in each slice

is there any way that we can see size and location of each connected componenent using label

sorry i am new to itk

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 22, 2021, 2:59pm UTC](https://discourse.itk.org/t/how-to-extract-labels-of-connected-components/3469/4 "2021-03-22T14:59:42Z")

</div>

Take a look at [SliceBySliceImageFilter](https://itk.org/Doxygen/html/classitk_1_1SliceBySliceImageFilter.html) or [ProcessA2DSliceOfA3DImage](https://itk.org/ITKExamples/src/Filtering/ImageGrid/ProcessA2DSliceOfA3DImage/Documentation.html) example.
