# How to transfer an image buffer to matlab image matrix

**URL:** <https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568>\
**Category:** Beginner Questions\
**Created:** [December 13, 2022, 11:50am UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568 "2022-12-13T11:50:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)\
**Post date:** [December 13, 2022, 11:50am UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568/1 "2022-12-13T11:50:25Z")

</div>

hello, i want to retrieve image data from itk image, and load it in matlab.  
transfer the data pixel by pixel is too slow, and there are no direct function like mxArray::new\_from\_buffer can do it, so i have an idea that pass the whole buffer as a 1\*N array to matlab, and reshape the matrix to get a correct image.  
Unfortunately the image is not correct as i expected,  
the original image is 20202 \* 12133 \* 3:  
 ![image](https://discourse.itk.org/uploads/default/original/2X/b/b160a1fcd4f1b1b507ea5c86d7c6055921cad196.jpeg)

get image data in itk cpp

```auto
itk::Image<PixelType, Dimension>::SizeType size = resampleF->GetOutput()->GetLargestPossibleRegion().GetSize();
void * dataPtr = resampleF->GetOutput()->GetPixelContainer()->GetBufferPointer();
long long unsigned int buffer_len = 3 * size[0] * size[1];
mwArray file_name(outFilename.c_str());
mwArray mwWidth(1, 1, mxUINT16_CLASS);
mwArray mwHeight(1, 1, mxUINT16_CLASS);
mwSize mdim = buffer_len;
mwArray mdisp_image(1, mdim, mxUINT8_CLASS, mxREAL);
mdisp_image.SetData((mxUint8*)dataPtr, mdim);
mwWidth.SetData(&size[0], 1);
mwHeight.SetData(&size[1], 1);
matlabProcessFuncFromDLL(mdisp_image, mwWidth, mwHeight, file_name);

```

process the image data in matlab matlabProcessFuncFromDLL  
`reshape(imgdata, [width, height, 3]) `  
results like this

 ![image](https://discourse.itk.org/uploads/default/original/2X/9/9db7dfcffb980d2dcb1d71c7a9cae8a2552890ae.jpeg)

`reshape(imgdata, [height, width, 3])`  
results

 ![image](https://discourse.itk.org/uploads/default/original/2X/3/392f295d88d285509f8fb24e79b2c03556a918e1.jpeg)

reshape and transpose it

```auto
reshape(imgdata, [width, height, 3])
imgdata= reshape(imgdata, [width, height, 3]);
T=affine2d([0 1 0;1 0 0;0 0 1]);
new_imgdata=imwarp(imgdata,T);

```

result:

 ![image](https://discourse.itk.org/uploads/default/original/2X/8/812d84569dcc19580ccd296613ebbd25c800e78a.jpeg)  
the image data return by GetBufferPointer structure is like this ryt?  
 ![image](https://discourse.itk.org/uploads/default/original/2X/e/e5c7496e80f59a6843c3feabc5ae1c548d4bd2a0.png)  
total size is w \* h \* 3  
is the way trans 1\*N array to matlab and reshape the matrix to get a matlab image feasible, or something i understand is wrong. does Mr @matt.mccormick have any idea about this, as i notice you have a [repo](https://github.com/thewtex/matlab-itk-import) related to it

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 13, 2022, 3:56pm UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568/2 "2022-12-13T15:56:33Z")

</div>

In ITK, channel is the fastest varying index, `rgbrgbrgb...rgb`. You have drawn channel as slowest varying `rrr...rggg...gbbb...b`. But your approach of reshaping sounds right.

---

<div class="post-metadata">

**Author:** ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)\
**Post date:** [December 14, 2022, 2:36am UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568/3 "2022-12-14T02:36:09Z")

</div>

thanks for reply.  
change the fast varying index to slow varying index, the output is right!  
full code here:

```auto
chan_r = imgdata(1:3:end);
chan_g = imgdata(2:3:end);
chan_b = imgdata(3:3:end);
imgdata_combine = [chan_r, chan_g, chan_b];
mid_imgdata = reshape(imgdata_combine, [width, height, 3]);
T=affine2d([0 1 0;1 0 0;0 0 1]);
new_imgdata=imwarp(mid_imgdata,T);

```

---

<div class="post-metadata">

**Author:** ![allen](https://discourse.itk.org/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@allen](https://discourse.itk.org/u/allen)\
**Post date:** [December 17, 2022, 9:30am UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568/4 "2022-12-17T09:30:28Z")

</div>

channel sequence from rgbrgbrgb…rgb to rrr…rggg…gbbb…b is called convert from HWC to CHW ryt,  
what if i want to do this in pure itk,  
i use VectorIndexSelectionCastImageFilter first, then compose them by ComposeImageFilter like this [post](https://discourse.itk.org/t/how-to-get-the-0-th-component-for-vector-image/2759/2) said, will i get a CHW image, or get the same HWC image just like split the channels before

```auto
// PixelType comes from completeMontage template 
// template <unsigned Dimension,
// typename PixelType,
// typename AccumulatePixelType>
using ScalarPixelType = typename itk::NumericTraits<PixelType>::ValueType;
using ScalarImageType = itk::Image<ScalarPixelType, Dimension>;
using OriginalImageType = itk::Image<PixelType, Dimension>;
using ImageAdaptorType = itk::VectorIndexSelectionCastImageFilter<OriginalImageType, ScalarImageType>;
using ComposeType = itk::ComposeImageFilter<ScalarImageType, OriginalImageType>;
typename ImageAdaptorType::Pointer cAdaptor = ImageAdaptorType::New();
cAdaptor->SetInput(resampleF->GetOutput());

std::vector<typename ScalarImageType::Pointer> zImages(PixelType::Length);
for (unsigned c = 0; c < PixelType::Length; c++)
{
  cAdaptor->SetIndex(c);
  zImages[c] = cAdaptor->GetOutput();
  zImages[c]->DisconnectPipeline();
}

// compose an RGB(A) image from the filtered component images
typename ComposeType::Pointer compose = ComposeType::New();
for (unsigned c = 0; c < PixelType::Length; c++)
{
  compose->SetInput(c, zImages[c]);
}
compose->Update();

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 18, 2022, 4:23pm UTC](https://discourse.itk.org/t/how-to-transfer-an-image-buffer-to-matlab-image-matrix/5568/5 "2022-12-18T16:23:15Z")

</div>

I think you will get the same image as before. Maybe you should combine them using [tile image filter](https://itk.org/Doxygen/html/classitk_1_1TileImageFilter.html) instead of ComposeImageFilter?
