[Rtk-users] volume with diffieret pixel type
Guangming Zang
guangming.zang at kaust.edu.sa
Tue Jul 28 13:38:45 EDT 2015
Hi, in ITK, the default type are described in *Utilities\MetaIO\metaTypes.h*
MET_CHAR,
MET_UCHAR
,
MET_SHORT,
MET_USHORT,
MET_INT,=20
MET_UINT,=20
MET_FLOAT,=20
MET_DOUBLE,
and for .mha file, the header file includes information like:
ObjectType = Image
NDims = 3
BinaryData = True
BinaryDataByteOrderMSB = False
CompressedData = False
TransformMatrix = 1 0 0 0 1 0 0 0 1
Offset = 0 0 0
CenterOfRotation = 0 0 0
AnatomicalOrientation = RAI
ElementSpacing = 1 1 1
DimSize = 128 128 128
ElementType = MET_FLOAT
ElementDataFile = LOCAL
And recently i just programmed to read and write .mha files. below is
the code. u can just replace ElementType from
MET_FLOAT
to
MET_SHORT
in your application.
hope this helps:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <sstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int m_Dims[3];
float spacing[3];
m_Dims[0]=128;
m_Dims[1]=128;
m_Dims[2]=128;
spacing[0]=spacing[1]=spacing[2]=1.0f;
ostringstream buffer, buffer1, buffer2, buf3;
buffer << spacing[0];
string sp= buffer.str();
buffer1 << spacing[1];
string sp1 = buffer1.str();
buffer2 << spacing[2];
string sp2 = buffer2.str();
string ElementSpacing ="ElementSpacing= "+sp+" "+sp1+" "+sp2+"\n";
// int ss=1000;
char temp[64], temp1[64],temp2[64];
string str;
sprintf(temp, "%d", m_Dims[0]);
sprintf(temp1, "%d", m_Dims[1]);
sprintf(temp2, "%d", m_Dims[2]);
string s(temp),s1(temp1),s2(temp2);
string DimSize="DimSize= "+s+" "+s1+" "+s2+"\n";
string Mywritefile="ObjectType = Image\nNDims = 3\nBinaryData =
True\nBinaryDataByteOrderMSB = False \nCompressedData = False
\nTransformMatrix = 1 0 0 0 1 0 0 0 1 \n" ;
Mywritefile+="Offset = 0 0 0 \nCenterOfRotation = 0 0 0
\nAnatomicalOrientation = RAI \n";
Mywritefile+=ElementSpacing+DimSize+"ElementType = MET_FLOAT
\nElementDataFile = LOCAL \n";
// ElementSpacing = 1 1 1 \nDimSize = 128 128 128 \nElementType =
MET_FLOAT \nElementDataFile = LOCAL \n";
int leng=Mywritefile.length();
cout<<Mywritefile<<endl;
char* MyOutputfile = strdup(Mywritefile.c_str());
FILE *fw;
fw=fopen("test.mha","wb"); //MyOutputfile
fwrite(MyOutputfile,sizeof(char),leng,fw);
fwrite(
your_volume_data
,sizeof(
your_volumedata_type
),m_Dims[0]*m_Dims[1]*m_Dims[2],fw);
fclose(fw);
}
cheers
Guangming
*Guangming Zang (Alex)*
*King Abdullah University of Science and Technology(KAUST)*
*University of Chinese Academy of Sciences(UCAS)*
--
------------------------------
This message and its contents, including attachments are intended solely
for the original recipient. If you are not the intended recipient or have
received this message in error, please notify me immediately and delete
this message from your computer system. Any unauthorized use or
distribution is prohibited. Please consider the environment before printing
this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/rtk-users/attachments/20150728/17e6ccf7/attachment-0010.html>
More information about the Rtk-users
mailing list