[ITK-users] Image to matrix and viceversa

Johnson, Hans J hans-johnson at uiowa.edu
Thu May 5 11:27:39 EDT 2016


Javier,

Please keep the conversation on the public mailing list.


This does not sound like you are having ITK issues per-se, but rather issues data reformatting.


Here is pseudo-code that I think would address your problem.



#include <itkImage.h>
#include <itkImageRegionConstIteratorWithIndex.h>
#include <itkImageRegionIteratorWithIndex.h>

itk::Array2D<double> MakeMatrix(itk::Image<std::vector<unsigned int>,3>::Pointer image, itk::Image<unsigned char,3>::Pointer mask)
{
  if( mask->GetLargestPossibleRegion() != image->GetLargestPossibleRegion() )
  {
    std::cerr << "ERROR regions must match" << std::endl;
  }
  itk::ImageRegionConstIteratorWithIndex<itk::Image<unsigned char,3> > maskIt(mask,mask->GetLargestPossibleRegion());
  itk::ImageRegionIteratorWithIndex<itk::Image<std::vector<unsigned int>,3> > imIt(image,image->GetLargestPossibleRegion());

  size_t numInMaskVoxels = 0;
  for(maskIt.GoToBegin(); !maskIt.IsAtEnd(); ++maskIt)
  {
    if(maskIt.Value() > 0)
    {
      ++numInMaskVoxels;
    }
  }

  imIt.GoToBegin();
  const size_t numElementsInPixel = imIt.Value().size();

  itk::Array2D<double> myMatrix(numInMaskVoxels,numElementsInPixel);
  size_t curr_row = 0;
  for(maskIt.GoToBegin(); !maskIt.IsAtEnd(); ++maskIt) {
    if (maskIt.Value() > 0) {
      const itk::Image<unsigned char,3>::IndexType & index = maskIt.GetIndex();
      const std::vector<unsigned int> & refPixel = image->GetPixel(index);
      size_t curr_col = 0;
      for(std::vector<unsigned int>::const_iterator vi = refPixel.begin(), refPixelEnd = refPixel.end();
          vi != refPixelEnd; ++vi )
      {
        myMatrix(curr_row,curr_col) = *vi;
        ++curr_row;
        ++curr_col;
      }
    }
  }
  return myMatrix;
}


Hans
--








On 5/5/16, 9:49 AM, "javij1 at gmail.com" <javij1 at gmail.com> wrote:

>
>Dear Johnson, Hans
>
>First of all, sorry for bother you again, but I cannot solve the problem I have and you are the only one that has answered my question. Please, I would appreciatte a lot if you can help me to solve the issue. I have the following problem:
>
>I have an image that has the dimensions 270x270x171x34. I want to convert this image into a Matrix of size Nx34, where N is the number of voxels inside a mask of size 270x270x171. The Matrix datatype is an Eigen Matrix class. In a classic "for loop" scheme the code to get what I want is
>
>for (int x = 0; x < image.width(); x++)
>{
>     for (int y = 0; y < image.height(); y++)
>     {
>         for (int z = 0; z < image.depth(); z++)
>         {
>             if (!mask(x, y, z))
>                 continue;
>
>             for (int c = 0; c < image.spectrum(); c++)
>                 Matrix(i, c) = image(x, y, z, c);
>             i++;
>        }
>    }
>}
>
>
>How can I do this in ITK?? I have read several ImageRegionIterator examples, but I do not find a clean and correct way to do it. I need to walk around my image and for those pixels that are inside the mask, store their 34 values in a row of the matrix.
>
>Thank you very much.
>Regards.
>
>Javier. 
>
>_____________________________________
>Sent from http://itk-insight-users.2283740.n2.nabble.com
>


More information about the Insight-users mailing list