[Insight-users] problem with orientation of dicom output from segmentation

John Drozd john.drozd at gmail.com
Thu Nov 26 12:57:21 EST 2009


Hello,

Can anyone please tell me why my connect threshold segmentation code, where
I input a 3d dicom brain volume and output a 3d dicom ventricle
segmentation, gives me my segmentation "upside down" (relative to the
original inputted image) when I view it in 3D Slicer?  When I output the
code in any other format eg. .mhd, the segmentation is right side up.

Thank you.

john

Below is my uncommented code:

/*
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>

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] );

  std::cout << index << std::endl;

  // 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 );
    }

  connectedThreshold->Print(std::cout,17100);



  typedef itk::MetaDataDictionary DictionaryType;

  DictionaryType inputdict = reader->GetMetaDataDictionary();

  writer->SetMetaDataDictionary( inputdict );

  writer->SetFileName( argv[2] );

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

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

  return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091126/75ac3d2c/attachment-0001.htm>


More information about the Insight-users mailing list