[Insight-users] Fwd: reading jpg files
Julien Jomier
jjomier at cs.unc.edu
Wed Sep 15 17:50:21 EDT 2004
Hi Jie,
Welcome to ITK!
Couple of comments regarding your code.
1) You don't have to allocate the image when the image is produced by a
reader (the reader is allocating the memory and setting the regions, etc...)
2) You need to do a "reader->Update();" before trying to access the image.
3) Is your JPEG image RGB? if this is the case, you should change your
ImageType to be:
typedef itk::RGBPixel<unsigned char> PixelType;
typedef itk::Image<PixelType,2> ImageType;
4) GetPixel() is very slow when accessing the pixel value of an image. In
your case, you can simply do:
std::cout << inputIt.Get() << " " << std::endl;
Hope this helps,
Let us know if you have any further problems,
Julien
> -----Original Message-----
> From: insight-users-bounces at itk.org
> [mailto:insight-users-bounces at itk.org] On Behalf Of Jie Zhu
> Sent: Wednesday, September 15, 2004 5:35 PM
> To: insight-users at itk.org
> Subject: [Insight-users] Fwd: reading jpg files
>
>
> Hi All,
> I am new to itk, and I need to use it with JPEG files. I wrote a
> simple program putting the jpeg file in image and tested to see if
> image.GetPixel() returns the correct value of the pixels. But the
> result I get looks like:
> 205
> 205
> 205
> 205
> ...
> Although the input image have a variety of pixel values, the pixel
> values GetPixel() return are all 205 or "=" if I dont convert it to
> (int). Can somebody please help me and tell me what I'm doing wrong.
> Thank you.
> Jie
>
> Below is the code
> --------------------------------------------------------------
> -----------------------------------------------
> typedef itk::Image<unsigned char,2> ImageType;
>
> typedef itk::ImageFileReader< ImageType > ReaderType;
>
> ReaderType::Pointer reader = ReaderType::New();
>
> const char * inputFilename = argv[1];
> const char * outputFilename = argv[2];
>
> reader->SetFileName( inputFilename );
> writer->SetFileName( outputFilename );
>
> ImageType::Pointer image = reader->GetOutput();
>
> //set regions in input image
> ImageType::RegionType region;
> ImageType::RegionType::SizeType size;
> ImageType::RegionType::IndexType index;;
> index[ 0 ] = 0;
> index[ 1 ] = 0;
> size[ 0 ] = 255;
> size[ 1 ] = 255;
>
> region.SetSize( size );
> region.SetIndex( index );
>
> image->SetRegions( region );
> image->Allocate();
>
> //creat linear iterators
> //typedef itk::ImageRegionConstIterator< ImageType >
> ConstIteratorType;
>
> typedef itk::ImageLinearConstIteratorWithIndex< ImageType >
> ConstIteratorType;
> ConstIteratorType inputIt( image, image->GetRequestedRegion() );
> inputIt.SetDirection(0);
>
> //iteration
> for ( inputIt.GoToBegin(); ! inputIt.IsAtEnd();
> inputIt.NextLine())
> {
> inputIt.GoToBeginOfLine();
> while ( ! inputIt.IsAtEndOfLine() )
> {
> ImageType::PixelType pixelValue = image->GetPixel( indx );
> std::cout<<(int)pixelValue<<" "<<std::endl;
> ++inputIt;
>
> }
> }
>
> --
> Jie Zhu
> Cornell University
>
>
>
>
> --
> Jie Zhu
> Cornell University
> _______________________________________________
> 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