Sorry, this content is no longer available

Ask Your Question
0

Need explanation for the following! thanks in advance.

asked Jul 28 '0

Jagadeesh1993 gravatar image

I have the following block of code in main function in C++. I am using openCV 4.1.1.

int a[ ] = { 1,2,3,4,5,6,7,8,9 };

Mat matrix(1, 9, CV_8SC1, &a);

cout<< matrix<<endl;< p="">

The expected output is:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

But the actual output is:

[ 1, 0, 0, 0, 2, 0, 0, 0, 3]

I am able to get the desired output by using the following statement!

Mat matrix(1, 9, CV_32SC1, &a);

Can someone please provide explanation for why CV_8SC1 is not giving the desired output, also provide references if possible. Thanks in advance.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
0

answered Jul 28 '0

berak gravatar image

on a 32bit system an int takes the same space as 4 bytes. so, if you set the type of the Mat header to CV_8S, it only considers the first 9 bytes in memory.

char a[ ] = { 1,2,3,4,5,6,7,8,9 };
Mat matrix(1, 9, CV_8SC1, &a);

(char array instead of int) would have been, what you wanted there.

in the end, you have to be very careful, using a Mat constructor with external data like above. apart from the type problem, this type of Mat is NOT refcounted, and once your initial data goes out of scope, your Mat is invalid ("dangling pointer")

Preview: (hide)
1

answered Jul 28 '0

mvuori gravatar image

8S is really a char, but you are feeding the Mat a buffer of ints - which in 32 bit program are 32Ss...

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jul 28 '0

Seen: 491 times

Last updated: Jul 28 '20