[Insight-users] MetaImageWriters!!

digvijay singh ds_1997in@yahoo.com
Fri, 20 Dec 2002 07:12:06 -0800 (PST)


--0-1796556863-1040397126=:95662
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

hi luis!!
i am trying to read in a meta file , apply a binary
threshold to it.....and then write the data to another
image...Initially when i was tryin to get the data
written in a file separate from the header file i was
getting a data file but when i tried to force the data
to be written in the header file i find that it simply
does not go in there...please help...
ciao
digvijay

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
--0-1796556863-1040397126=:95662
Content-Type: text/x-c++src; name="BinaryThresholdFilter.cxx"
Content-Description: BinaryThresholdFilter.cxx
Content-Disposition: inline; filename="BinaryThresholdFilter.cxx"

/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkBinaryThresholdImageFilterTest.cxx,v $
  Language:  C++
  Date:      $Date: 2002/05/01 17:50:08 $
  Version:   $Revision: 1.2 $

  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include <itkMetaImageIO.h>
#include <itkMetaImageIOFactory.h>

#include "itkImage.h"
#include "itkRandomImageSource.h"
#include "itkThresholdImageFilter.h"
#include "itkCommand.h"
#include "itkTextOutput.h"


#include <fstream.h>
#include <vector>
#include <iostream>
#include <string>
#include <itkMetaImageIO.h>
#include <itkMetaImageIOFactory.h>

#include <cstdlib>
#include <fstream>
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkHistogram.h>
#include <itkImageRegionIterator.h>
#include "OptionList.h"
#include "OptionList.h"
#include <fstream>
#include <OptionList.h>
#include <itkGradientDescentOptimizer.h>
#include <vnl/vnl_math.h>
#include <fstream>
#include <stdio.h>

#include "itkImage.h"
#include "itkRandomImageSource.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"


int BinaryThresholdFilter(int argc, char* argv[]) 
{
 
  string fileName ;
  
 // Define the dimension of the images
  const unsigned int ImageDimension = 3;

  // Declare  image types
  typedef itk::Image<short, ImageDimension>  InputImageType;
  typedef itk::Image<float, ImageDimension>  OutputImageType;
  typedef InputImageType::PixelType InputPixelType;
  typedef OutputImageType::PixelType OutputPixelType;

  // Declare iterators
 
  typedef itk::ImageRegionIterator<InputImageType>  InputIteratorType;
                                  

  typedef itk::ImageRegionIterator<OutputImageType>  OutputIteratorType;
                                  
 
  
  typedef short InputImagePixelType;
  typedef int OutputImagePixelType ;
  typedef itk::Image<InputImagePixelType,3>  ImageType;
  ImageType::Pointer image ;

  typedef itk::ImageFileReader< InputImageType > VolumeReaderType;
  VolumeReaderType::Pointer imageReader = VolumeReaderType::New();
  typedef itk::ImageFileWriter< OutputImageType > VolumeWriterType ;
  VolumeWriterType::Pointer writer = VolumeWriterType::New();


  OptionList options(argc, argv) ;

  if (argc <= 1)
    {
      cout << "usage: " << argv[0] << endl
	   << "       --i infile [--binsize int]" << endl
	   << "" << endl;
      exit(0) ;
    }

  
 
 

  try
    {
      options.GetStringOption("i", &fileName, true) ;
      std::cout << fileName << std::endl ;
     
    }
  catch(OptionList::RequiredOptionMissing e)
    {
      cout << "Error: The " << e.OptionTag 
	   << " option is required but missing." 
	   << endl ;
      exit(0) ;
    }
 
 

 

 
  try
    {
      imageReader->SetFileName(fileName.c_str()) ;
      imageReader->Update() ;
      image = imageReader->GetOutput() ;
    }
  catch(itk::ExceptionObject e)
    {
      e.Print(cout) ;
      exit(0) ;
    }

 

   InputIteratorType iter( image, image->GetBufferedRegion() );

  typedef itk::BinaryThresholdImageFilter< InputImageType,
                               OutputImageType  >  FilterType;
            

  // Create a filter                                
  FilterType::Pointer filter = FilterType::New();

  // Setup ivars
  InputPixelType lower = 900;
  InputPixelType upper = 2047;
  filter->SetUpperThreshold( upper );
  filter->SetLowerThreshold( lower );

  OutputPixelType inside = 0;
  OutputPixelType outside = 1;
  filter->SetInsideValue( inside );
  filter->SetOutsideValue( outside );

  filter->Print( std::cout );

  
  filter->SetInput( imageReader->GetOutput() ); 

  // Get the Smart Pointer 
  OutputImageType::Pointer outputImage = filter->GetOutput();
  
  
  try
    {
    filter->Update();
    }
  catch(...)
    {
    std::cerr << "Caught an unexpected exception. " << std::endl;
    std::cerr << "check for errors please " << std::endl;
    return EXIT_FAILURE;
    }

  // Create an iterator for input image
  InputIteratorType  it(  imageReader->GetOutput() , imageReader->GetOutput()->GetRequestedRegion() ); 
  OutputIteratorType ot(outputImage, outputImage->GetRequestedRegion());
  
  //  Check the content of the result image

  ot.GoToBegin();
  it.GoToBegin();
  while( !ot.IsAtEnd() ) 
  {

    const InputPixelType  input  = it.Get();
    const OutputPixelType output = ot.Get();
    std::cout <<  (double) input  << " " << (double) output << std::endl; 

    bool pass = true;
    if( lower <= input && input <= upper )
      {
      if ( output != inside )
        {
        pass = false;
        }
      }
    else if ( output != outside )
      {
      pass = false;
      }

    if ( !pass )
      {
      std::cerr << "Error in BinaryThresholdImageFilter " << std::endl;
      std::cerr << " lower = " << (double)lower;
      std::cerr << " upper = " << (double)upper;
      std::cerr << " inside = " << (double)inside;
      std::cerr << " outside = " << (double) outside;
      std::cerr << std::endl;
      std::cerr << " input = " << (double) input;
      std::cerr << " output = " << (double) output;
      std::cerr << std::endl;
      return EXIT_FAILURE;
      }

    ++ot;
    ++it;
  }
  itk::MetaImageIO::Pointer metaWriter = itk::MetaImageIO::New() ;
  
  writer->SetImageIO( metaWriter );
  metaWriter->SetDataFileName( " LOCAL" );
  writer->SetFileName( "test.mha" ) ;
  writer->SetInput(outputImage );
  writer->Write() ;

}


int main(int argc, char* argv[])
{
  FILE *fpa ;
   if((fpa=freopen("binary.txt","w",stdout))==NULL)
   {
     cout << "cannot open file" ;
     exit(1);
   }
   
 BinaryThresholdFilter(argc, argv);
 fclose(fpa);

}




--0-1796556863-1040397126=:95662
Content-Type: text/plain; name="CMakeLists.txt"
Content-Description: CMakeLists.txt
Content-Disposition: inline; filename="CMakeLists.txt"


SET(ITK_SOURCE_DIR /opt/tools/itk/Insight_Nightly/Insight)
SET(ITK_LIB_DIR    /opt/tools/itk/Insight_Nightly/lib)
SET(ITK_BUILD_DIR /opt/tools/itk/Insight_Nightly/build)

SET(ITK_BINARY_DIR /opt/tools/itk/my_source)
SET(HOME /home/vertebra/digvijay)
SET(ITK_PROJECT_DIR ${HOME}/itk/proj_1/histo_test)
# Include OpenGL
INCLUDE (${CMAKE_ROOT}/Modules/FindOpenGL.cmake)

# Include FLTK
INCLUDE (${CMAKE_ROOT}/Modules/FindFLTK.cmake)	

INCLUDE_DIRECTORIES(
${ITK_SOURCE_DIR}/Code/
${ITK_SOURCE_DIR}/Utilities
${ITK_SOURCE_DIR}/Utilities/MetaIO

${ITK_SOURCE_DIR}/Code/IO
${ITK_SOURCE_DIR}/Code/Numerics/
${ITK_SOURCE_DIR}/Code/Numerics/Statistics
${ITK_SOURCE_DIR}/Code/Numerics/FEM
${ITK_SOURCE_DIR}/Code/Numerics/vxl
${ITK_SOURCE_DIR}/Code/Numerics/vxl/vnl/

${ITK_SOURCE_DIR}/Code/Numerics/vxl/vcl
${ITK_SOURCE_DIR}/Code/Common
${ITK_SOURCE_DIR}/Code/BasicFilters
${ITK_SOURCE_DIR}/Code/Algorithms
${ITK_SOURCE_DIR}/Code/IO
${ITK_SOURCE_DIR}/Auxiliary/FltkImageViewer
${ITK_BUILD_DIR}
${ITK_BUILD_DIR}/Code/Numerics/vxl/vcl
${ITK_BUILD_DIR}/Auxiliary/FltkImageViewer
${ITK_SOURCE_DIR}/Examples/IBSRValidation/Common
${FLTK_INCLUDE_PATH}
${OPENGL_INCLUDE_PATH}
)

LINK_DIRECTORIES( ${ITK_LIB_DIR} )

LINK_LIBRARIES (
VXLNumerics
ITKIO
ITKAlgorithms
ITKBasicFilters
ITKNumerics
itkpng
ITKStatistics
ITKMetaIO
itkzlib
param
ITKVtkFltk
ITKFltkImageViewer
ITKFEM
ITKCommon
${FLTK_LIBRARY}
${OPENGL_LIBRARY}
${GLU_LIBRARY}
-lpthread -ldl -lm 
)

ADD_DEFINITIONS(-ftemplate-depth-50 -D_PTHREADS)


ADD_EXECUTABLE(Test Test.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test1 Test1.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test2 Test2.cxx  OptionList.cxx)

ADD_EXECUTABLE(Test3 Test3.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test4 Test4.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test5 Test5.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test6 Test6.cxx  OptionList.cxx)
ADD_EXECUTABLE(temp temp.cxx  OptionList.cxx)

ADD_EXECUTABLE(Test8 Test8.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test9 Test9.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test10 Test10.cxx  OptionList.cxx)
ADD_EXECUTABLE(Test11 Test11.cxx  OptionList.cxx)
#ADD_EXECUTABLE(ThresholdFilter ThresholdFilter.cxx  OptionList.cxx)
ADD_EXECUTABLE(BinaryThresholdFilter BinaryThresholdFilter.cxx  OptionList.cxx)
--0-1796556863-1040397126=:95662--