[Insight-users]Accessing pixel data in png file

Luis Ibanez luis.ibanez at kitware.com
Sat Apr 29 16:08:29 EDT 2006


Hi Siddharth,


Your guess is incorrect:


    Calling GetOuput() *does not* triggers
       an automatic call to Update()


You *must* call Update() before attempting
to call GetOutput(). Otherwise the output
is invalid, because the file has not been
read.


Your code should look like:


      try
        {
        reader->Update();
        image = reader->GetOutput();
        }



   Regards,



       Luis



========================
Siddharth Vikal wrote:
> Thanks Luis for promt reply.
> 
> I did reader->GetOutput for getting the image in a variable, i guess
> GetOutput will automatically Update internaly.
> 
> Perhaps a look my code (below) might help in finding the mistake I am 
> making:
> -------------------------------------------------------------------
>     typedef unsigned char PixelType;
>     
>        const unsigned int ImageDimension = 2;
> 
>     typedef itk::Image<PixelType, ImageDimension> ImageType;
> 
>     typedef itk::ImageFileReader<ImageType> ReaderType;
> 
>     ReaderType::Pointer reader = ReaderType::New();
>     
>     const char *filename =
> "D:\\ITK\\InsightToolkit-2.6.0_Source\\Examples\\Data\\BrainProtonDensitySliceBSplined10.png"; 
> 
> 
>     reader->SetImageIO(itk::PNGImageIO::New());
> 
>     reader->SetFileName(filename);
> 
>     ImageType::IndexType pixelIndex;
> 
>     pixelIndex[0] = 2;
>     pixelIndex[1] = 2;
> 
>     ImageType::Pointer image;
> 
>     try
>     {
>         image = reader->GetOutput();
>     }
>     catch (itk::ExceptionObject & excp)
>     {
>         std::cerr<<"error reading file"<<std::endl;
>         std::cerr<<excp<<std::endl;
>         return 1;
>     }
> 
>     ImageType::RegionType region = image->GetLargestPossibleRegion();
> 
>     unsigned long ImageRegionSize = 0;
>     
>        region.GetSize(ImageRegionSize);
> 
>     std::cout<<"Region size" <<ImageRegionSize<<std::endl;
> 
>     ImageType::PixelType pixelValue = image->GetPixel(pixelIndex);
>     
>     std::cout<<pixelValue<<std::endl;
> 
> 
> -------------------------------------------------------------------
> 
> There are two things:
> 
> 1) RegionSize, I am getting as zero
> 2) I am still getting run time access violation, when GetPixel call is made
> 
> And, yes thanks for pointing out that ImageIterator will be more
> efficient, I will definitely look into that. It was just out of
> curiousty and gradual learning strategy that I wanted to try out
> GetPixel.
> 
> Waiting for your reply.
> 
> regards
> siddharth
> On 4/28/06, Luis Ibanez <luis.ibanez at kitware.com> wrote:
> 
>>
>> Hi Siddrarth,
>>
>>
>> Did you called Update() in the reader before calling GetPixel() ?
>>
>> How are you getting the index range for GetPixel() ?
>> are you taking it from LargestPossible region ?
>>
>> Why are you using GetPixel() instead of using ImageIterators ?
>> GetPixel() is very slow compared to ImageIterators.
>>
>>
>> You may find useful to look at the Image Iterators chapter
>> in the ITK Software Guide
>>
>>
>>       http://www.itk.org/ItkSoftwareGuide.pdf
>>
>> Section 11.3, "Image Iterators", pdf-page 738-776.
>>
>>
>>
>>     Regards,
>>
>>
>>         Luis
>>
>>
>> ----------------------
>> Siddharth Vikal wrote:
>> > Hello,
>> >
>> > Am new to itk and am trying various exampes. I'm trying to access
>> > pixel data of a png file (from example data), but getting runtime
>> > error while using GetPrixel function. The error is some access
>> > violation, unhandled exception.
>> >
>> > can anyone please help?
>> >
>> > thanks
>> > siddharth
>> > _______________________________________________
>> > Insight-users mailing list
>> > Insight-users at itk.org
>> > http://www.itk.org/mailman/listinfo/insight-users
>> >
>> >
>>
>>
>>
> 
> 




More information about the Insight-users mailing list