# Incorrect Result from itk::ConnectedComponentImageFilter

**URL:** <https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676>\
**Category:** Uncategorized\
**Created:** [February 3, 2020, 6:21am UTC](https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676 "2020-02-03T06:21:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DVigneault](https://discourse.itk.org/user_avatar/discourse.itk.org/dvigneault/32/189_2.png) [@DVigneault](https://discourse.itk.org/u/DVigneault)\
**Post date:** [February 3, 2020, 6:21am UTC](https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676/1 "2020-02-03T06:21:04Z")

</div>

All–

I’ve encountered what I believe to be a bug in the behavior of `itk::ConnectedComponentImageFilter`. In the below example, I create a 2x2x2 “Checkerboard” image, where there are 4 foreground pixels, none of which share a face. I then use the `itk::ConnectedComponentImageFilter` to label the pixels, with `FullyConnected` set to `Off`. In the result, three pixels are correctly labeled, but the pixel at `[1, 0, 1]` is incorrectly labeled as being “connected” to the pixel at `[1, 1, 0]`, although they do not share a face.

Here is a MCE:

```
#include <itkConnectedComponentImageFilter.h>

using TImage = itk::Image<unsigned char, 3>; 
using TConnected = itk::ConnectedComponentImageFilter< TImage, TImage >;

int main() {

  std::bitset<8> bits(105); // 3D Checkerboard
  std::cout << bits << std::endl; // 01101001

  const auto image = TImage::New();
  TImage::RegionType region({{0,0,0}}, {{2,2,2}});

  image->SetRegions( region );
  image->Allocate();
  image->FillBuffer( 0 );

  for (size_t i = 0; i < 8; ++i) {
    std::bitset<3> idx(i);
    image->SetPixel( {{idx[0], idx[1], idx[2]}}, bits[i] );
  }

  const auto connected = TConnected::New();
  connected->SetInput( image );
  connected->FullyConnectedOff();
  connected->Update();

  itk::ImageRegionConstIterator<TImage> it(connected->GetOutput(), connected->GetOutput()->GetLargestPossibleRegion());
 
  while (!it.IsAtEnd()) {
    std::cout << "Index : Label => " << it.GetIndex() << " : " << static_cast<int>(it.Get()) << std::endl;
    ++it;
  }

  // Index : Label => [0, 0, 0] : 1
  // Index : Label => [1, 0, 0] : 0
  // Index : Label => [0, 1, 0] : 0
  // Index : Label => [1, 1, 0] : 2
  // Index : Label => [0, 0, 1] : 0
  // Index : Label => [1, 0, 1] : 2 <= Incorrect Label
  // Index : Label => [0, 1, 1] : 3
  // Index : Label => [1, 1, 1] : 0

  return EXIT_SUCCESS;

}

```

And here is a screenshot of the output, where I’ve extracted the labeled image as a mesh where each box (corresponding to one pixel) is colored according to its pixel value. Note that none of the four foreground pixels share a face, and yet two pixels have the same label:

 ![Screen Shot 2020-02-03 at 12.46.55 AM](https://discourse.itk.org/uploads/default/original/2X/1/13ede2550b8e8da20450123ac6a39b37f7e07f9a.jpeg)

Thanks in advance, and apologies if there is an error somewhere in my logic.

Best,

–Davis

---

<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:** [February 3, 2020, 4:24pm UTC](https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676/2 "2020-02-03T16:24:05Z")

</div>

I will check this out.

---

<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:** [February 3, 2020, 5:13pm UTC](https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676/3 "2020-02-03T17:13:17Z")

</div>

Thanks for bug report @DVigneault! Here is the [PR](https://github.com/InsightSoftwareConsortium/ITK/pull/1589).

---

<div class="post-metadata">

**Author:** ![DVigneault](https://discourse.itk.org/user_avatar/discourse.itk.org/dvigneault/32/189_2.png) [@DVigneault](https://discourse.itk.org/u/DVigneault)\
**Post date:** [February 4, 2020, 4:36am UTC](https://discourse.itk.org/t/incorrect-result-from-itk-connectedcomponentimagefilter/2676/4 "2020-02-04T04:36:42Z")

</div>

Excellent, thank you!! This PR seems to have fixed all my internal test cases. Here’s the above example with the new PR:

 ![Screen Shot 2020-02-03 at 11.35.03 PM](https://discourse.itk.org/uploads/default/original/2X/e/eb5e0c95038fcf8ebda5243678809380ce87b7a4.jpeg)

Will review on GitHub–can’t thank you enough!
