[英]Opencv row method has a strange behavior
I'm working with OpenCv Mat access and copy in (C++). 我正在使用OpenCv Mat访问和复制(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. 问题是
row(0)
始终返回0,并且使用row(1)...row(n)
访问Mat相对于at<>()
方法而言偏移为+1,这对我来说似乎很奇怪。 Am I missing something or is a known issue of OpenCv? 我是否缺少某些东西,或者是OpenCv的已知问题?
Look at Mat::zeros()
, the way you call it is rows = 100
cols = 1
. 看一下
Mat::zeros()
,您的调用方式是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. 当您使用
i = 0
调用values.at<float>(i, 1)
,您将访问第0行和第1行的元素,这当然超出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. 将
values.at<float>(i, 1)
更改为values.at<float>(i, 0)
并在Debug模式下运行构建,以供以后参考,其中OpenCV断言将捕获此类错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.