我在 OpenCV 中访问 5D 矩阵的元素时遇到问题。我使用
创建我的矩阵int sizes[5] = { height_, width_, range_, range_, range_ };
Mat w_i_ = Mat(2 + channels, sizes, CV_16UC(channels), Scalar(0));
其中 channels = 3。然后我尝试使用 for 循环访问和修改矩阵元素:
for (UINT Y = 0; Y < height; ++Y) {
for (UINT X = 0; X < width; ++X) {
// a) Compute the homogeneous vector (wi,w)
Vec3b wi = image.at<Vec3b>(Y, X);
// b) Compute the downsampled coordinates
UINT y = round(Y / sigmaSpatial);
UINT x = round(X / sigmaSpatial);
Vec3b zeta = round( (image.at<Vec3b>(Y, X) - min) / sigmaRange);
// round() here is overloaded for vectors
// c) Update the downsampled S×R space
int idx[5] = { y, x, zeta[0], zeta[1], zeta[2] };
w_i_.at<Vec3b>(idx) = wi;
}
}
当我运行代码时,我收到 Mat::at() 产生的断言失败错误。具体来说,我收到的消息是:
OpenCV Error: Assertion failed (elemSize() == (((((DataType<_Tp>::type) & ((512 - 1) << 3)) >> 3) + 1) << ((((sizeof(size_t)/4+1)*16384|0x3a50) >> ((DataType<_Tp>::type) & ((1 << 3) - 1))*2) & 3))) in cv::Mat::at, file c:\opencv\build\include\opencv2\core\mat.inl.hpp, line 1003
我在网上搜索过,但似乎找不到任何关于 5D 矩阵的主题(类似的主题被证明没有帮助)。
提前致谢
最佳答案
您初始化了 zeta
变量并且不检查它的值。
您很可能得到 zeta[0]、zeta[1]
和 zeta[2]
索引的超出范围值,因此 中的内部范围检查>at()
函数失败。
为防止此类崩溃,至少在调用 at() 之前添加一些手动范围检查:
for(int i = 0 ; i < 3 ; i++)
if(zeta[i] < 0 || zeta[i] >= _range)
continue;
关于c++ - OpenCV:访问 5D 矩阵的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36874518/