[Insight-users] Persisting a dynamically created image onto the
file system
Shlomo Kashani
shlomo_kashani at yahoo.com
Thu Jun 29 10:59:52 EDT 2006
Thank you both for your help.
I was able to modify and use image5.cxx to create a 2D impulse response
image using an ImportImageFilter and successfully write it using an image
writer (see the code below). However, a buffer is allocated using the new
keyword and then it is traversed using C style loops. On the other hand in
example image2.cxx a new image is created using:
typedef itk::Image<unsigned short,2> ImageType2D;
// create a new image
ImageType2D::Pointer smallImage = ImageType2D::New();
and then allocated using:
smallImage->Allocate();
In this case which is advantageous, I guess I can use an iterator instead of
C style loops but I still don?t know how to associate samllImage with an
image writer.
Thanks again,
Shlomo.
(comments removed for posting)
#include "itkImage.h"
#include "itkImportImageFilter.h"
#include "itkImageFileWriter.h"
int main(int argc, char * argv[])
{
if( argc < 4 )
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " outputImageFile sizeX sizeY " << std::endl;
return 1;
}
typedef unsigned char PixelType;
const unsigned int Dimension = 2;
// A 2D image type
typedef itk::Image< PixelType, Dimension > ImageType2D;
typedef itk::ImportImageFilter< PixelType, Dimension >
ImportFilterType2D;
ImportFilterType2D::Pointer importFilter = ImportFilterType2D::New();
ImportFilterType2D::SizeType size;
size[0] = atoi(argv[2]); // size along X
size[1] = atoi(argv[3]); // size along Y
ImportFilterType2D::IndexType start;
start.Fill( 0 );
ImportFilterType2D::RegionType region;
region.SetIndex( start );
region.SetSize( size );
importFilter->SetRegion( region );
double origin[ Dimension ];
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // Y coordinate
importFilter->SetOrigin( origin );
double spacing[ Dimension ];
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along Y direction
importFilter->SetSpacing( spacing );
// Allocate memory for the dynamically generated image
const unsigned int numberOfPixels = size[0] * size[1];
PixelType * localBuffer = new PixelType[ numberOfPixels ];
// Pointer alias
PixelType * it = localBuffer;
int midX=(size[0]/2);
int midY=(size[1]/2);
for(unsigned int y=0; y < size[1]; y++)
{
for(unsigned int x=0; x < size[0]; x++)
{
*it++ = ( (x==midX) && (y==midY) ) ? 255 : 0;
}
}
const bool importImageFilterWillOwnTheBuffer = true;
importFilter->SetImportPointer( localBuffer, numberOfPixels,
importImageFilterWillOwnTheBuffer );
typedef itk::ImageFileWriter< ImageType2D > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[1] );
writer->SetInput( importFilter->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & exp )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << exp << std::endl;
}
return 0;
}
--
View this message in context: http://www.nabble.com/Persisting-a-dynamically-created-image-onto-the-file-system-tf1850153.html#a5104334
Sent from the ITK - Users forum at Nabble.com.
More information about the Insight-users
mailing list