[Insight-users] Simple question about saving an image...

Shahabuddin Ansari ansari_shahab at yahoo.com
Mon Mar 27 20:03:49 EST 2006


Hello,
   
  I have image of short pixel type. I need to save the image into another file with pixels of certain intensity levels. The code is given below but unfortunately it is saving pixels with intensity levels other than desired. Any idea would appreciated.
   
  Thanks
Shahab
   
  #include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include <iostream>
using namespace std;
   
  int main()
{
  int xdim = 256;
  int ydim = 256;
  int zdim = 128;
  
 typedef itk::Image< short, 3 > ImageType;
 ImageType::Pointer image = ImageType::New();
   
   typedef itk::ImageFileReader< ImageType > ReaderType;
 typedef itk::ImageFileWriter< ImageType > WriterType;
   
   ReaderType::Pointer reader = ReaderType::New();
 WriterType::Pointer writer = WriterType::New();
   
   reader->SetFileName( "seg2.img" );
 writer->SetFileName( "rhippo_seg.hdr" );
   
   image = reader->GetOutput();
 writer->SetInput( reader->GetOutput() );
   
   try 
 { 
  reader->Update();
} 
  
 catch( itk::ExceptionObject & err ) 
 { 
  cout << "ExceptionObject caught !" << endl; 
  cout << err << endl; 
  system("pause");
  return 1;
}
 
 ImageType::PixelType pixelValue;
 ImageType::IndexType pixelIndex;
   for(int z=0; z<zdim; z++){
  for(int y=0; y<ydim; y++){
   for(int x=0; x<xdim; x++){
      pixelIndex[0] = x; // x position
    pixelIndex[1] = y; // y position
    pixelIndex[2] = z; // z position
      try 
    { 
      pixelValue = image->GetPixel( pixelIndex );
      if(pixelValue == 53) //need in this intensity value and hoping binary output image
       image->SetPixel( pixelIndex, 32767 );
      else
       image->SetPixel( pixelIndex, -32766 );
    } 
  
    catch( itk::ExceptionObject & err ) 
    { 
     cout << "ExceptionObject caught !" << endl; 
     cout << err << endl; 
     system("pause");
     return 1;
    }
   }
  }
 }
  
 try 
    { 
  writer->Update();
    } 
  
 catch( itk::ExceptionObject & err ) 
    { 
  cout << "ExceptionObject caught !" << endl; 
  cout << err << endl; 
  system("pause");
  return 1;
    }
  
 system("pause");
 return 0;
}

		
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates starting at 1&cent;/min.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20060327/848ca223/attachment.htm


More information about the Insight-users mailing list