Ask Your Question
0

cvGetCol(), cvGetRow(), cvGetDiag(), cvGetSubRect()

asked 2013-02-19 23:59:53 -0600

Almaz gravatar image

updated 2013-02-20 00:16:25 -0600

My code:

float a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
CvMat b = cvMat( 3, 3, CV_32FC1, a );
float c[] = { 6, 7, 8, 9 };
CvMat d = cvMat( 2, 2, CV_32FC1, c );
CvMat* e = cvGetSubRect( &b, &d, cvRect( 0, 0, 1, 1 ) );
cout << *( e->data.fl + 0 ) << "   " << *( e->data.fl + 1 ) << "   " << *( e->data.fl + 2 ) << "   " << *( e->data.fl + 3 ) << "\n";

The result: 1 2 3 4

But must be: 1 2 4 5

What am I doing wrong?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-02-20 02:03:37 -0600

rics gravatar image

When you create CvMat* e with cvGetSubRect you only create a matrix header which means matrix b is not copied and e points to the memory location of b. Your dereferences in cout point to the memory locations following the starting memory position of b.

Your expected result can be achieved like this (I assume older OpenCV version): CV_MAT_ELEM(e, float, 0, n) with n=0,1,2,3.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-19 23:59:53 -0600

Seen: 2,429 times

Last updated: Feb 20 '13