# Understanding cv::Mat

cv::Mat mat34(3,4, CV_32FC(3),cv::Scalar(20,30,40));

mat34.at<float>(3,3) = 1000;

std::cout << mat34.at<float>(3,3) << " and " << mat34.at<cv::Vec<float,3> >(3,3)[0] << std::endl;
std::cout << mat34 << std::endl;


Output

1000 and 0
[20, 30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40;
20, 30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40;
20, 30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40]


I dont understand the 3,3 part. Is the element 3,3 not direclty in the matrix? Was expecting to see 1000 somewhere in the entire matrix. Also, how can I access channels using cv::Vec? Since, I am learning I am not looking for alternative solutions, but just understanding how this works.

edit retag close merge delete

1

( 2017-08-21 18:20:50 -0500 )edit

Sort by ยป oldest newest most voted

I dont understand the 3,3 part. Is the element 3,3 not direclty in the matrix?

no, it isn't.

for a Mat(3,4,CV_32FC3) the last valid element is at (2,3), we count from 0 to N-1 in c++ ..

also, to access an element of a Mat filled with Vec3b, you have to use:

Vec3b & v = mat34.at<Vec3b>(2,3);
v[0] = .2f;

more

why isnt there any exception, when I am trying to access an index that is not accessible by the matrix? But things do not change even if I try to manipulate mat34.at<float>(2,3) = 1000; The resulting answer does not contain any 1000.

( 2017-08-22 02:25:58 -0500 )edit

If executable is in release there is no exception. In debug exception are available

( 2017-08-22 03:11:45 -0500 )edit

@infoclogged, basically, it is "undefined behaviour", anything might happen (or not)

( 2017-08-22 03:32:57 -0500 )edit

thnaks for the hint regarding the index. I have put my own answer for further clarification.

( 2017-08-22 05:10:15 -0500 )edit

Official site

GitHub

Wiki

Documentation