[Insight-users] itkBinaryImageToShapeLabelMapFilter

John Drozd john.drozd at gmail.com
Wed Dec 9 15:03:22 EST 2009


Hi Richard,

I don't know if you have time to help me, but I thought I would ask you
anyways.
If you can't help me, that's ok.

As you suggested, I am trying to get a label map using
itkBinaryImageToShapeLabelMapFilter,
but I am getting zero label objects and I don't know why.

In my code below, I perform a connected threshold filter segmentation, then
after some casting, I feed the segmentation
to an itkBinaryImageToLabelMapFilter, which is then fed to a
itkLabelMapToLabelImageFilter, and finally to
the itkBinaryImageToShapeLabelMapFilter.

Using the deprecated itkRelabelComponentImageFilter, I do get labeled
objects, but I would like to get my code working using the
itkBinaryImageToShapeLabelMapFilter.

Can you see what I am doing wrong?

My output is below (the first four outputted lines are from the deprecated
itkRelabelComponentImageFilter):


./ConnectedThresholdImageFilter correctedsubject5.dcm outsubject5.dcm 103
142 95 17100 17300
Number of pixel for object 0: 22129
Physical size for object 0: 23901
Number of pixel for object 1: 1
Physical size for object 1: 1.08004
labelMap->GetNumberOfLabelObjects() = 0


My code is below.
- Show quoted text -


/*
to run type:
./ConnectedThresholdImageFilter correctedsubject5.dcm outsubject5.dcm 103
142 95 17100 17300
*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif


#include "itkConnectedThresholdImageFilter.h"

#include "itkImage.h"
#include "itkCastImageFilter.h"

#include "itkCurvatureFlowImageFilter.h"

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkGDCMImageIO.h"

#include "itkVersion.h"

#include "itkOrientedImage.h"
#include "itkMinimumMaximumImageFilter.h"

#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkNumericSeriesFileNames.h"

#include "itkImageSeriesReader.h"
#include "itkImageSeriesWriter.h"

#include "itkResampleImageFilter.h"
#include "itkShiftScaleImageFilter.h"

#include "itkIdentityTransform.h"
#include "itkLinearInterpolateImageFunction.h"

#include <itksys/SystemTools.hxx>

#include "gdcm/src/gdcmFile.h"
#include "gdcm/src/gdcmUtil.h"

#include <string>

//added per attribute_values.cxx
#include "itkImageFileReader.h"

#include "itkShapeLabelObject.h"

#include "itkLabelMap.h"

#include "itkBinaryImageToShapeLabelMapFilter.h"
//end of added code


//ADDED PER WEB PAGE
//
http://old.nabble.com/Labeling-an-image-and-displaying-results-with-distinguished-levels-of-gray-colors-td26623991.html
#include "itkConnectedComponentImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
//END OF ADDED CODE


//added per statistics_relabel.cxx
#include "itkStatisticsRelabelImageFilter.h"
//end of added code

//added per label.cxx
#include "itkLabelObject.h"
#include "itkLabelMap.h"
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
- Show quoted text -

//end of added code

int main( int argc, char *argv[])
{
  if( argc < 7 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImage  outputImage seedX seedY seedZ lowerThreshold
upperThreshold" << std::endl;

    return 1;
    }

  typedef   float           InternalPixelType;

  const     unsigned int    Dimension = 3;

  typedef itk::Image< InternalPixelType, Dimension >  InternalImageType;

  typedef signed short OutputPixelType;

  typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
  typedef itk::Image< float, Dimension > OutputImageType2;
  typedef itk::CastImageFilter< InternalImageType, OutputImageType >
    CastingFilterType;
  CastingFilterType::Pointer caster = CastingFilterType::New();

  const    unsigned int    ImageDimension = 3;
  typedef  signed short    PixelType;

  typedef itk::Image< PixelType, ImageDimension >  FixedImageType;
  typedef itk::Image< float, ImageDimension >  FloatImageType;

  typedef  itk::ImageFileReader< FixedImageType > ReaderType;
  typedef  itk::ImageFileWriter<  OutputImageType  > WriterType;
  typedef  itk::ImageFileWriter<  FloatImageType  > WriterType2;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
  WriterType2::Pointer writer2 = WriterType2::New();

  typedef itk::GDCMImageIO           ImageIOTypefixed;
  ImageIOTypefixed::Pointer gdcmImageIOfixed = ImageIOTypefixed::New();
  reader->SetImageIO( gdcmImageIOfixed );

  typedef itk::GDCMImageIO           ImageIOTypefixed2;
  ImageIOTypefixed2::Pointer gdcmImageIOfixed2 = ImageIOTypefixed2::New();

  reader->SetFileName( argv[1] );

  reader->Update();

  typedef itk::CurvatureFlowImageFilter< InternalImageType,
InternalImageType >
    CurvatureFlowImageFilterType;

  CurvatureFlowImageFilterType::Pointer smoothing =
                         CurvatureFlowImageFilterType::New();

  typedef itk::ConnectedThresholdImageFilter< InternalImageType,
                                    InternalImageType > ConnectedFilterType;

  ConnectedFilterType::Pointer connectedThreshold =
ConnectedFilterType::New();

  typedef signed short InputAPixelType;
  typedef float OutputBPixelType;

  typedef itk::Image< InputAPixelType, 3 > InputAImageType;
  typedef itk::Image< OutputBPixelType, 3 > OutputBImageType;

  typedef itk::CastImageFilter< InputAImageType, OutputBImageType >
CastFilterType;

  CastFilterType::Pointer castFilter = CastFilterType::New();


  castFilter->SetInput( reader->GetOutput() );


  connectedThreshold->SetInput( castFilter->GetOutput() );

  caster->SetInput( connectedThreshold->GetOutput() );


  smoothing->SetNumberOfIterations( 20 ); //was 5
  smoothing->SetTimeStep( 0.125 );

  const InternalPixelType lowerThreshold = atof( argv[6] );
  const InternalPixelType upperThreshold = atof( argv[7] );

  connectedThreshold->SetLower(  lowerThreshold  );
  connectedThreshold->SetUpper(  upperThreshold  );

  connectedThreshold->SetReplaceValue( 255 );

  InternalImageType::IndexType  index;

  index[0] = atoi( argv[3] );
  index[1] = atoi( argv[4] );

  //added
  index[2] = atoi( argv[5] );

  // Software Guide : BeginCodeSnippet
  connectedThreshold->SetSeed( index );

  //obtain a 5 x 5 bounding region of seeds
  int ii, jj, kk;

  ii = index[0];
  jj = index[1];
  kk = index[2];

  for (int i = ii; i < ii + 5; i++)
    for (int j = jj; j < jj + 5; j++)
      for (int k = kk; k < kk + 5; k++)
    {

      index[0] = i;
      index[1] = j;
      index[2] = k;
      connectedThreshold->AddSeed( index );
    }

  for (int i = ii; i > ii - 5; i--)
    for (int j = jj; j > jj - 5; j--)
      for (int k = kk; k > kk - 5; k--)
    {

      index[0] = i;
      index[1] = j;
      index[2] = k;
      connectedThreshold->AddSeed( index );
    }

  typedef itk::MetaDataDictionary DictionaryType;

  DictionaryType inputdict = reader->GetMetaDataDictionary();

  writer->SetMetaDataDictionary( inputdict );

  writer->SetFileName( argv[2] );



  //ADDED PER WEB PAGE: This deprecated code works

  //
http://old.nabble.com/Labeling-an-image-and-displaying-results-with-distinguished-levels-of-gray-colors-td26623991.html
  typedef itk::ConnectedComponentImageFilter <OutputImageType,
OutputImageType>  LabelType2;
  typedef itk::RelabelComponentImageFilter <OutputImageType,
OutputImageType> RelabelType;

  LabelType2::Pointer labeler = LabelType2::New();
  RelabelType::Pointer relabeler = RelabelType::New();

  labeler->SetInput(caster->GetOutput());
  labeler->Update();

  relabeler->SetInput(labeler->GetOutput());
  relabeler->Update();

  for (unsigned int i=0; i<relabeler->GetNumberOfObjects(); i++)
    {
        std::cout<<"Number of pixel for object "<<i<<":
"<<relabeler->GetSizeOfObjectsInPixels()[i]<<std::endl;

    std::cout<<"Physical size for object "<<i<<":
"<<relabeler->GetSizeOfObjectsInPhysicalUnits()[i]<<std::endl;
    }
  //END OF ADDED CODE


/***************************************************************************/
  //but the code below fails to label the binary image giving zero label
objects

/***************************************************************************/

  typedef itk::Image < signed short, 3 > IType;

  typedef itk::LabelObject< signed short, 3 > LabelObjectTypeD;
  typedef itk::LabelMap< LabelObjectTypeD > LabelMapTypeD;

  typedef itk::BinaryImageToLabelMapFilter< OutputImageType, LabelMapTypeD>
I2LType;
  I2LType::Pointer i2l = I2LType::New();
  i2l->SetInput( caster->GetOutput() );
  i2l->SetFullyConnected( 255 );
  //i2l->SetForegroundValue( 100 );
  //i2l->SetBackgroundValue( 255 );
  //i2l->Update();
  i2l->GetOutput()->PrintLabelObjects();
  typedef itk::LabelMapToLabelImageFilter< LabelMapTypeD, IType> L2IType;
  L2IType::Pointer l2i = L2IType::New();
  l2i->SetInput( i2l->GetOutput() );

  typedef signed short LabelType3;
  typedef itk::ShapeLabelObject< LabelType3, 3 > LabelObjectType2;
  typedef itk::LabelMap< LabelObjectType2 > LabelMapType2;

  typedef itk::BinaryImageToShapeLabelMapFilter< IType, LabelMapType2 >
ConverterType;

  ConverterType::Pointer converter = ConverterType::New();
  converter->SetInput( l2i->GetOutput() );


  LabelMapType2::Pointer labelMap = converter->GetOutput();

  std::cout << "labelMap->GetNumberOfLabelObjects() = " <<
labelMap->GetNumberOfLabelObjects() << std::endl;


  writer->SetInput( caster->GetOutput() );

  try
    {
    writer->Update();
    }
  catch( itk::ExceptionObject & excep )
    {
    std::cerr << "Exception caught !" << std::endl;
    std::cerr << excep << std::endl;
    }

  return 0;
}

Thanks,
john
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091209/29a0ee6a/attachment.htm>


More information about the Insight-users mailing list