[Insight-users] ConnectedComponentFunctorImageFilter and background pixels

Bastian A. bastianra at gmail.com
Thu Aug 29 14:54:22 EDT 2013


Hi all,

I am trying to find connected components having the same pixel values,
using ConnectedComponentFunctorImageFilter with the equal functor.

The Filter correctly identifies connected components,
however background pixels are considered to be among of the
connected components. I'd expect that pixels that have the value
specified using SetBackgroundValue() are considered background pixels.
Am I missing something, or is this a bug?

Incidentially, GetObjectCount() always returns 0.

Thanks,
Bastian

Below is some example code that illustrates the issue (based on
http://www.itk.org/Wiki/ITK/Examples/ImageProcessing/ConnectedComponentImageFilter
):

####################################################
ConnectedComponent.cxx:
###################################################

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkConnectedComponentImageFilter.h"
#include <itkConnectedComponentFunctorImageFilter.h>
#include "itkLabelToRGBImageFilter.h"
#include <itkLogicOpsFunctors.h>
#include "itksys/SystemTools.hxx"
#include <sstream>

#include "QuickView.h"

template <typename TImage>
static void CreateImage(TImage* const image);

int main( int argc, char *argv[])
{
  const unsigned int Dimension = 2;
  typedef unsigned char                       PixelType;
  typedef itk::RGBPixel<unsigned char>         RGBPixelType;
  typedef itk::Image<PixelType, Dimension>     ImageType;
  typedef itk::Image<RGBPixelType, Dimension>  RGBImageType;

  ImageType::Pointer image;
  if( argc < 2 )
    {
    image = ImageType::New();
    CreateImage(image.GetPointer());
    }
  else
    {
    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();

    image = reader->GetOutput();
    }

  typedef itk::Image< unsigned short, Dimension > OutputImageType;


typedef itk::Functor::Equal< ImageType::PixelType, ImageType::PixelType,
ImageType::PixelType > EqualFunctorType;
typedef itk::ConnectedComponentFunctorImageFilter< ImageType,
OutputImageType, EqualFunctorType, ImageType >
 ConnectedComponentFilterType;
ConnectedComponentFilterType::Pointer connected =
ConnectedComponentFilterType::New();
connected->SetInput(image);
connected->SetBackgroundValue(0);
connected->Update();



  std::cout << "Number of objects: " << connected->GetObjectCount() <<
std::endl;

  typedef itk::LabelToRGBImageFilter<OutputImageType, RGBImageType>
RGBFilterType;
  RGBFilterType::Pointer rgbFilter = RGBFilterType::New();
  rgbFilter->SetInput( connected->GetOutput() );

  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(),true,
    argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated
image");

  std::stringstream desc;
  desc << "Connected Components: "
       << connected->GetObjectCount() << " objects";
  viewer.AddRGBImage(
    rgbFilter->GetOutput(),
    true,
    desc.str());

  viewer.Visualize();

  return EXIT_SUCCESS;
}

template <typename TImage>
void CreateImage(TImage* const image)
{
  // Create an image with 2 connected components
  typename TImage::IndexType start = {{0,0}};
  start[0] = 0;
  start[1] = 0;

  typename TImage::SizeType size;
  unsigned int NumRows = 200;
  unsigned int NumCols = 300;
  size[0] = NumRows;
  size[1] = NumCols;

  typename TImage::RegionType region(start, size);

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

  // Make a square
  for(typename TImage::IndexValueType r = 0; r < 100; r++)
    {
    for(typename TImage::IndexValueType c = 30; c < 130; c++)
      {
      typename TImage::IndexType pixelIndex = {{r,c}};

      image->SetPixel(pixelIndex, 255);
      }
    }

  // Make another square
  for(typename TImage::IndexValueType r = 90; r < 200; r++)
    {
    for(typename TImage::IndexValueType c = 115; c < 160; c++)
      {
      typename TImage::IndexType pixelIndex = {{r,c}};

      image->SetPixel(pixelIndex, 100);
      }
    }
}


####################################################
CMakeLists.txt :
###################################################
cmake_minimum_required(VERSION 2.8)

project(ConnectedComponentImageFilter)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
  find_package(VTK REQUIRED)
  include(${VTK_USE_FILE})
else()
  find_package(ItkVtkGlue REQUIRED)
  include(${ItkVtkGlue_USE_FILE})
  set(Glue ItkVtkGlue)
endif()

add_executable(ConnectedComponentImageFilter MACOSX_BUNDLE
ConnectedComponent.cxx)
target_link_libraries(ConnectedComponentImageFilter
  ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20130829/3d841917/attachment.htm>


More information about the Insight-users mailing list