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&lt;MovingImageType&gt; ConstIteratorType;</div>
<div><br></div><div>  ConstIteratorType<span class="Apple-tab-span" style="white-space: pre; ">        </span>imageout(resampler-&gt;GetOutput(), resampler-&gt;GetOutput()-&gt;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">&lt;<a href="mailto:bill.lorensen@gmail.com">bill.lorensen@gmail.com</a>&gt;</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&lt;<a href="mailto:gregthom99@yahoo.com">gregthom99@yahoo.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Hello all<br>
&gt;<br>
&gt;<br>
&gt; I am doing a small test with moving data from matlab - &gt; itk -&gt; back to<br>
&gt; matlab.<br>
&gt; I can&#39;t get code below to work properly. Is there something I am doing wrong<br>
&gt; ?<br>
&gt; This is not entirely itk but I believe the problem is more in the itk side<br>
&gt; than matlab<br>
&gt; so please anyone with some itk/matlab experience please have a look.<br>
&gt;<br>
&gt;<br>
&gt; I have this templated function :<br>
&gt;<br>
&gt; template &lt; class TImageType, class TPixelType&gt;<br>
&gt; int itkImage2carray( TImageType *img, TPixelType outData[] )<br>
&gt; {<br>
&gt;  typedef itk::ImageRegionIterator&lt;TImageType&gt; ItType;<br>
&gt;  ItType it( img, img-&gt;GetLargestPossibleRegion() );<br>
&gt;<br>
&gt;  int ctr = 0;<br>
&gt;  for (it = it.Begin(); !it.IsAtEnd(); ++it)<br>
&gt;  {<br>
&gt;     outData[ctr]=it.Get();<br>
&gt;     ctr++;<br>
&gt;  }<br>
&gt;<br>
&gt;  return 0;<br>
&gt; }<br>
&gt;<br>
&gt; and I use it like this:<br>
&gt;<br>
&gt;    typedef float PixelType;<br>
&gt;    const unsigned int Dimension = 3;<br>
&gt;<br>
&gt;    // before calling the function<br>
&gt;    typedef itk::Image&lt;PixelType, Dimension&gt;  ImageType;<br>
&gt;    ImageType::Pointer image = (ImageType *)mxArray2ITKImage(prhs[0]);<br>
&gt;    image-&gt;UnRegister(); // Decrement the reference count<br>
&gt;<br>
&gt;    ImageType::SizeType size;<br>
&gt;    size = image-&gt;GetLargestPossibleRegion().GetSize();<br>
&gt;    mwSize* dims;<br>
&gt;    dims[0]  = size[0] ;<br>
&gt;    dims[1]  = size[1] ;<br>
&gt;    dims[2]  = size[2] ;//assume 3D here !<br>
&gt;<br>
&gt;<br>
&gt;    //copy back to matlab<br>
&gt;    PixelType data[size[0]*size[1]*size[2]];<br>
&gt;    int res = itkImage2carray&lt;ImageType,PixelType&gt;( image, data);<br>
&gt;<br>
&gt;    plhs[0]  = mxCreateNumericArray(Dimension, dims, mxDOUBLE_CLASS,<br>
&gt; mxREAL);<br>
&gt;    memcpy((void *)(mxGetPr(plhs[0])), (void *)data, sizeof(data));<br>
&gt;<br>
&gt;<br>
&gt; This has behaved eratically , sometimes I got only half of the input image ,<br>
&gt; now, as it is, this only generates a seg fault in matlab.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; 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>

&gt; Sent from the ITK - Users mailing list archive at Nabble.com.<br>
&gt;<br>
&gt; _____________________________________<br>
&gt; Powered by <a href="http://www.kitware.com" target="_blank">www.kitware.com</a><br>
&gt;<br>
&gt; Visit other Kitware open-source projects at<br>
&gt; <a href="http://www.kitware.com/opensource/opensource.html" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
&gt;<br>
&gt; 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>
&gt;<br>
&gt; Follow this link to subscribe/unsubscribe:<br>
&gt; <a href="http://www.itk.org/mailman/listinfo/insight-users" target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
&gt;<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>