"Access violation" when assigning matrix values
Hello,
I am writing a program to estimate the fundamental matrix between two images, using OpenCV. This is a computer vision technique, but the error I am getting is something to do with memory in the program itself. I have copied the code exactly from this reliable source: http://books.google.co.uk/books?id=s...xample&f=false
Here is the code below:
Code:
int main()
{
int point_count = 100;
CvMat* points1;
CvMat* points2;
CvMat* status;
CvMat* fundamental_matrix;
points1 = cvCreateMat(1,point_count,CV_32FC2);
points2 = cvCreateMat(1,point_count,CV_32FC2);
status = cvCreateMat(1,point_count,CV_8UC1);
/* Fill the points here … */
for(int i = 0; i < point_count; i++ )
{
points1->data.db[i*2] = 2;
points1->data.db[i*2+1] = 4;
points2->data.db[i*2] = 5;
points2->data.db[i*2+1] = 3;
}
fundamental_matrix = cvCreateMat(3,3,CV_32FC1);
int fm_count = cvFindFundamentalMat(
points1,points2,fundamental_matrix,
CV_FM_RANSAC,1.0,0.99,status );
return 0;
}
However, when this is run, I get the following error:
"Unhandled exception at 0x0040122b in TestProgram.exe: 0xC0000005: Access violation writing location 0x0037d000."
This occurs when i = 54, at the line "points2->data.db[i*2] = 5;"
It seems to be suggesting that I don't have enough memory to make this assignment, but I have reserved enough. My "points2" matrix contains 100 elements, each of type CV_32FC2 (which represent two floating points), and thus the matrix should be able to hold 200 floating points.
Furthermore, this code is copied exactly from the source, which is a reliable text book. So, the code should run, and I am very confused as to why I am getting this error.
Thanks for any help :)
Re: "Access violation" when assigning matrix values
You are using the .db member of the union, which assumes that the underlying array is of doubles. However, CV_32FC2 assumes that each element is a 32-bit float----not a 64-bit double.
Re: "Access violation" when assigning matrix values
Hi,
Thanks - I managed to fix it by using cvmSet(), which seems to automatically convert to a float.
However, I am now having an issue with the "status" matrix. I want to print out the elements of the matrix, and so I use "cout << cvmGet(status, 0, i);", but it gives me the following runtime error:
"Debug Error! Program: ........MyProgram.exe The application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."
Here is the code:
Code:
int main()
{
CvMat* points1;
CvMat* points2;
CvMat* status;
CvMat* fundamental_matrix;
points1 = cvCreateMat(2, 8, CV_32FC1);
points2 = cvCreateMat(2, 8, CV_32FC1);
status = cvCreateMat(1, 8, CV_8UC1);
for (int i = 0; i < 8; i ++)
{
cvmSet(points1, 0, i, i);
cvmSet(points1, 1, i, i);
cvmSet(points2, 0, i, i);
cvmSet(points2, 1, i, i);
}
fundamental_matrix = cvCreateMat(3, 3, CV_32FC1);
int fm_count = cvFindFundamentalMat(points1, points2, fundamental_matrix, CV_FM_RANSAC, 1.0, 0.99, status);
cout << endl << endl << endl;
for (int i = 0; i < 8; i ++)
{
cout << cvmGet(status, 0, i);
}
return 0;
}
Any ideas??
Thanks :)
Re: "Access violation" when assigning matrix values
Quote:
Originally Posted by
karnavor
Hi,
Thanks - I managed to fix it by using cvmSet(), which seems to automatically convert to a float.
However, I am now having an issue with the "status" matrix. I want to print out the elements of the matrix, and so I use "cout << cvmGet(status, 0, i);", but it gives me the following runtime error:
"Debug Error! Program: ........MyProgram.exe The application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."
Use divide and conquer.
Remove these lines:
Code:
fundamental_matrix = cvCreateMat(3, 3, CV_32FC1);
int fm_count = cvFindFundamentalMat(points1, points2, fundamental_matrix, CV_FM_RANSAC, 1.0, 0.99, status);
Now what happens? If the error still occurs, then its something very basic, otherwise it's that function that was removed is the cause of the problem.
Regards,
Paul McKenzie