k-jax
k-jax

Reputation: 23

C++ OpenCV mat.at gives access violation when using data

I'm using openCV 2.1 in a visual studio 2010 C++ dll to do matrix operations. The dll recieves arrays from a VB.NET program and loads them into matrices for some manipulation. However I cannot use the .at member on any cv::mat object without throwing an access violation exception. I thought it was because I was passing in the arrays but I cannot even run this:

Mat Rhat(2,1,CV_32FC1);
Rhat.at<double>(0,0) = 10;
Rhat.release();

If I remove the .at line then it runs fine. I had the whole thing done with C using CvMat types but it didn't like cvCreateMat and started working with the cv namespace instead. All my non opencv functions in the dll work fine so the problem is in my cv setup or something.
Can anyone help?

Upvotes: 2

Views: 3512

Answers (1)

alarouche
alarouche

Reputation: 441

The problem is that your created a matrix of float (32FC1) and you are trying to access it using double which causes an out of bound access.

You can either use float everywhere:

Mat Rhat(2,1,CV_32FC1);
Rhat.at<float>(0,0) = 10;
Rhat.release();

or double:

Mat Rhat(2,1,CV_64FC1);
Rhat.at<double>(0,0) = 10;
Rhat.release();

Upvotes: 8

Related Questions