[Community] [Insight-users] Cast itkImage to Matlab emxArray
Bradley Lowekamp
brad at lowekamp.net
Fri Oct 18 13:07:26 EDT 2013
Glad to hear that it worked.
Looking at you code one more things to consider or be careful of is the case when ITK's Image BufferedRegion is not the same size as the LargestPossibleRegion. As your code does not contain any allocation, I'd hope that you have the check some place else.
Brad
On Oct 18, 2013, at 12:04 PM, Luca Tersi <lucatersi at gmail.com> wrote:
> It works like a charm.
> Thank you
> Luca
>
>
> ---
> ----------------------------------------------------------------------------------------------------------
>
>
> On 18 October 2013 17:36, Bradley Lowekamp <brad at lowekamp.net> wrote:
> Hello,
>
> The GetBufferPointer is a method, and I would try to avoid that in a tight loop.
>
> Here is a recent implementation to efficiently copy ITK regions:
> https://github.com/Kitware/ITK/blob/master/Modules/Core/Common/include/itkImageAlgorithm.hxx#L137
>
> We have found that std::copy is a very robust and efficient way to copy the buffers with modern compilers. It can even work when a conversion is needed.
>
> If you have validated the buffer is continuous and is the correct type and size. I would recommend just getting the raw buffer pointer and using std::copy. It may be up to 10-100X faster than your implementation below.
>
>
> On Oct 18, 2013, at 11:28 AM, Luca Tersi <lucatersi at gmail.com> wrote:
>
>> Hi,
>>
>> I've included some algorithms developed with Matlab in my C++ software. The Matlab algorithms were translated to C++ using Matlab Coder.
>> I've some images that have to be passed back and forth from the itk pipeline to the Matlab algorithm that work with emxArray data type.
>> I've made the following methods in order to copy the data, but is there any better and faster method? Moreover, is it better to use iterators or access the buffer directly?
>>
>> void ImageProcessing::CopyItkToMatlabImage(ImageProcessing::RealImageType::Pointer itkI, emxArray_real_T *matlabI)
>> {
>> RealImageType::SizeType size = itkI->GetLargestPossibleRegion().GetSize();
>>
>> for (int ii=0; ii < size[0]; ++ii)
>> for (int jj=0; jj < size[1]; ++jj)
>> {
>> matlabI->data[ii*size[0]+jj] = (real_T)itkI->GetBufferPointer()[ii*size[0]+jj];
>> }
>> }
>>
>>
>>
>> void ImageProcessing::CopyMatlabToItkImage(emxArray_real_T * matlabI, ImageProcessing::RealImageType::Pointer itkI)
>> {
>>
>> int32_T *size = matlabI->size;
>> RealIteratorType out ( itkI, itkI->GetLargestPossibleRegion() );
>> out.GoToBegin();
>>
>> for (int ii=0; ii < size[0]; ++ii)
>> for (int jj=0; jj < size[1]; ++jj)
>> {
>> out.Set(matlabI->data[ii*size[0]+jj]);
>> ++out;
>> }
>> }
>>
>> Thanks a lot
>>
>> Luca
>>
>> ---
>> ----------------------------------------------------------------------------------------------------------
>> _____________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Kitware offers ITK Training Courses, for more information visit:
>> http://www.kitware.com/products/protraining.php
>>
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20131018/28409795/attachment.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php
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 Community
mailing list