[Insight-users] Reading bmp image with ITK
Luis Ibanez
luis.ibanez at kitware.com
Tue Nov 2 13:28:17 EST 2004
Hi Jimmy,
When you read/write BMP images you should use
images defined over RGBPixels. Like in
typedef unsigned char ComponentType;
typedef itk::RGBPixel<ComponentType> 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();
reader->SetFileName("Input.bmp");
reader->Update();
Please find attached the modified version of
your program. It is now using RGBPixels and
it is working fine for reading and writing BMP.
Regards,
Luis
-----------------------------
Jimmy Wong wrote:
> Thank you for these information.
>
> Actually, I am using the data provided by Brainweb Database.
>
> But if I want to use some synthetic images to test my algorithm, I have
> no choice but use bmp. For this part, any suggestion.
>
> For the program, I think it should be ok. But whenever I run it, it
> poped a dialog with " The instruction at "0x77f58df" referenced memory
> at "0x2a262430". The memory could not be "written"". I don't know why.
>
> My synthetic image is 256 by 256 bmp image.
>
> Zhimin
>
>
>> From: Luis Ibanez <luis.ibanez at kitware.com>
>> To: Jimmy Wong <good_piggy at msn.com>
>> CC: Insight-users at itk.org
>> Subject: Re: [Insight-users] Reading bmp image with ITK
>> Date: Mon, 01 Nov 2004 10:33:20 -0500
>>
>>
>> Hi Jimmy,
>>
>> If you want to rank these three file formats by
>> they suitability for storing Medical Images,
>> here is a tentative list:
>>
>> BMP is poor
>> JPEG is poorer
>> RAW is the poorest
>>
>> All of them lack fields for patient information,
>> spacing (BMP has a bit of spacing in the form of
>> #pixels per centimeter), origin, orientation.
>>
>> JPEG is poorer because it uses lossy compression.
>> The JPEG compression algorithm was not designed
>> for medical images but for picture scenes, for
>> example the photos of a Birthday party or the
>> picture of a dog sitting in the backyard. :-)
>>
>> JPEG is not what you want to use if your image
>> actually contain a tumor. It is particularly bad
>> for segmentation because the compression is done
>> in blocks of 8x8 pixels, therefore the consistency
>> of intensities across those blocks is compromised.
>>
>> RAW is plain *dangerous* it doesn't contain any
>> information about the pixel type being used, the
>> image dimensions, the endianess, the pixel spacing,
>> the origin. In practice, RAW is useless if you don't
>> have an accompanying header that tells you the
>> characteristics of the image.
>>
>>
>> If you are storing real patient data you may want
>> to use Analyze, GIPL or MetaImage, or to stick to
>> the original DICOM files that are produced from
>> CT and MRI scanners.
>>
>>
>> -------------------
>>
>> About your code:
>>
>> It seems to be fine for reading a BMP image
>> and writing it down.
>>
>> Did you find any problem while running this code ?
>>
>>
>>
>> Regards,
>>
>>
>> Luis
>>
>>
>>
>> PS: Note that a bug was recently fixed in the BMP
>> reader/writer. This bug manifested in problems when
>> reading or writing images whose row size was not a
>> multiple of 4. If you are using BMP images you may
>> want to use a recent CVS checkout of the toolkit.
>>
>>
>> --------------------
>> Jimmy Wong wrote:
>>
>>> Thank you for your reply.
>>>
>>> If bmp is poor, which one is better? raw or jpg?
>>>
>>> I've read the manual, but very confuse of it.
>>>
>>> Here is my main function
>>>
>>> **********************************************
>>> void main()
>>> {
>>> // Definition of stuctures
>>> 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;
>>>
>>> // Create a object of reader and read the image
>>> ReaderType::Pointer reader = ReaderType::New();
>>> WriterType::Pointer writer = WriterType::New();
>>>
>>> const char pInputfileName[] = "Input.bmp";
>>> const char pOutputfileName[] = "Output.bmp";
>>>
>>> reader->SetFileName( pInputfileName );
>>> reader->Update();
>>>
>>> ImageType::Pointer image = reader->GetOutput();
>>>
>>>
>>> writer->SetFileName( pOutputfileName );
>>>
>>> writer->SetInput( reader->GetOutput() );
>>>
>>> try
>>> {
>>> writer->Update();
>>> }
>>>
>>> catch( itk::ExceptionObject & err )
>>> {
>>> std::cout << "ExceptionObject caught !" << std::endl;
>>> std::cout << err << std::endl;
>>> exit(-1);
>>> }
>>> }
>>> ******************************************************
>>>
>>> Is it correct for reading image?
>>>
>>> Thank you again.
>>>
>>> Zhimin
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>
> # This project is designed to be built outside the Insight source tree.
> PROJECT(ReadWriteImage)
>
> # Find ITK.
> FIND_PACKAGE(ITK)
> IF(ITK_FOUND)
> INCLUDE(${ITK_USE_FILE})
> ENDIF(ITK_FOUND)
>
> # Find VTK.
> FIND_PACKAGE ( VTK)
> IF ( VTK_FOUND)
> INCLUDE( ${USE_VTK_FILE} )
> ENDIF( VTK_FOUND)
>
> INCLUDE_DIRECTORIES(${myProject_SOURCE_DIR})
>
> ADD_EXECUTABLE(ReadWriteImage ReadWriteImage.cxx )
>
> TARGET_LINK_LIBRARIES(ReadWriteImage
> ITKBasicFilters ITKCommon ITKIO
> vtkRendering vtkGraphics vtkHybrid
> vtkImaging vtkIO vtkFiltering vtkCommon)
>
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkImage.h"
>
> void main()
> {
> // Definition of stuctures
> 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;
>
> // Create a object of reader and read the image
> ReaderType::Pointer reader = ReaderType::New();
> WriterType::Pointer writer = WriterType::New();
>
> const char pInputfileName[] = "Input.bmp";
> const char pOutputfileName[] = "Output.bmp";
>
> reader->SetFileName( pInputfileName );
> reader->Update();
>
> ImageType::Pointer image = reader->GetOutput();
>
>
> writer->SetFileName( pOutputfileName );
>
> writer->SetInput( reader->GetOutput() );
>
> try
> {
> writer->Update();
> }
>
> catch( itk::ExceptionObject & err )
> {
> std::cout << "ExceptionObject caught !" << std::endl;
> std::cout << err << std::endl;
> exit(-1);
> }
> }
>
>
================================================================
-------------- next part --------------
#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkImage.h"
#include "itkRGBPixel.h"
int main()
{
// Definition of stuctures
typedef unsigned char ComponentType;
typedef itk::RGBPixel<ComponentType> PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
// Create a object of reader and read the image
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
const char * pInputfileName = "Input.bmp";
const char * pOutputfileName = "Output.bmp";
reader->SetFileName( pInputfileName );
reader->Update();
ImageType::Pointer image = reader->GetOutput();
writer->SetFileName( pOutputfileName );
writer->SetInput( reader->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cout << "ExceptionObject caught !" << std::endl;
std::cout << err << std::endl;
exit(-1);
}
return 0;
}
More information about the Insight-users
mailing list