[Insight-users] finding the type of an image

Vincent Garcia vincent.garcia at inria.fr
Fri Jul 1 07:32:56 EDT 2011


Hi John, 

I'd say that reading images in ITK is not the most intuitive way to do it if you care to keep the original type. 
So, what I do is the following. 
I have written this function that reads only the header of the image : 

------------------------------------------------------------------------- 
itk::ImageIOBase::Pointer 
readImageInformation( std::string fileName ) 
{ 

// Define image IO 
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO( fileName.c_str(), itk::ImageIOFactory::ReadMode ); 

// Test if image exists 
if ( !imageIO ) 
throw std::runtime_error( "Could not read image " + fileName + "." ); 

// Read image information 
try 
{ 
imageIO->SetFileName( fileName ); 
imageIO->ReadImageInformation(); 
} 
catch( itk::ExceptionObject& ) 
{ 
throw std::runtime_error( "Could not read image information." ); 
} 
return imageIO; 
} 
------------------------------------------------------------ 

The output of this function if an itk::ImageIOBase. 
Then, you can figure out what is the type of your image by using theses function on the ImageIOBase pointer : 

image_base->GetPixelType() 
image_base->GetComponentType() 

The first one tells you if your image is scalar, RGB, vector, etc. (see http://www.itk.org/Doxygen320/html/classitk_1_1ImageIOBase.html#abd189f096c2a1b3ea559bc3e4849f658). 
The second one tells you the type of each component : short, int, double, etc. (see http://www.itk.org/Doxygen320/html/classitk_1_1ImageIOBase.html#a8dc783055a0af6f0a5a26cb080feb178) 

Like that, you can have everything you need to correctly read your image (dimension etc.). 
Read the class, it's pretty clear. 

Then, when you know the type of the input image, you use the correct ImageFileReader (I mean the correct templates). 
Basically, it will be a BIG switch... (thanks to templates) 

Cheers, 

Vincent Garcia 

----- Original Message -----


From: "john smith" <mkitkinsightuser at gmail.com> 
To: insight-users at itk.org 
Sent: Friday, 1 July, 2011 11:39:07 AM 
Subject: [Insight-users] finding the type of an image 

I want somehow to find the type of an image. To be more specific, I have a greyscale and an RGB image and I want to process this two images. But when I load the image I do not know the type of the reader that I must use. Must I use an RGB pointer or a greyscale pointer? 
I hope somebody could help me to solve this problem 

Thanks in advance 


_____________________________________ 
Powered by www.kitware.com 

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html 

Kitware offers ITK Training Courses, for more information visit: 
http://www.kitware.com/products/protraining.html 

Please keep messages on-topic and check the ITK FAQ at: 
http://www.itk.org/Wiki/ITK_FAQ 

Follow this link to subscribe/unsubscribe: 
http://www.itk.org/mailman/listinfo/insight-users 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110701/caca47a5/attachment.htm>


More information about the Insight-users mailing list