[Insight-users] Writing an image of Vectors
Luis Ibanez
luis . ibanez at kitware . com
Tue, 14 Oct 2003 11:26:18 -0400
Hi Bing,
The following code will give you an example of how to write
an image of CovariantVectors into a MetaImage file.
Regards,
Luis
------------ BEGIN OF EXAMPLE ----------------
#include "itkImage.h"
#include "itkCovariantVector.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
int main( int argc, char *argv[] )
{
const unsigned int Dimension = 3;
typedef itk::CovariantVector<float,Dimension> VectorPixelType;
typedef itk::Image< VectorPixelType, Dimension > VectorImageType;
typedef VectorImageType::Pointer VectorImagePointer;
VectorImagePointer vectorImage = VectorImageType::New();
VectorImageType::RegionType region;
VectorImageType::IndexType index;
VectorImageType::SizeType size;
size[0] = 100;
size[1] = 100;
size[2] = 100;
index.Fill( 0 );
region.SetIndex( index );
region.SetSize( size );
vectorImage->SetRegions( region );
vectorImage->Allocate();
typedef itk::ImageRegionIterator< VectorImageType > IteratorType;
VectorPixelType vector;
vector[0] = 13;
vector[1] = 17;
vector[2] = 19;
IteratorType it( vectorImage, region );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Set( vector );
++it;
}
typedef itk::ImageFileWriter< VectorImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("VectorImage.mha");
writer->SetInput( vectorImage );
try
{
writer->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
}
return 0;
}
------------ END OF EXAMPLE ----------------
-------------------------------
Bing Jian wrote:
> THanks Luis.
> I even cannot write an image of vectors. Trying to write a
> vector image gave following error at runtime:
>
> This application has requested the Runtime to terminate it in an
> unusual way.
> Please contact the application's support team for more information.
> Press any key to continue
>
> What I did is just as follows:
>
> typedef itk::ImageFileWriter < VectorImageType > WriterType;
> WriterType::Pointer writer = WriterType::New();
> writer->SetFileName(argv[2]);
> writer->SetInput(gradient->GetOutput());
> writer->Update();
>
>
>
>