need help with matrices

Hi guys

I need some help with accessing matrices elements.

I am doing the mahalanobis distance and am writing my own script as the mahalanobis distance function gives me an unhandled exception error.

so i first have to multiple the difference between mean to the invcovariance matrix.

this is the code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double mahadistance(Mat invcov, Scalar meanvec, Vec3b patternvec)
{
	double distmeasure=0;
	Scalar x1,x2;
	
	//mahalanobis distance is equal to (x-mean)^T*inv(cov)*(x-mean)
	x1[0]=(patternvec[0]-meanvec[0]);
	x1[1]=(patternvec[1]-meanvec[1]);
	x1[2]=(patternvec[2]-meanvec[2]);
	
	x2[0]=(x1[0]*invcov.at<double>(1,1)+x1[1]*invcov.at<double>(2,1)+x1[2]*invcov.at<double>(3,1));
	x2[1]=(x1[0]*invcov.at<double>(1,2)+x1[1]*invcov.at<double>(2,2)+x1[2]*invcov.at<double>(3,2));
	x2[2]=(x1[0]*invcov.at<double>(1,3)+x1[1]*invcov.at<double>(2,3)+x1[2]*invcov.at<double>(3,3));

	distmeasure=((x2[0]*x1[0])+(x2[1]*x1[1])+(x2[2]*x1[2]));
	return distmeasure;
}


it gets up to x2[0] = (x1[0] * invcov.at<double>(1,1).....
and it crashes with an unhandled exception.

Its really driving me crazy to do this simple thing. But i am soo stuck with matrix operations it just crashes.

i tried to loop through the matrix elements the long instead and store in a multidimensional array i.e. double matrx[3][3]; but the value inside the double array was -1.#QNAN.

thanks
thanks
Last edited on
My guess is that you go out of bounds on the indices for some container. The likely culprit is invcov, but I would need the definitions for Scalar, Vec3b, and Mat, in order to be sure. Try to go from 0 to 2 instead of from 1 to 3
That was it, it had to be between 0 to 2 one more thing with the inverse of a covariance matrix.

e.g. covmatrix = 0.2 0.1 0.9
0.02 1.2 3.9
1.0 2.2 1.8

doing covmatrix.inv() all the elements are -1.#QNAN

must be something in the inv function which is prebuilt function for Mat
Topic archived. No new replies allowed.