[Insight-users] new ImageIO.cxx

Luis Ibanez luis.ibanez at kitware.com
Mon Aug 22 19:18:31 EDT 2005


Hi Martijn,

The problem that you describe sounds like a pixel type issue.

Could you post technical data about the image that you are reading,
and also attach the MetaImage header that you generated as output.
(only the header, not the binary data).

Please mention:

1) pixel type
2) image dimension
3) number of pixel on every dimension



Thanks


   Luis


---------------------------
Martijn van der Bom wrote:
> Hi All,
> 
> I've been trying to write a new ImageIO class to read (and write) images 
> in .v3d format (volume data). In order to test it i've tried to read an 
> .v3d image and write it as an .mhd
> So far getting the header info is no problem but when  I open the new 
> mhd-image in a viewer only the first 7 (and a part of the 8th) slice are 
> correct. The rest of the image has maximum greyvalue.
> 
> 
> Thoughts anyone?
> 
> -- 
> 
> Martijn van der Bom
> 
> Image Sciences Institute
> 
> University Medical Centre Utrecht
> 
> Heidelberglaan 100, 3584 CX Utrecht, The Netherlands
> 
> phone: +31 (0)30 250 6711, fax: +31 (0)30 251 3399
> 
> martijn at isi.uu.nl <mailto:martijn at isi.uu.nl>, 
> http://www.isi.uu.nl/People/Martijn
> 
> 
> ------------------------------------------------------------------------
> 
> /*=========================================================================
> 
>   
>   Module:   itkV3DImageIO.h
>   By:		ISI Image Registration Group, BOM
>   
> 
> =========================================================================*/
> 
> #include "itkV3DImageIO.h"
> #include "itkExceptionObject.h"
> #include "itkByteSwapper.h"
> #include <iostream>
> #include <list>
> #include <string>
> #include <math.h>
> 
> namespace itk
> {
> 
> 	V3DImageIO::V3DImageIO() {
> 		this->SetNumberOfDimensions(3);
> 		m_PixelType = SCALAR;
> 		m_ComponentType = USHORT;
> 	}
> 
> 	V3DImageIO::~V3DImageIO() {
> 	}
> 
> bool V3DImageIO::CanReadFile(const char* f) {
>   std::string filename = f;
>   m_FileName = f;
>   m_ComponentType = USHORT;
> 
> 	if( filename == ""){
> 	  itkDebugMacro(<<"The filename extension is not recognized");
> 	  return false;
> 	}
>   bool extensionFound = false;
>   std::string::size_type v3dPos = filename.rfind(".v3d");
> 	if ((v3dPos != std::string::npos)
>      && (v3dPos == filename.length() - 4)) {
>      extensionFound = true;
>     }
> 
>   v3dPos = filename.rfind(".V3D");
> 	if ((v3dPos != std::string::npos)
>       && (v3dPos == filename.length() - 4)) {
> 	  extensionFound = true;
>     }
>   
> 	if( !extensionFound ) {
>       itkDebugMacro(<<"The filename extension is not recognized");
>       return false;
>     }
> 	else {
> 		return true;
> 	}
> }
> 
> void V3DImageIO::readLittleEndian(std::ifstream &is, unsigned int &i) {
> 	is.read( (char* ) &i, (sizeof(int)));
> }
> 
> void V3DImageIO::readLittleEndian(std::ifstream &is, double &i) {
> 	is.read( (char* ) &i, (sizeof(double)));
> }
> 
> void V3DImageIO::ReadImageInformation() {
>   m_Spacing[0] = 1.0; 
>   m_Spacing[1] = 1.0;
>   m_Spacing[2] = 1.0;
>   m_PixelType = SCALAR;
>   m_ComponentType = USHORT;
>   this->SetNumberOfDimensions(3);
>   
>   unsigned int nrOfVoxelsx;
>   unsigned int nrOfVoxelsy;
>   unsigned int nrOfVoxelsz;
>   double voxelSizex;
>   double voxelSizey;
>   double voxelSizez;
>   double centerx;
>   double centery;
>   double centerz;
> 
>   const int versionIdLength =40;
>   char buffer[versionIdLength];
>   char R3[versionIdLength] = "3D-RA R3.1";
>   const char R4[versionIdLength] = "3D-RA R4.1";
> 
>   std::string filename = m_FileName;
>   std::ifstream infile(filename.c_str());
>   infile.read(buffer, versionIdLength);
> 
>   if(0 == strcmp(R4, buffer)){
> 	  readLittleEndian(infile,nrOfVoxelsx);
> 	  readLittleEndian(infile,nrOfVoxelsy);
> 	  readLittleEndian(infile,nrOfVoxelsz);
> 			  
> 	  readLittleEndian(infile,voxelSizex);
> 	  readLittleEndian(infile,voxelSizey);
> 	  readLittleEndian(infile,voxelSizez);
> 
> 	  readLittleEndian(infile,centerx);
> 	  readLittleEndian(infile,centery);
> 	  readLittleEndian(infile,centerz);
>   }
> 
>   m_Dimensions[0] = nrOfVoxelsx;
>   m_Dimensions[1] = nrOfVoxelsy;
>   m_Dimensions[2] = nrOfVoxelsz;
>   m_Spacing[0] = voxelSizex;
>   m_Spacing[1] = voxelSizey;
>   m_Spacing[2] = voxelSizez;
>   m_Origin[0] = centerx - (0.5*(nrOfVoxelsx * voxelSizex));
>   m_Origin[1] = centery - (0.5*(nrOfVoxelsy * voxelSizey));
>   m_Origin[2] = centerz - (0.5*(nrOfVoxelsz * voxelSizez));
>   this->SetByteOrderToLittleEndian();
>   this->SetFileTypeToBinary();
>   this->ComputeStrides();
>   
> }
> 
> void V3DImageIO::Read(void* buffer) {
> 
> 	long nrOfVoxels = this->m_Dimensions[0] * this->m_Dimensions[1] * this->m_Dimensions[2];
> 	long componentSize = sizeof(unsigned short);
> 	long dataSize = nrOfVoxels * componentSize;
> 	long headerSize = (6 * ( sizeof ( int ))) + (6 * ( sizeof ( double ))) + versionIdLength;
> 	std::string filename = m_FileName;
> 	std::ifstream infile(filename.c_str());
>   
>   // check filesize en headersize...
>  
>   /** Set to right position and do the reading. */
>   infile.seekg (headerSize, std::ios::beg);
> 
>   char * newBuffer = static_cast<char *>(buffer);
>   infile.read( newBuffer, this->GetImageSizeInBytes());
>   
> }
> 
> bool V3DImageIO::CanWriteFile( const char * name ) {
>   std::string filename = name;
> 
>   if (filename == "")
>     {
>     return false;
>     }
>   
>   std::string::size_type V3DPos = filename.rfind(".v3d");
>   if ( (V3DPos != std::string::npos)
>        && (V3DPos == filename.length() - 4) )
>     {
>     return true;
>     }
> 
>   V3DPos = filename.rfind(".V3D");
>   if ( (V3DPos != std::string::npos)
>        && (V3DPos == filename.length() - 4) )
>     {
>     return true;
>     }
>   else {
> 	  return false;
>   }
> }
> 
> void V3DImageIO::WriteImageInformation() {
> }
> 
> void V3DImageIO::Write(const void *buffer) {
> 
>   unsigned int nDims = this->GetNumberOfDimensions();
> 
> 	if(nDims != 3) {
>       ExceptionObject exception(__FILE__, __LINE__);
>       exception.SetDescription("V3DImageIO cannot write images with a dimension != 3");
>       throw exception;
>     }
> 
> 	if(this->GetComponentType() != USHORT) {
>       itkExceptionMacro(<<"V3DImageIO supports unsigned short only");
>     }
> 
> 	std::ofstream outfile;
> outfile.open(m_FileName.c_str(), std::ios::binary | std::ios::out);
> 
> 	if( m_Ofstream.fail() ) {
>       ExceptionObject exception(__FILE__, __LINE__);
>       exception.SetDescription("File cannot be write");
>       throw exception;
> 	}
> 	unsigned int tmp = m_Dimensions[0];
> 	outfile.write( (char *) tmp,sizeof(int));
> 	tmp = m_Dimensions[1];
> 	outfile.write( (char *) tmp,sizeof(int));
> 	tmp = m_Dimensions[2];
> 	outfile.write( (char *) tmp,sizeof(int));
> 
> 	double tmp_d = m_Spacing[0];
> 	outfile.write((char *) &tmp_d ,sizeof(double));
> 	tmp_d = m_Spacing[1];
> 	outfile.write((char *) &tmp_d,sizeof(double));
> 	tmp_d = m_Spacing[2];
> 	outfile.write( (char *) &tmp_d,sizeof(double));
> 
> 	tmp_d = (m_Origin[0] + (0.5*(m_Dimensions[0] * m_Spacing[0])));
> 	outfile.write((char *) &tmp_d,sizeof(double));
> 	tmp_d = (m_Origin[1] + (0.5*(m_Dimensions[1] * m_Spacing[1])));
> 	outfile.write((char *) & tmp_d,sizeof(double));
> 	tmp_d = (m_Origin[2] + (0.5*(m_Dimensions[2] * m_Spacing[2])));
> 	outfile.write((char *) & tmp_d,sizeof(double));
> 
> 	tmp = 0;
> 	outfile.write( (char *) tmp, sizeof(unsigned short) * 8);
> 	tmp = 100;
> 	outfile.write( (char *) tmp, sizeof(int));
> 	outfile.write( (char *) true, sizeof(int));
> 
> 	
> 
> } // end namespace itk
> 
> 
> }
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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