运行时间错误使用CV :: Mat :: AT分配CV :: MAT元素值
在以下代码中,我想为循环中垫变量的元素分配一个值。我在下面遇到运行时错误。
pair<Mat, Mat> meshgrid(vector<int> x, vector<int> y) {
int sx = (int)x.size();
int sy = (int)y.size();
Mat xmat = Mat::ones(sy, sx, CV_16U);
Mat ymat = Mat::ones(sy, sy, CV_16U);
for (int i = 0; i < sx; i++) {
for (int j = 0; j < sy; j++) {
xmat.at<int>(i, j) = j; // <------- here is place of error.
cout << j << "\t";
}
cout << endl;
}
for (int i = 0; i < sx; i++) {
for (int j = 0; j < sy; j++) {
ymat.at<int>(i, j) = i; // <------- here is place of error.
cout << j << "\t";
}
cout << endl;
}
return make_pair(xmat, ymat);
}
调试时的这张照片;
这是我收到的运行时间错误:
OpenCV(...) Error: Assertion failed
(((((sizeof(size_t)<<28)|0x8442211) >> ((traits::Depth<_Tp>::value) &
((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file
...\include\opencv2\core\mat.inl.hpp, line 1108
谢谢您的回答。
In the following code I would like to assign a values to elements of a Mat variable in a loop. I get the runtime error below.
pair<Mat, Mat> meshgrid(vector<int> x, vector<int> y) {
int sx = (int)x.size();
int sy = (int)y.size();
Mat xmat = Mat::ones(sy, sx, CV_16U);
Mat ymat = Mat::ones(sy, sy, CV_16U);
for (int i = 0; i < sx; i++) {
for (int j = 0; j < sy; j++) {
xmat.at<int>(i, j) = j; // <------- here is place of error.
cout << j << "\t";
}
cout << endl;
}
for (int i = 0; i < sx; i++) {
for (int j = 0; j < sy; j++) {
ymat.at<int>(i, j) = i; // <------- here is place of error.
cout << j << "\t";
}
cout << endl;
}
return make_pair(xmat, ymat);
}
This picture when debuging;
This is the run time error I get:
OpenCV(...) Error: Assertion failed
(((((sizeof(size_t)<<28)|0x8442211) >> ((traits::Depth<_Tp>::value) &
((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file
...\include\opencv2\core\mat.inl.hpp, line 1108
Thank you for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您打算生成类似于 /code> 和 matlab
matlab
。meshgrid
meshgrid您的代码中有几个错误:
cv :: mat
使用类型CV_16U初始化(即16位无符号值),但是当您使用 at 访问元素时,您会使用int
(为32位签名)。您应该将其更改为
at&lt; unsigned Short&gt;
(或将cv :: mat
的类型更改为32位签名 -cv_32s
)。cv :: mat
带错误的大小:xmat
的大小为(sy,sx)
,但是ymat
的大小为(SY,SY)
。行
,cols
,循环索引
IROW
,ICOL
。x
和y
向量(而不是索引)中的值。请参阅下面的更新代码(以及有关更改的下面的注释):
输出:
一些注释:
cv :: mat
's中设置的值。但是至少现在您的代码不会崩溃。您可以根据需要更改分配的值。at
访问cv :: mat
一个接一个的元素效率非常低,因为at
包含每个访问的一些验证。使用
cv :: mat
方法ptr
,它为您提供了一排数据的指针。然后,您可以使用此指针来更有效地穿越该行 - 请参见上文cv :: mat
一排接头(而不是列列)更有效。这会导致您访问连续的内存,并减少缓存失误的数量。meshgrid
函数只会创建矩阵,那就更好了。如果需要,将它们打印在外面。cv :: mat
s初始化,因为之后立即为所有元素设置值。x
和y
中,通过const refernce传递给该函数。它更有效(避免复制),还迫使编译器验证向量未经修改。从类似的原因中,我也推荐使用命名空间CV 避免
。
I assume you meant to generate output similar to
numpy.meshgrid
, and Matlabmeshgrid
.There are several errors in your code:
cv::Mat
is initialized with type CV_16U (i.e. 16 bit unsigned value), but when you access the elements withat
you useint
(which is 32bit signed).You should change it to
at<unsigned short>
(or change the type of thecv::Mat
to 32 bit signed -CV_32S
).cv::Mat
with wrong sizes:xmat
has size of(sy, sx)
, butymat
has size of(sy, sy)
.rows
,cols
,and the loop indices to
iRow
,iCol
.x
andy
vectors (not the indices).See updated code below (and the notes following it regarding the changes):
Output:
Some notes:
cv::Mat
's. But at least now you have code that does not crash. You can change the assigned values as you wish.at
to accesscv::Mat
elements one after the other is very inefficient, becauseat
contains some validations for every access.It's a lot more efficient to use the
cv::Mat
methodptr
, that gives you a pointer to the data of a row. Then you can use this pointer to traverse the row more efficiently - see abovecv::Mat
one row after another (and not column by column). This causes you to access continous memory, and decrease the number of cache misses.meshgrid
function will only create the matrices. Print them outside if you need.cv::Mat
s to ones, because immediatly afterwards we set the values for all elements.x
andy
are passed to the function by const refernce. It is more efficient (avoid copy) and also forces the compiler to verify the vectors are not modified.using namespace std
- see here Why is "using namespace std;" considered bad practice?.From similar reasons I recomend to avoid
using namespace cv
as well.