Ask Your Question
0

cv::Mat assignment to Scalar

asked 2015-09-11 12:48:29 -0600

I am getting unexpected behavior for assigning the elements of a matrix--it seems the last element of the matrix is not assigned properly. The following is a minimum code example and output.

typedef unsigned short ValueType;
cv::Mat matrix;
matrix.create(5, 1, CV_16U);
matrix = 6;

for (unsigned int idx = 0; idx < matrix.rows; idx++)
{
    std::cout << idx << ": " << matrix.at<ValueType>(idx, 1) << std::endl;
}

Output:

0: 6
1: 6
2: 6
3: 6
4: 0

I am running this on WIndows 7, OpenCV version 2.4.10 and 2.4.11 compiled with Visual Studio 2012, 64bit.

I can't imagine something like this would be a problem in OpenCV, so I'm assuming there's something else going on here. Does anyone know what it might be? It does not matter if I replace the assignment line with any of these or other variations or even using different element types in the matrix.

matrix = cv::Scalar(6);
matrix.setTo(6);
edit retag flag offensive close merge delete

Comments

1

matrix.at<ValueType>(idx, 1) <-- already out of bounds. 0 is the only valid col index in your case..

please run a DEBUG build, it should throw a runtime ex. there.

berak gravatar imageberak ( 2015-09-12 00:59:38 -0600 )edit

Absolutely, you are right. I can't believe I did that, and so stubbornly, too. Thank you.

BrianEastwood gravatar imageBrianEastwood ( 2015-09-24 17:09:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-09-24 17:15:35 -0600

Berak answered this question by pointing out that I got the indices on my matrix wrong. The loop should read as follows, and then behaves as expected.

for (unsigned int idx = 0; idx < matrix.rows; idx++)
{
    std::cout << idx << ": " << matrix.at<ValueType>(idx, 0) << std::endl;
}

Thank you, Berak, for catching my silly mistake.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-09-11 12:48:29 -0600

Seen: 2,243 times

Last updated: Sep 24 '15