Hello,<br><br>I have segmented the ventricles from an image using a connected threshold image filter.<br>I then tried to convert my outputted segmentation to a label map filter and then to get the label&#39;s attributes.<br>
<br>I am using parts of the example code attribute_values.cxx that is described in section 9.4 of<br>Gaetan Lehmann&#39;s paper: &quot;Label object representation and manipulation with ITK&quot;<br> <br>My problem is that my code is telling me that the number of labels is 0.<br>
How do I apply a label to my segmented ventricles?<br><br>Below is my code:<br><br>/*<br>to run type:<br>./ConnectedThresholdImageFilter correctedsubject5.dcm outsubject5.dcm 103 142 95 17100 17300<br>*/<br><br>#if defined(_MSC_VER)<br>
#pragma warning ( disable : 4786 )<br>#endif<br><br>#ifdef __BORLANDC__<br>#define ITK_LEAN_AND_MEAN<br>#endif<br><br><br>#include &quot;itkConnectedThresholdImageFilter.h&quot;<br><br>#include &quot;itkImage.h&quot;<br>#include &quot;itkCastImageFilter.h&quot;<br>
<br>#include &quot;itkCurvatureFlowImageFilter.h&quot;<br><br>#include &quot;itkImageFileReader.h&quot;<br>#include &quot;itkImageFileWriter.h&quot;<br><br>#include &quot;itkGDCMImageIO.h&quot;<br><br>#include &quot;itkVersion.h&quot;<br>
<br>#include &quot;itkOrientedImage.h&quot;<br>#include &quot;itkMinimumMaximumImageFilter.h&quot;<br><br>#include &quot;itkGDCMImageIO.h&quot;<br>#include &quot;itkGDCMSeriesFileNames.h&quot;<br>#include &quot;itkNumericSeriesFileNames.h&quot;<br>
<br>#include &quot;itkImageSeriesReader.h&quot;<br>#include &quot;itkImageSeriesWriter.h&quot;<br><br>#include &quot;itkResampleImageFilter.h&quot;<br>#include &quot;itkShiftScaleImageFilter.h&quot;<br><br>#include &quot;itkIdentityTransform.h&quot;<br>
#include &quot;itkLinearInterpolateImageFunction.h&quot;<br><br>#include &lt;itksys/SystemTools.hxx&gt;<br><br>#include &quot;gdcm/src/gdcmFile.h&quot;<br>#include &quot;gdcm/src/gdcmUtil.h&quot;<br><br>#include &lt;string&gt;<br>
<br>//added per attribute_values.cxx<br>#include &quot;itkImageFileReader.h&quot;<br><br>#include &quot;itkShapeLabelObject.h&quot;<br>//#include &quot;itkStatisticsLabelObject.h&quot;<br><br>#include &quot;itkLabelMap.h&quot;<br>
<br>#include &quot;itkBinaryImageToShapeLabelMapFilter.h&quot;<br>//#include &quot;itkBinaryImageToStatisticsLabelMapFilter.h&quot;<br><br>//end of added code<br><br>int main( int argc, char *argv[])<br>{<br>  if( argc &lt; 7 )<br>
    {<br>    std::cerr &lt;&lt; &quot;Missing Parameters &quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; &quot;Usage: &quot; &lt;&lt; argv[0];<br>    std::cerr &lt;&lt; &quot; inputImage  outputImage seedX seedY seedZ lowerThreshold upperThreshold&quot; &lt;&lt; std::endl;<br>
<br>    return 1;<br>    }<br><br>  typedef   float           InternalPixelType;<br> <br>  const     unsigned int    Dimension = 3;<br><br>  typedef itk::Image&lt; InternalPixelType, Dimension &gt;  InternalImageType;<br>
<br>  typedef signed short OutputPixelType;<br><br>  typedef itk::Image&lt; OutputPixelType, Dimension &gt; OutputImageType;<br>  typedef itk::Image&lt; float, Dimension &gt; OutputImageType2;<br>  typedef itk::CastImageFilter&lt; InternalImageType, OutputImageType &gt;<br>
    CastingFilterType;<br>  CastingFilterType::Pointer caster = CastingFilterType::New();<br>                       <br>  const    unsigned int    ImageDimension = 3;<br>  typedef  signed short    PixelType;<br><br>  typedef itk::Image&lt; PixelType, ImageDimension &gt;  FixedImageType;<br>
  typedef itk::Image&lt; float, ImageDimension &gt;  FloatImageType;<br><br>  typedef  itk::ImageFileReader&lt; FixedImageType &gt; ReaderType;<br>  typedef  itk::ImageFileWriter&lt;  OutputImageType  &gt; WriterType;<br>
  typedef  itk::ImageFileWriter&lt;  FloatImageType  &gt; WriterType2;<br><br>  ReaderType::Pointer reader = ReaderType::New();<br>  WriterType::Pointer writer = WriterType::New();<br>  WriterType2::Pointer writer2 = WriterType2::New();<br>
<br>  typedef itk::GDCMImageIO           ImageIOTypefixed;<br>  ImageIOTypefixed::Pointer gdcmImageIOfixed = ImageIOTypefixed::New();<br>  reader-&gt;SetImageIO( gdcmImageIOfixed );<br><br>  typedef itk::GDCMImageIO           ImageIOTypefixed2;<br>
  ImageIOTypefixed2::Pointer gdcmImageIOfixed2 = ImageIOTypefixed2::New();<br><br>  reader-&gt;SetFileName( argv[1] );<br>  <br>  reader-&gt;Update();<br><br>  typedef itk::CurvatureFlowImageFilter&lt; InternalImageType, InternalImageType &gt;<br>
    CurvatureFlowImageFilterType;<br> <br>  CurvatureFlowImageFilterType::Pointer smoothing =<br>                         CurvatureFlowImageFilterType::New();<br><br>  typedef itk::ConnectedThresholdImageFilter&lt; InternalImageType,<br>
                                    InternalImageType &gt; ConnectedFilterType;<br><br>  ConnectedFilterType::Pointer connectedThreshold = ConnectedFilterType::New();<br><br>  typedef signed short InputAPixelType;<br>  typedef float OutputBPixelType;<br>
<br>  typedef itk::Image&lt; InputAPixelType, 3 &gt; InputAImageType;<br>  typedef itk::Image&lt; OutputBPixelType, 3 &gt; OutputBImageType;<br><br>  typedef itk::CastImageFilter&lt; InputAImageType, OutputBImageType &gt; CastFilterType;<br>
<br>  CastFilterType::Pointer castFilter = CastFilterType::New();<br><br><br>  castFilter-&gt;SetInput( reader-&gt;GetOutput() );<br> <br><br>  connectedThreshold-&gt;SetInput( castFilter-&gt;GetOutput() );<br><br>  caster-&gt;SetInput( connectedThreshold-&gt;GetOutput() );<br>
<br><br>  smoothing-&gt;SetNumberOfIterations( 20 ); //was 5<br>  smoothing-&gt;SetTimeStep( 0.125 );<br> <br>  const InternalPixelType lowerThreshold = atof( argv[6] );<br>  const InternalPixelType upperThreshold = atof( argv[7] );<br>
 <br>  connectedThreshold-&gt;SetLower(  lowerThreshold  );<br>  connectedThreshold-&gt;SetUpper(  upperThreshold  );<br>  <br>  connectedThreshold-&gt;SetReplaceValue( 255 );<br><br>  InternalImageType::IndexType  index;<br>
 <br>  index[0] = atoi( argv[3] );<br>  index[1] = atoi( argv[4] );<br> <br>  //added<br>  index[2] = atoi( argv[5] );<br><br>  std::cout &lt;&lt; index &lt;&lt; std::endl;<br><br>  // Software Guide : BeginCodeSnippet<br>
  connectedThreshold-&gt;SetSeed( index );<br> <br>  //obtain a 5 x 5 bounding region of seeds<br>  int ii, jj, kk;<br><br>  ii = index[0];<br>  jj = index[1];<br>  kk = index[2];<br><br>  for (int i = ii; i &lt; ii + 5; i++)<br>
    for (int j = jj; j &lt; jj + 5; j++)<br>      for (int k = kk; k &lt; kk + 5; k++)<br>    {<br>     <br>      index[0] = i;<br>      index[1] = j;<br>      index[2] = k;<br>      connectedThreshold-&gt;AddSeed( index );<br>
    }<br><br>  for (int i = ii; i &gt; ii - 5; i--)<br>    for (int j = jj; j &gt; jj - 5; j--)<br>      for (int k = kk; k &gt; kk - 5; k--)<br>    {<br>     <br>      index[0] = i;<br>      index[1] = j;<br>      index[2] = k;<br>
      connectedThreshold-&gt;AddSeed( index );<br>    }<br><br>  connectedThreshold-&gt;Print(std::cout,17100);<br> <br>  typedef itk::MetaDataDictionary DictionaryType;<br> <br>  DictionaryType inputdict = reader-&gt;GetMetaDataDictionary();<br>
 <br>  writer-&gt;SetMetaDataDictionary( inputdict );<br>   <br>  writer-&gt;SetFileName( argv[2] );<br><br>  //added per attribute_values.cxx<br><br>  // define the object type. Here the ShapeLabelObject type<br>  // is chosen in order to read some attribute related to the shape<br>
  // of the objects (by opposition to the content of the object, with<br>  // the StatisticsLabelObejct).<br>  typedef unsigned long LabelType;<br>  <br>  typedef itk::ShapeLabelObject&lt; LabelType, 3 &gt; LabelObjectType;<br>
  <br>  typedef itk::LabelMap&lt; LabelObjectType &gt; LabelMapType;<br><br>  // convert the image in a collection of objects<br>  typedef itk::BinaryImageToLabelMapFilter&lt; InternalImageType, LabelMapType &gt; ConverterType;<br>
  <br>  ConverterType::Pointer converter = ConverterType::New();<br>  <br>  converter-&gt;SetInput( connectedThreshold-&gt;GetOutput() );<br><br>  //converter-&gt;SetForegroundValue( atoi(argv[2]) );<br>  //converter-&gt;SetForegroundValue( 255 );<br>
<br>  // valuate the attributes with the dedicated filter: ShapeLabelMapFilter<br><br>  typedef itk::ShapeLabelMapFilter&lt; LabelMapType &gt; ShapeFilterType;<br>  ShapeFilterType::Pointer shape = ShapeFilterType::New();<br>
  shape-&gt;SetInput( converter-&gt;GetOutput() );<br><br>  shape-&gt;Update();<br><br><br>  // then we can read the attribute values we&#39;re interested in. The BinaryImageToShapeLabelMapFilter<br>  // produce consecutive labels, so we can use a for loop and GetLabelObject() method to retrieve<br>
  // the label objects. If the labels are not consecutive, the GetNthLabelObject() method must be<br>  // use instead of GetLabelObject(), or an iterator on the label object container of the label map.<br>  LabelMapType::Pointer labelMap = shape-&gt;GetOutput();<br>
<br>  std::cout &lt;&lt; &quot;Number of label objects = &quot; &lt;&lt; labelMap-&gt;GetNumberOfLabelObjects() &lt;&lt; std::endl;<br><br><br>  for( unsigned int label=1; label&lt;=labelMap-&gt;GetNumberOfLabelObjects(); label++ )<br>
    {<br>    // we don&#39;t need a SmartPointer of the label object here, because the reference is kept in<br>    // in the label map.<br>    const LabelObjectType * labelObject = labelMap-&gt;GetLabelObject( label );<br>
    std::cout &lt;&lt; label &lt;&lt; &quot;\t&quot; &lt;&lt; labelObject-&gt;GetPhysicalSize() &lt;&lt; &quot;\t&quot; &lt;&lt; labelObject-&gt;GetCentroid() &lt;&lt; std::endl;<br>    }<br><br>  writer-&gt;SetInput( caster-&gt;GetOutput() );<br>
<br>  try<br>    {<br>    writer-&gt;Update();<br>    }<br>  catch( itk::ExceptionObject &amp; excep )<br>    {<br>    std::cerr &lt;&lt; &quot;Exception caught !&quot; &lt;&lt; std::endl;<br>    std::cerr &lt;&lt; excep &lt;&lt; std::endl;<br>
    }<br><br>  std::cout &lt;&lt; &quot;output from reader-&gt;GetOutput()-&gt;GetDirection()&quot; &lt;&lt; std::endl;<br>  std::cout &lt;&lt; reader-&gt;GetOutput()-&gt;GetDirection() &lt;&lt; std::endl;<br>  <br>  std::cout &lt;&lt; &quot;output from castFilter-&gt;GetOutput()-&gt;GetDirection()&quot; &lt;&lt; std::endl;<br>
  std::cout &lt;&lt; castFilter-&gt;GetOutput()-&gt;GetDirection() &lt;&lt; std::endl;<br>  <br>  std::cout &lt;&lt; &quot;output from connectedThreshold-&gt;GetOutput()-&gt;GetDirection()&quot; &lt;&lt; std::endl;<br>  std::cout &lt;&lt; connectedThreshold-&gt;GetOutput()-&gt;GetDirection() &lt;&lt; std::endl;<br>
<br>  std::cout &lt;&lt; &quot;output from caster-&gt;GetOutput()-&gt;GetDirection()&quot; &lt;&lt; std::endl;<br>  std::cout &lt;&lt; caster-&gt;GetOutput()-&gt;GetDirection() &lt;&lt; std::endl;<br><br>  return 0;<br>}<br>
<br>