[Insight-users] Problem with GetPixel

Gomez, Alberto alberto.gomez at kcl.ac.uk
Wed Feb 17 11:09:45 EST 2010


Hi,

what is the size of our image? Do something like

        ImageType::Size imageSize = image->GetLargestPossibleRegion().GetSize();

and print the size, to verify that your index is not too big.
Also, verify that the image is correctly red (for example with std::cout << image << std::endl;)

Hth



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


-- 

Alberto Gómez

/Division of Imaging Sciences
The Rayne Institute
4th Floor, Lambeth Wing
St Thomas' Hospital
London SE1 7EH /

phone: +44 (0) 20 718 88364
email: alberto.gomez at kcl.ac.uk <mailto:alberto.gomez at kcl.ac.uk>



More information about the Insight-users mailing list