[Insight-users] How to convert 3d image into 1D vector array......

Iván Macía imacia at vicomtech.org
Thu May 28 05:38:13 EDT 2009


Hi Victor,

 

This is inefficient in ITK since GetPixel() provides random access to
pixels. You should do this with image iterators. 

 

Simple example to copy pixels from one image to the other:

 

itk::ImageRegionConstIterator<ImageType> it( image,
image->GetRequestionRegion() ); // const version is read only

itk::ImageRegionIterator<ImageType> oit( outputImage,
image->GetRequestionRegion() ); // make sure region is the same and it is in
memory

 

it.GoToBegin();

oit.GoToBegin();

 

while( !it.IsAtEnd() )

{

  oit.Set( it.Get() ); // copy pixel to output image

  ++it;

  ++oit;

}

 

or alternatively

 

for( it.GoToBegin(), oit.GoToBegin(); !it.IsAtEnd(); ++it, ++oit )

  oit.Set( it.Get() ); // do sth with ret value

 

All this is in the ITK Software Guide

 

Hope that helps

 

Iván

 

 

 

 

De: insight-users-bounces at itk.org [mailto:insight-users-bounces at itk.org] En
nombre de Víctor Rodrigo
Enviado el: miércoles, 27 de mayo de 2009 20:39
Para: insight-users at itk.org
Asunto: Re: [Insight-users] How to convert 3d image into 1D vector
array......

 

Hello.

I don't know if is the best option, but you can do something like this:


ImageType::SizeType size=image->GetLargestPossibleRegion().GetSize();
ImageType::IndexType index;
int i=0;
int numVox=size[0]*size[1]*size[2];
PixelType* img=new PixelType[numVox];
for(index[2]=0;index[2]<size[2];index[2]++) {
     for(index[1]=0;index[1]<size[1];index[1]++) {
           for(index[0]=0;index[0]<size[0];index[0]++) {
                img[i++]=image->GetPixel(index);
           }
      }
}



 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090528/97161d92/attachment-0001.htm>


More information about the Insight-users mailing list