[Insight-users] help with itkImportImageFilter
ankit master
ankit.master at gmail.com
Sat Nov 12 19:47:04 EST 2011
Hello,
I am trying to convert an 2d array into itkImage type and I am using
ImportImageFilter to do so. So, this is what I am doing,
1) I read data from a binary file.
and 2) create a 2d array having image data
I was reading through some examples on the internet but could not find
anything that would help me.
My problem, how do I pass the image data into the ImportImageFilter object.
so for example : the following snippet passes on pixelData to
ImportImageFilter object, but no where am I passing or using information
from the the data (realImage, from this particular example) that I want
to convert into ImportImageFilter.
float realImage[64][64]; // create a 2d
image
for (int i=0; i<64; i++)
{ for (int j=0; j<64; j++)
{
realImage[i][j]= imageData[i][j].re;
}
}
PixelType * pixelData = new PixelType[ numberOfPixels ];
//PixelType *pixelData = realImage;
int totalNumofPixels = size[0]*size[1];
importFilter -> SetImportPointer(pixelData, totalNumofPixels, true );
Any help is greatly appreciated.
Thank you in advance.
Following is my complete code,
#include "itkImage.h"
#include "itkImportImageFilter.h"
#include<iostream>
#include<fstream>
#include "itkImageFileWriter.h"
struct f32complex{
float re,im;
};
int main(int argc, char* argv[])
{
// read binary file readBinaryDataFromDisk();
f32complex imageData[64][64];
std::cout << "this works " << std:: endl;
std::ifstream readImageData("complexImage.dat", std::ios::in |
std::ios::binary);
for (int i=0; i<64; i++)
{ for (int j=0; j<64; j++)
{
readImageData.read( (char*) &imageData[i][j], sizeof(f32complex)) ;
}
}
readImageData.close();
//real part getRealImage();
float realImage[64][64];
for (int i=0; i<64; i++)
{ for (int j=0; j<64; j++)
{
realImage[i][j]= imageData[i][j].re;
}
}
// imaginary part getImaginaryImage();
float imagImage[64][64];
for (int i=0; i<64; i++)
{ for (int j=0; j<64; j++)
{
imagImage[i][j]= imageData[i][j].im;
}
}
// ITK stuff view
typedef float PixelType;
const int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImportImageFilter< PixelType, Dimension > ImportFilterType;
ImportFilterType::Pointer importFilter = ImportFilterType::New();
ImportFilterType::SizeType size;
size[0] = 64; // size along X
size[1] = 64; // size along Y
//size[2] = 200; // size along Z
ImportFilterType::IndexType start;
//start.Fill( 0 );
start[0]=0;
start[1]=0;
ImportFilterType::RegionType region;
region.SetIndex( start );
region.SetSize( size );
importFilter->SetRegion( region );
double spacing[ 2 ];
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along Y direction
//spacing[2] = 1.0; // along Z direction
double origin[ 2 ];
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // Y coordinate
//origin[2] = 0.0; // Z coordinate
importFilter->SetOrigin( origin );
importFilter->SetSpacing( spacing );
const bool importFilterWillDeleteTheInputBuffer = false;
const unsigned int numberOfPixels = size[0] * size[1] ; // * size[2];
PixelType * pixelData = new PixelType[ numberOfPixels ];
//PixelType *pixelData = realImage;
int totalNumofPixels = size[0]*size[1];
importFilter -> SetImportPointer(pixelData, totalNumofPixels, true );
importFilter->Update();
//return importFilter->GetOutput();
return 0;
}
--
More information about the Insight-users
mailing list