# Can't extract elements from a Mat object into an int array (C++)

**URL:** <https://forum.opencv.org/t/cant-extract-elements-from-a-mat-object-into-an-int-array-c/12244>\
**Category:** C++\
**Tags:** core, programming\
**Created:** [March 6, 2023, 4:07am UTC](https://forum.opencv.org/t/cant-extract-elements-from-a-mat-object-into-an-int-array-c/12244 "2023-03-06T04:07:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_noodler](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/mr_noodler/32/7307_2.png) [@mr\_noodler](https://forum.opencv.org/u/mr_noodler)\
**Post date:** [March 6, 2023, 4:07am UTC](https://forum.opencv.org/t/cant-extract-elements-from-a-mat-object-into-an-int-array-c/12244/1 "2023-03-06T04:07:01Z")

</div>

Hello,  
I’m new to OpenCV, and I’m trying to extract the int values of a CV\_8U type (1 channel) Mat object. I can extract one value at a time, but if I put it into a loop, the program aborts in the console at runtime.

I’d like to have an int array because I want to convert the image into ASCII art, Please see below code? Is the console running out of memory or something?

Thanks in advance for the help.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f0776b14e26b6653152a7b1060f11176c62d5e62.png)

---

<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:** [March 6, 2023, 7:26am UTC](https://forum.opencv.org/t/cant-extract-elements-from-a-mat-object-into-an-int-array-c/12244/2 "2023-03-06T07:26:28Z")

</div>

please, NO useless screenshots of code or errors here. replace with TEXT, thank you.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [March 6, 2023, 9:15am UTC](https://forum.opencv.org/t/cant-extract-elements-from-a-mat-object-into-an-int-array-c/12244/3 "2023-03-06T09:15:26Z")

</div>

you messed up the indices.

(i,j) ~ (y,x)  
(x,y) ~ (j,i)

cv::Mat takes (i,j) indexing, and so does the C array.

please learn about “row-major” storage. that’s the standard, unless you’re Fortran or OpenGL.

storage and iteration orders matter. if you do it wrong, the CPU cache becomes useless.

don’t just use `[x][y]` indexing into a C array because it “looks good”. it goes against convention, so to everyone else it’ll look wrong.

use `int numbVersion[480][640]` and index with `[i][j]`

allocating the C array is also pointless. you have the same data in the `cv::Mat`.

for “ascii art”, you should be allocating something smaller, if a “pixel” should be one character.

you should use `cv::resize()` to downsample the input image. use appropriate fx, fy (characters aren’t square) and `INTER_AREA` mode.
