I'm working with OpenCv Mat access and copy in (C++). Considering the following example:
cv::Mat values = cv::Mat::zeros(100, 1, CV_32FC1);
for (int i = 0; i < 100; i++) {
values.at<float>(i, 1) = 10 + i;
}
cout<<values.at<float>(0, 1)<<endl; // prints 10
cout<<values.at<float>(1, 1)<<endl; // prints 11
cout<<values.row(0) <<endl; // prints 0
cout<<values.row(1)<<endl; // prints 10
cout<<values.row(2)<<endl; // prints 11
The problem is that row(0)
always returns 0 and accessing the Mat with row(1)...row(n)
has an offset of +1 with respect to the method at<>()
which looks strange to me. Am I missing something or is a known issue of OpenCv?
Look at Mat::zeros()
, the way you call it is rows = 100
cols = 1
. When you call values.at<float>(i, 1)
with i = 0
you are accessing the element at row 0 and col 1, which of course is out of bounds of your Mat.
Change values.at<float>(i, 1)
to values.at<float>(i, 0)
and for future reference run your builds in Debug mode where OpenCV assertions will catch your errors like this one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.