This code works for me:<div><br></div><div><div> const mwSize* <span class="Apple-tab-span" style="white-space: pre; ">        </span>dimsFix = mxGetDimensions(prhs[0]);</div><div><span class="Apple-tab-span" style="white-space: pre; ">        </span></div>
<div> mxArray* mvar = mxCreateNumericArray(Dimension,dimsFix,mxSINGLE_CLASS,mxREAL);</div><div> float* data = (float*)mxGetData(mvar);</div><div><br></div><div> typedef itk::ImageRegionConstIterator<MovingImageType> ConstIteratorType;</div>
<div><br></div><div> ConstIteratorType<span class="Apple-tab-span" style="white-space: pre; ">        </span>imageout(resampler->GetOutput(), resampler->GetOutput()->GetRequestedRegion());</div><div> imageout.GoToBegin();</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space: pre; ">        </span>for (int i = 0; !imageout.IsAtEnd(); ++i,++imageout)</div><div><span class="Apple-tab-span" style="white-space: pre; ">        </span>{ </div>
<div><span class="Apple-tab-span" style="white-space: pre; ">                </span>data[i] = imageout.Get();</div><div><span class="Apple-tab-span" style="white-space: pre; ">        </span>}</div><div><br></div><div> plhs[0] = mvar;</div><div>
<br></div><div>Hope this helps.</div><br><div class="gmail_quote">2009/6/23 Bill Lorensen <span dir="ltr"><<a href="mailto:bill.lorensen@gmail.com">bill.lorensen@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
You should never UnRegister an itk smart pointer. It will UnRegister<br>
itself when it leaves scope.<br>
<div><div></div><div class="h5"><br>
<br>
On Mon, Jun 22, 2009 at 6:22 PM, gregthom<<a href="mailto:gregthom99@yahoo.com">gregthom99@yahoo.com</a>> wrote:<br>
><br>
> Hello all<br>
><br>
><br>
> I am doing a small test with moving data from matlab - > itk -> back to<br>
> matlab.<br>
> I can't get code below to work properly. Is there something I am doing wrong<br>
> ?<br>
> This is not entirely itk but I believe the problem is more in the itk side<br>
> than matlab<br>
> so please anyone with some itk/matlab experience please have a look.<br>
><br>
><br>
> I have this templated function :<br>
><br>
> template < class TImageType, class TPixelType><br>
> int itkImage2carray( TImageType *img, TPixelType outData[] )<br>
> {<br>
> typedef itk::ImageRegionIterator<TImageType> ItType;<br>
> ItType it( img, img->GetLargestPossibleRegion() );<br>
><br>
> int ctr = 0;<br>
> for (it = it.Begin(); !it.IsAtEnd(); ++it)<br>
> {<br>
> outData[ctr]=it.Get();<br>
> ctr++;<br>
> }<br>
><br>
> return 0;<br>
> }<br>
><br>
> and I use it like this:<br>
><br>
> typedef float PixelType;<br>
> const unsigned int Dimension = 3;<br>
><br>
> // before calling the function<br>
> typedef itk::Image<PixelType, Dimension> ImageType;<br>
> ImageType::Pointer image = (ImageType *)mxArray2ITKImage(prhs[0]);<br>
> image->UnRegister(); // Decrement the reference count<br>
><br>
> ImageType::SizeType size;<br>
> size = image->GetLargestPossibleRegion().GetSize();<br>
> mwSize* dims;<br>
> dims[0] = size[0] ;<br>
> dims[1] = size[1] ;<br>
> dims[2] = size[2] ;//assume 3D here !<br>
><br>
><br>
> //copy back to matlab<br>
> PixelType data[size[0]*size[1]*size[2]];<br>
> int res = itkImage2carray<ImageType,PixelType>( image, data);<br>
><br>
> plhs[0] = mxCreateNumericArray(Dimension, dims, mxDOUBLE_CLASS,<br>
> mxREAL);<br>
> memcpy((void *)(mxGetPr(plhs[0])), (void *)data, sizeof(data));<br>
><br>
><br>
> This has behaved eratically , sometimes I got only half of the input image ,<br>
> now, as it is, this only generates a seg fault in matlab.<br>
><br>
><br>
><br>
> --<br>
> View this message in context: <a href="http://www.nabble.com/what-is-wrong-with-this-code---%3A-exporting-itkImage-tp24156607p24156607.html" target="_blank">http://www.nabble.com/what-is-wrong-with-this-code---%3A-exporting-itkImage-tp24156607p24156607.html</a><br>
> Sent from the ITK - Users mailing list archive at Nabble.com.<br>
><br>
> _____________________________________<br>
> Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
><br>
> Visit other Kitware open-source projects at<br>
> <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
><br>
> Please keep messages on-topic and check the ITK FAQ at: <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
><br>
> Follow this link to subscribe/unsubscribe:<br>
> <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
><br>
_____________________________________<br>
Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at: <a href="http://www.itk.org/Wiki/ITK_FAQ" target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
</div></div></blockquote></div><br></div>