Incorrect Result from itk::ConnectedComponentImageFilter

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:

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

Best,

–Davis

I will check this out.

1 Like

Thanks for bug report @DVigneault! Here is the PR.

1 Like

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

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

1 Like