[Insight-users] Re: Importing image data from a byte array
Karthik Krishnan
Karthik.Krishnan at kitware.com
Wed Sep 7 17:14:02 EDT 2005
Chris Farmer wrote:
>Thanks for the pointers to the ImportImageFilter, but I'm still having
>problems. Either I'm doing something stupid, misunderstanding a
>fundamental concept, or both. I read through the pdf in the getting
>started V guide, and it looked a lot like the Image5 example in the
>source distribution, so I started trying to bastardize the Image5.cxx
>file to accomplish my goal of importing a byte array (containing png
>data) into an itk image. I used a cheesy 240 x 160 pixel png I found on
>the web as a data source. I just read its bytes into an unsigned char
>array, then try to use that array as a source in the SetImportPointer
>call. I don't get any exception thrown, but my output file
>(burgerout.png) is a garbled image looking nothing like the original.
>Clearly I'm missing something. Is it acceptable to pass in PNG data in
>this way?
>
>Thanks,
>Chris
>
>
>Here's the code that I'm trying to use:
>
> typedef unsigned char PixelType;
> const unsigned int Dimension = 2;
> typedef itk::Image< PixelType, Dimension > ImageType;
> typedef itk::ImportImageFilter< PixelType, Dimension >
>ImportFilterType;
>
> ImportFilterType::Pointer importFilter = ImportFilterType::New();
>
> ImportFilterType::SizeType size;
>
> size[0] = 240;
> size[1] = 160;
>
> ImportFilterType::IndexType start;
> start.Fill( 0 );
>
> ImportFilterType::RegionType region;
> region.SetIndex( start );
> region.SetSize( size );
>
> importFilter->SetRegion( region );
>
> double origin[ Dimension ];
> origin[0] = 0.0;
> origin[1] = 0.0;
> importFilter->SetOrigin( origin );
>
> double spacing[ Dimension ];
> spacing[0] = 1.0;
> spacing[1] = 1.0;
> importFilter->SetSpacing( spacing );
>
> PixelType * localBuffer = new PixelType[ 4131 ];
> FILE* f;
> f = fopen("C:\\burger.PNG", "r+b");
> size_t read = fread(localBuffer, sizeof(PixelType), 4131, f);
>
>
You cannot read in a PNG image like a standard file stream . PNG's have
headers... You could do this with a "RAW" file format. (which you
probably will never find on the web). Such a raw file will be of size
240*160*sizeof(PixelType).
This is why your output is garbled.
> fclose(f);
>
> // make sure the data in localBuffer is correct
> f = fopen("c:\\burgertest.png", "w+b");
> fwrite(localBuffer, sizeof(PixelType), read, f);
> fclose(f);
>
> const bool importImageFilterWillOwnTheBuffer = true;
> importFilter->SetImportPointer( localBuffer, read,
> importImageFilterWillOwnTheBuffer );
> importFilter->Update();
>
> typedef itk::ImageFileWriter< ImageType > WriterType;
> WriterType::Pointer writer = WriterType::New();
> writer->SetFileName( "c:\\burgerout.png" );
> writer->SetInput( importFilter->GetOutput() );
>
> try
> {
> writer->Update();
> }
> catch( itk::ExceptionObject & exp )
> {
> std::cerr << "Exception caught !" << std::endl;
> std::cerr << exp << std::endl;
> }
>
>
>
>
>-----Original Message-----
>From: Brian Eastwood [mailto:beastwoo at email.unc.edu]
>Sent: Tuesday, September 06, 2005 7:07 PM
>To: insight-users at itk.org
>Subject: [Insight-users] Re: Importing image data from a byte array
>
>Hi Chris,
>
>You are looking for the itk::ImportImageFilter. An example of how to
>use it is in the Getting Started V guide available here:
>
>http://www.itk.org/HTML/Tutorials.htm
>
>You first need to set up the size, origin, and spacing of your image.
>The method ImportImageFilter::SetImportPointer() allows you to pass the
>pointer to your image buffer--you specify the size of the image buffer
>and indicate whether you want ITK to manage the buffer memory.
>
>Hope this helps.
>Brian
>
>
>
>>Message: 4
>>Date: Tue, 6 Sep 2005 16:21:04 -0700
>>From: "Chris Farmer" <cfarmer at scitegic.com>
>>Subject: [Insight-users] Importing image data from a byte array
>>To: <insight-users at itk.org>
>>Message-ID:
>> <830D8D4719112B418ABBC3A0EBA958128FA568 at webmail.scitegic.com>
>>Content-Type: text/plain; charset="us-ascii"
>>
>>
>>Hi. I've just started working with the ITK library and have a simple
>>question. In my current application, I have a byte array that contains
>>image data (usually in png format). I'd like to use that data in the
>>ITK world, but I can't see a good way to use it without first writing
>>out a file and using the standard reader model that is shown in the
>>examples. I poked through the ImageIOBase code to see how easy it
>>
>>
>would
>
>
>>be to use a byte array as a data source instead of a file, but it
>>
>>
>didn't
>
>
>>seem too obvious.
>>
>>Can someone please give me a pointer to using a byte array as a source
>>of image data?
>>
>>
>>Thanks,
>>Chris
>>
>>
>>
>_______________________________________________
>Insight-users mailing list
>Insight-users at itk.org
>http://www.itk.org/mailman/listinfo/insight-users
>
>
>
>_______________________________________________
>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