[Insight-users] Strange behavior when reading RGB .mhd files

Luis Ibanez luis.ibanez at kitware.com
Sun Apr 10 11:40:53 EDT 2005


Hi Rhazes,

Please don't use the MetaImage reader in order to read TIFF images.

              ITK DOES have a TIFF reader.

You can simply pass your filename .tif to the ImageFileReader.
The IO factory will take care of the rest.

When you encapsulate a TIF file inside a MetaImage header, the meta
image reader treats the tif file as a RAW file, and interprets the
RGB componets in a naive order. This naive order visibly don't match
the actual encoding of the tiff file.


Please use just the ImageFileReader.


If you happen to have a series of TIFF images to be read into a
Volume. Then you can use the ImageSeriesReader.   You will find
examples in the directory


                     Insight/Examples/IO



   Regards,


       Luis




-------------------------------------------------------------------
Rhazes Spell wrote:

> I get strange results when I read an .mhd file specifying a stack of  
> RGB tif images.  The output images have the color channels swapped (R=B  
> and G=R.  There is no blue channel in the original image).  To isolate  
> my problem I changed the mhd to only specify one slice.  I then read  
> the slice in through the mhd format and as  a tif.  When I read it in  
> as a tif it writes out properly (RGB = RGB).  However, when I read the  
> mhd file and writ it out the color swapping problem occurs.  I have  
> included my test code and the mhd file.      Any assistance that you  
> can provide would be GREATLY appreciated.
> 
> 
> NDims = 3
> DimSize = 2048 2048 1
> ElementSpacing = 1 1 1
> Position = 0 0 0
> ElementByteOrderMSB = False
> ElementNumberOfChannels = 3
> ElementType = MET_UCHAR
> ElementDataFile = 10x-CZ1197%03d.tif 8 8 1  (So that I only read in one  
> slice.  The problem still occurs when I read in more than one):
> 
> 
> =-=-=-=-=-==-=-=-=-=-=-=-=-=
> /* *****************
>  * Test reading and processing 3D RGB images (I only want the green  
> channel for now)
>  *
>  * To run: input file; output file; resample dimensionx; resample  
> dimensiony; 3d flag (optional)
>  * ./Preprocess cz1197/10xCZ1197.mhd test3d.tif 2 2 3d
>  *
>  * ***************** */
> 
> #include "itkImage.h"
> #include "itkImageFileReader.h"
> #include "itkImageFileWriter.h"
> #include "itkRGBPixel.h"
> 
> #include <unistd.h>
> 
> using namespace std;
> 
> //Pixel Types
> typedef unsigned char charPixelType;
> typedef itk::RGBPixel<charPixelType> rgbPixelType;
> 
> //Image Types
> typedef itk::Image<rgbPixelType, 3> Image3DType;
> typedef itk::Image<rgbPixelType, 2> Image2DType;
> 
> //IO Types
> typedef itk::ImageFileReader<Image3DType> Reader3D;
> typedef itk::ImageFileReader<Image2DType> Reader2D;
> 
> typedef itk::ImageFileWriter<Image3DType> Writer3D;
> typedef itk::ImageFileWriter<Image2DType> Writer2D;
> 
> 
> int main(int argc, char * argv[]) {
>     if(atoi(argv[3]) == 3) {
>         Reader3D::Pointer inputReader3D = Reader3D::New();
>         Writer3D::Pointer outputWriter3D = Writer3D::New();
> 
>         inputReader3D->SetFileName(argv[1]);
>         try {
>             inputReader3D->Update();
>         }catch( itk::ExceptionObject &err ) {
>             cout << "Exception while reading file " << endl;
>             cout << err << endl;
>         }
> 
>         Image3DType::Pointer inputImage = inputReader3D->GetOutput();
>         cout << inputImage << endl;
> 
>         outputWriter3D->SetFileName(argv[2]);
>         outputWriter3D->SetInput(inputImage);
>         try{
>             outputWriter3D->Update();
>         }catch (itk::ExceptionObject &err) {
>             cout << "Exception while writing file " << endl;
>             cout << err << endl;
>         }
>     } else {
>         cout << "Running 2D.... " << endl;
> 
>         Reader2D::Pointer inputReader2D = Reader2D::New();
>         Writer2D::Pointer outputWriter2D = Writer2D::New();
> 
>         inputReader2D->SetFileName(argv[1]);
>         try {
>             inputReader2D->Update();
>         }catch( itk::ExceptionObject &err ) {
>             cout << "Exception while reading file " << endl;
>             cout << err << endl;
>         }
> 
>         Image2DType::Pointer inputImage = inputReader2D->GetOutput();
>         cout << inputImage << endl;
> 
>         outputWriter2D->SetFileName(argv[2]);
>         outputWriter2D->SetInput(inputImage);
>         try{
>             outputWriter2D->Update();
>         }catch (itk::ExceptionObject &err) {
>             cout << "Exception while writing file " << endl;
>             cout << err << endl;
>         }
> 
> 
> 
>     }
> 
> 
> 
> 
> 
> 
> ************************************************************************ 
> ****
> Rhazes Spell, Ph.D.
> Duke University,
>          -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
> Visualization Technology Group - http://vis.duke.edu
> Ctr. for Environmental Genomics - http://www.envgenomics.duke.edu
> Ctr. for Computational Science Engineering and Medicine -  
> http://csem.duke.edu
>          -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=
> rhazes.spell at alumni.duke.edu
> 919.660.5595 (office)
> ************************************************************************ 
> ****
> 
> _______________________________________________
> 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