[Insight-users] How to convert itk::image to itk::matrix?

Luis Ibanez luis.ibanez at kitware.com
Sun Feb 4 13:23:10 EST 2007


Hi Ali,

You can do this by using the vnl_matrix_ref class
which points to a user-allocated buffer, and using
the image->GetBufferePointer() as such input buffer.

The code will look like:


#include "itkImage.h"
#include "vnl_matrix_ref.h"


typedef itk::Image< float, 2 >   ImageType;
typedef vnl_matrix_ref< float >  MatrixType;


ImageType::Pointer image = reader->GetOutput();

const ImageType::RegionType region = image->GetBufferedRegion();
const ImageType::SizeType size = region.GetSize();

const unsigned int rows = size[0];
const unsigned int cols = size[0];

MatrixType matrix( rows, cols, image->GetBufferPointer() );


It is *your responsibility* to make sure that the
Matrix operation actually make sense in an image
space.  Many matrix operation *do not* make sense
in an image space. The association of images and
matrices is in most cases a bad habit acquired from
the use of Matlab where the matrix is the basic type.


In order to convert the output matrix back into an
ITK image you can use the itkImportImageFilter as
described in the ITK Software Guide:


    http://www.itk.org/ItkSoftwareGuide.pdf

Section 4.1.7 "Importing Image Data from a Buffer",
in pdf-pages 78-81.


and using the matrix method

               matrix.data_block()

in order to get access to the internal storage
buffer of the matrix.



   Regards,


      Luis


--------------
Ali - wrote:
> I wonder if it is possible to treat itk images as matrices so that
> one can apply matrix manipulations between them. In this case,
> it should be possible to convert itk images to vnl matrices, perform
> the manipulations and convert them back to images. How can this
> be done?
> 


More information about the Insight-users mailing list