[Insight-users] what is wrong with this code ? : exporting itkImage

gregthom gregthom99 at yahoo.com
Mon Jun 22 18:22:38 EDT 2009


Hello all


I am doing a small test with moving data from matlab - > itk -> back to
matlab. 
I can't get code below to work properly. Is there something I am doing wrong
? 
This is not entirely itk but I believe the problem is more in the itk side
than matlab
so please anyone with some itk/matlab experience please have a look.


I have this templated function :

template < class TImageType, class TPixelType>
int itkImage2carray( TImageType *img, TPixelType outData[] )
{
 typedef itk::ImageRegionIterator<TImageType> ItType;
 ItType it( img, img->GetLargestPossibleRegion() );
 
 int ctr = 0;
 for (it = it.Begin(); !it.IsAtEnd(); ++it)
  {
     outData[ctr]=it.Get();
     ctr++;
  }

 return 0;
}

and I use it like this:

    typedef float PixelType; 
    const unsigned int Dimension = 3;    
     
    // before calling the function
    typedef itk::Image<PixelType, Dimension>  ImageType;
    ImageType::Pointer image = (ImageType *)mxArray2ITKImage(prhs[0]);
    image->UnRegister(); // Decrement the reference count
   
    ImageType::SizeType size;
    size = image->GetLargestPossibleRegion().GetSize();
    mwSize* dims;
    dims[0]  = size[0] ;
    dims[1]  = size[1] ; 
    dims[2]  = size[2] ;//assume 3D here !


    //copy back to matlab
    PixelType data[size[0]*size[1]*size[2]];
    int res = itkImage2carray<ImageType,PixelType>( image, data);
     
    plhs[0]  = mxCreateNumericArray(Dimension, dims, mxDOUBLE_CLASS,
mxREAL);
    memcpy((void *)(mxGetPr(plhs[0])), (void *)data, sizeof(data));


This has behaved eratically , sometimes I got only half of the input image ,
now, as it is, this only generates a seg fault in matlab.



-- 
View this message in context: http://www.nabble.com/what-is-wrong-with-this-code---%3A-exporting-itkImage-tp24156607p24156607.html
Sent from the ITK - Users mailing list archive at Nabble.com.



More information about the Insight-users mailing list