# Different order of elements while std::cout print a cv::Mat than to iterate with cv::Mat begin(), end()

**URL:** <https://forum.opencv.org/t/different-order-of-elements-while-std-cout-print-a-cv-mat-than-to-iterate-with-cv-mat-begin-end/5765>\
**Category:** C++\
**Tags:** mat, core\
**Created:** [October 21, 2021, 10:53am UTC](https://forum.opencv.org/t/different-order-of-elements-while-std-cout-print-a-cv-mat-than-to-iterate-with-cv-mat-begin-end/5765 "2021-10-21T10:53:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Israel\_Diaz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/israel_diaz/32/3533_2.png) [@Israel\_Diaz](https://forum.opencv.org/u/Israel_Diaz)\
**Post date:** [October 21, 2021, 10:53am UTC](https://forum.opencv.org/t/different-order-of-elements-while-std-cout-print-a-cv-mat-than-to-iterate-with-cv-mat-begin-end/5765/1 "2021-10-21T10:53:45Z")

</div>

I have the following code to obtain a 32FC3 cv::Mat

```auto
cv::Mat imageBGR = cv::imread(imageFilepath, cv::ImreadModes::IMREAD_COLOR);
cv::Mat resizedImageBGR, resizedImageRGB, resizedImage, preprocessedImage;
cv::resize(imageBGR, resizedImageBGR,
cv::Size(inputDims.at(2), inputDims.at(1)),
cv::InterpolationFlags::INTER_CUBIC);
cv::cvtColor(resizedImageBGR, resizedImageRGB, cv::ColorConversionCodes::COLOR_BGR2RGB);
resizedImageRGB.convertTo(preprocessedImage, CV_32FC3);

```

Then if I print the **preprocessedImage** →

```auto
std::cout << preprocessedImage.cols * preprocessedImage.rows * preprocessedImage.channels() << " " << preprocessedImage << std::endl;`

147456 [196, 211, 172, 217, 227, 189, 214, 224, 184, 216, 226, 187, 212, 222, 184 .....

```

Now I use that matrix to fill a vector of floats…

```auto
std::vector<float> inputTensorValues(inputTensorSize);

inputTensorValues.assign(preprocessedImage.begin<float>(),
 
preprocessedImage.end<float>());

```

And then I want to print the order of the vector…

```auto
std::cout << "Tensor = " << std::endl;

for (auto i: inputTensorValues) 
    std::cout << i << ' ';
 
std::cout << "End Tensor" << std::endl;

```

```auto

I obtain this order:

```

Tensor =  
196 217 214 216 212 182 170 182 232 …

```auto

Which is different than the printed Mat.
So how can I obtain the order is printed by std::cout in the vector??
```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [October 21, 2021, 12:19pm UTC](https://forum.opencv.org/t/different-order-of-elements-while-std-cout-print-a-cv-mat-than-to-iterate-with-cv-mat-begin-end/5765/2 "2021-10-21T12:19:09Z")

</div>

> [@Israel\_Diaz](#):
>
> `std::vector<float>`

different memory layout than `Mat_<Vec3f>` , your code copies only the 1st elem from each Vec3f (it’s NOT a matter of printing !)

```auto
std::vector<Vec3f> inputTensorValues(inputTensorSize);
inputTensorValues.assign(preprocessedImage.begin<Vec3f>(),
preprocessedImage.end<Vec3f>());

```

would work as expected
