Reputation: 81
I have a problem accessing elements of a 5D Matrix in OpenCV. I create my Matrix using
int sizes[5] = { height_, width_, range_, range_, range_ };
Mat w_i_ = Mat(2 + channels, sizes, CV_16UC(channels), Scalar(0));
where channels = 3. Then I'm trying to access and modify the matrix elements using for loops:
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;
}
}
I am getting an assertion failed error produced by Mat::at() when I run the code. Specifically the message I get is:
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
I have searched the web but I can't seem to find any topics on 5D Matrices (similar topics proved of no help).
Thanks in advance
Upvotes: 1
Views: 266
Reputation: 14467
You initialize the zeta
variable and do not check its values.
Most likely you get an out-of-range value for zeta[0], zeta[1]
and zeta[2]
indices and thus the internal range checking in at()
function fails.
To prevent such crashes at least add some manual range checking before calling at():
for(int i = 0 ; i < 3 ; i++)
if(zeta[i] < 0 || zeta[i] >= _range)
continue;
Upvotes: 0