Ask Your Question
2

Understanding cv::Mat

asked 2017-08-21 17:24:46 -0600

infoclogged gravatar image

updated 2017-08-21 17:27:20 -0600

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 flag offensive close merge delete

Comments

1

did you read documentation

sturkmen gravatar imagesturkmen ( 2017-08-21 18:20:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-08-22 00:42:51 -0600

berak gravatar image

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;
edit flag offensive delete link more

Comments

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.

infoclogged gravatar imageinfoclogged ( 2017-08-22 02:25:58 -0600 )edit

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

LBerger gravatar imageLBerger ( 2017-08-22 03:11:45 -0600 )edit

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

berak gravatar imageberak ( 2017-08-22 03:32:57 -0600 )edit

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

infoclogged gravatar imageinfoclogged ( 2017-08-22 05:10:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-08-21 17:24:46 -0600

Seen: 1,077 times

Last updated: Aug 22 '17