[Insight-users] itk::Image::Graft() cannot cast error

lynx.abraxas at freenet.de lynx.abraxas at freenet.de
Thu Jul 23 17:31:21 EDT 2009


Hello!


I've written the little test program below but it only works if I use MorphologicalWatershedImageFilter directly but not if I use MorphologicalWatershedFromMarkersImageFilter and hm, rm and cc as discribed in the publication. I used segGel.cxx as an example.
What is wrong with my test in long form?
What is causing:
terminate called after throwing an instance of 'itk::ExceptionObject'
  what():  /usr/local/include/InsightToolkit/Common/itkImage.txx:135:
itk::ERROR: Image(0x85dd268): itk::Image::Graft() cannot cast PKN3itk10DataObjectE to PKN3itk5ImageIsLj2EEE

Many thanks for any hints or help
Lynx




#include <iostream>
#include "itkSignedDanielssonDistanceMapImageFilter.h"
#include "itkWatershedImageFilter.h"
#include "itkMorphologicalWatershedImageFilter.h"
#include "itkMorphologicalWatershedFromMarkersImageFilter.h"
#include "itkMaskImageFilter.h"
#include "itkRegionalMinimaImageFilter.h"
#include "itkHMinimaImageFilter.h"
#include "itkConnectedComponentImageFilter.h"

//#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkInvertIntensityImageFilter.h"
#include "itkScalarToRGBPixelFunctor.h"
#include "itkUnaryFunctorImageFilter.h"
#include "itkLabelOverlayImageFilter.h"


int main( int argc, char * argv[] )
{
  if( argc < 3 )
    {
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImageFile outputDistanceMapImageFile ";
    std::cerr << " outputVoronoiMapImageFile ";
    std::cerr << std::endl;  
    return EXIT_FAILURE;
    }

  typedef  unsigned char   InputPixelType;
  //typedef  float          FilterPixelType;
  typedef  signed short SDDMPType; // has to support negative values!
  typedef  unsigned char  OutputPixelType;
  const unsigned int Dimension = 2;

  typedef itk::Image<InputPixelType,  Dimension>   InputImageType;
  typedef itk::Image<SDDMPType,  Dimension>   SDDMIType;
  typedef itk::Image<OutputPixelType, Dimension>   OutputImageType;
  
  typedef itk::ImageFileReader<InputImageType>  ReaderType;
  typedef itk::ImageFileWriter<OutputImageType>  WriterType;

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

  reader->SetFileName(argv[1]);



  typedef itk::SignedDanielssonDistanceMapImageFilter<InputImageType,  SDDMIType>  FilterType;
  
  FilterType::Pointer sddm = FilterType::New();
  sddm->SetSquaredDistance(true);
  FilterType::Pointer sddm2 = FilterType::New();
  sddm2->SetSquaredDistance(true);


  typedef itk::HMinimaImageFilter<SDDMIType, OutputImageType> HMType;
  HMType::Pointer hm= HMType::New();
  hm->SetHeight(atoi(argv[5]));
  HMType::Pointer hm2= HMType::New();
  hm2->SetHeight(atoi(argv[5]));


  typedef itk::RegionalMinimaImageFilter<OutputImageType, OutputImageType> RegMinType;
  RegMinType::Pointer rm = RegMinType::New();
  rm->SetFullyConnected(atoi(argv[6]));
  RegMinType::Pointer rm2 = RegMinType::New();
  rm2->SetFullyConnected(atoi(argv[6]));


  typedef itk::ConnectedComponentImageFilter<OutputImageType, OutputImageType> LabelType;
  LabelType::Pointer cc = LabelType::New();
  cc->SetFullyConnected(atoi(argv[6]));


  typedef itk::MorphologicalWatershedFromMarkersImageFilter<SDDMIType, OutputImageType> MWatershedType;
//  typedef itk::MorphologicalWatershedImageFilter<SDDMIType, OutputImageType> MWatershedType;
  MWatershedType::Pointer ws = MWatershedType::New();
  ws->SetMarkWatershedLine(true);
  ws->SetFullyConnected(atoi(argv[6]));

 
  typedef itk::MaskImageFilter<OutputImageType, InputImageType, OutputImageType> MaskType;
  MaskType::Pointer mask = MaskType::New();
  mask->SetInput(1, reader->GetOutput());

  typedef itk::RGBPixel<unsigned char>   RGBPixelType;
  typedef itk::Image<RGBPixelType, Dimension>    RGBImageType;
  typedef itk::Image<unsigned long, Dimension>   LabeledImageType;

  typedef itk::Functor::ScalarToRGBPixelFunctor<OutputPixelType> ColorMapFunctorType_v;
  typedef itk::UnaryFunctorImageFilter<OutputImageType, RGBImageType, ColorMapFunctorType_v> ColorMapFilterType_v;
  ColorMapFilterType_v::Pointer colormapper_v = ColorMapFilterType_v::New();

  typedef itk::Functor::ScalarToRGBPixelFunctor<unsigned long> ColorMapFunctorType;
  typedef itk::UnaryFunctorImageFilter<LabeledImageType, RGBImageType, ColorMapFunctorType> ColorMapFilterType;
  ColorMapFilterType::Pointer colormapper = ColorMapFilterType::New();

  typedef itk::ImageFileWriter<RGBImageType> RGBFileWriterType;
  RGBFileWriterType::Pointer writer_rgb = RGBFileWriterType::New();


  typedef itk::LabelOverlayImageFilter<SDDMIType, OutputImageType, RGBImageType > OverlayType;
  OverlayType::Pointer overlay = OverlayType::New();




/////distmap    
  std::cout << "signed danielson distance map..." << std::endl;
  sddm->SetInput(reader->GetOutput());



////Watershed
  std::cout << "Morphological watershed..." << std::endl;
  const char * watershedFileName = argv[4];

 hm->SetInput(sddm->GetOutput());
 rm->SetInput(hm->GetOutput());
 cc->SetInput(rm->GetOutput());

//  ws->SetInput(sddm->GetOutput());
  ws->SetMarkerImage(cc->GetOutput());

  std::cout << "Creating first pre-output..." << std::endl;
  writer->SetFileName(argv[2]);
  writer->SetInput(rm->GetOutput());
  writer->Update();

  std::cout << "Creating second pre-output..." << std::endl;
  writer->SetFileName(argv[3]);
  writer->SetInput(cc->GetOutput());
  writer->Update();

  std::cout << "Creating nice output..." << std::endl;
  mask->SetInput(0, ws->GetOutput());
  //writer->SetInput(mask->GetOutput());
  colormapper_v->SetInput(mask->GetOutput());
  writer_rgb->SetInput(colormapper_v->GetOutput());
  writer_rgb->SetFileName(watershedFileName);

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


  return EXIT_SUCCESS;
}





More information about the Insight-users mailing list