[Insight-users] Problem with GetPixel

Bradley Lowekamp blowekamp at mail.nih.gov
Wed Feb 17 12:01:19 EST 2010


Hello,

The problem with this code is that the reader has not been updated before you are trying to get it's output origin. Before the call:

	image->GetOrigin();

your should call:

	image->UpdateOutputInformation(); 

if you just want the meta data updated. Alternatively call:

	image->Update();

if you want to execute the reader, to load the image into memory. Chapter 13 in the ITK Software Guide goes over the execution of the pipeline if you'd like to know more.

Brad

On Feb 17, 2010, at 10:55 AM, edoardo.belletti at alice.it wrote:

> 
> 
> Hi
> I have a problem with this program, when I run it the output is that:
>  Segmentation fault
> 
> The problem is with GetPixel I suppose.
> 
> #if defined(_MSC_VER)
> #pragma warning ( disable : 4786 )
> #endif
> 
> #ifdef __BORLANDC__
> #define ITK_LEAN_AND_MEAN
> #endif
> 
> 
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> 
> 
> int main( int argc, char ** argv )
> {
>         // Verify the number of parameters in the command line
>         if( argc < 3 )
>         {
>                 std::cerr << "Usage: " << std::endl;
>                 std::cerr << argv[0] << " inputImageFile  outputImageFile " << std::endl;
>                 return EXIT_FAILURE;
>         }
> 
>         typedef unsigned char PixelType;
>         const   unsigned int Dimension = 2;
>         typedef itk::Image< PixelType, Dimension >    ImageType;
>  
>         typedef itk::ImageFileReader< ImageType >  ReaderType;
>         typedef itk::ImageFileWriter< ImageType >  WriterType;
>  
>         ReaderType::Pointer reader = ReaderType::New();
>         WriterType::Pointer writer = WriterType::New();
>  
>         const char * inputFilename  = argv[1];
>         const char * outputFilename = argv[2];
> 
>         reader->SetFileName( inputFilename  );
>         writer->SetFileName( outputFilename );
> 
>         ImageType::Pointer image = reader->GetOutput();
>        
>         const ImageType::PointType& orgn = image->GetOrigin();
>         std::cout << "Origin = ";
>         std::cout << orgn[0] << ", " << orgn[1] << std::endl;
> 
>         ImageType::IndexType pixelIndex;
>        
>         pixelIndex[0] = 27; // x position
>         pixelIndex[1] = 29; // y position
>         std::cout << "x position: " << pixelIndex[0] << "\ny position: " << pixelIndex[1] << std::endl;
>        
>        
>         ImageType::PixelType pixelValue = image->GetPixel(pixelIndex);
>        
>         std::cout << "valore pixel: " << pixelValue << std::endl;
> 
>         image->SetPixel( pixelIndex, pixelValue+1 );
>        
>        
>         writer->SetInput( image );
> 
>         try
>         {
>                 writer->Update();
>         }
>         catch( itk::ExceptionObject & err )
>         {
>                 std::cerr << "ExceptionObject caught !" << std::endl;
>                 std::cerr << err << std::endl;
>                 return EXIT_FAILURE;
>         }
> 
>         return EXIT_SUCCESS;
> }
> 
> 
> 
> Thanks
> Edoardo
> 
> <ATT00001..txt>

========================================================
Bradley Lowekamp  
Lockheed Martin Contractor for
Office of High Performance Computing and Communications
National Library of Medicine 
blowekamp at mail.nih.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100217/b6719834/attachment.htm>


More information about the Insight-users mailing list