Wajih
Wajih

Reputation: 791

Access violation for mat.at<short>(x,y)

For a simple vector that I have

Mat m = Mat(1, vectorLength, CV_16S, short(0));

I try to update a value with the following line

m.at<short>(0,0) = 10000*value;

But .at(0,0) keeps giving me access violation.

Is this a possible bug in opencv3 ?

Upvotes: 0

Views: 252

Answers (1)

Micka
Micka

Reputation: 20130

The error is probably in your mat construction (but is first recognized in .get, where the data you want to access isn't present):

use instead:

Mat m = Mat(1, vectorLength, CV_16S, cv::Scalar(0));

or

Mat m = Mat::zeros(1, vectorLength, CV_16S);

In your version it interprets short(0) as address where referenced data is assumed. So it probably uses this Mat constructor there: Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP) where void* data is short(0) instead of this constructor, which you probably wanted to call: Mat::Mat(int rows, int cols, int type, const Scalar& s)

But maybe I'm wrong, then you probably want to use this instead:

Mat m = Mat(1, vectorLength, CV_16S, yourVector.data());

in this case, the matrix doesn't allocate own memory but instead just links to the vector's data.

Upvotes: 1

Related Questions