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

Bill Lorensen bill.lorensen at gmail.com
Mon Jun 22 18:49:46 EDT 2009


You should never UnRegister an itk smart pointer. It will UnRegister
itself when it leaves scope.


On Mon, Jun 22, 2009 at 6:22 PM, gregthom<gregthom99 at yahoo.com> wrote:
>
> 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.
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ITK FAQ at: http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>


More information about the Insight-users mailing list