function outputs different matrix

Pages: 12
I minimized so the for loop only contains two functions.
but still it seems to take 2 seconds per image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

icovar=inversemat(covmat);

while(1){
		//copy webcam stream to image
		cap>>image;
		//print image to screen
		for(int i = 0; i < image.rows;i++)
		{
			for(int j=0;j<image.cols;j++)
			{
				pixel= image.at<Vec3b>(i,j);	
				
				//own made inv matrix function
				mdist=mahadistance(icovar,meanmat,pixel);
				if(mdist>distance)
					mask.at<uchar>(i,j)=255;
				else
					mask.at<uchar>(i,j)=0;
			}
		}
	
		imshow("original",image);
		imshow("mask",mask);


but i don't understand why it is still taking long only 2 instructions in the loop and they execute pretty quickly it has a lot of commenting on it.
Do a quick test: replace mdist=mahadistance(icovar,meanmat,pixel); with mdist=0;
If it works fast, your problem is in mahadistance function. If you post that, someone might find a way to make it faster
Sorry, I noticed you posted it already. But please check your speed with mdist=0; Also, you can comment out line by line, see where is your problem.
Last edited on
Ok
so when i replaced mdist=mahadistance(icovar,meanmat,pixel) with mdist=0 it was moving pretty fast atleast several frames per second 2-3, but it was fast.

so something is wrong with my mahadisance calculation the function is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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]);
	
	for(int k = 0; k < 3;k++)
	{
		x2[0]=x2[0]+(x1[k]*invcov.at<double>(k,0));
		x2[1]=x2[1]+(x1[k]*invcov.at<double>(k,1));  //-1.#QNAN is element of inverse covariance matrix
		x2[2]=x2[2]+(x1[k]*invcov.at<double>(k,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;
}


thanks
I can't see what is slowing this down, simple calculations.
Topic archived. No new replies allowed.
Pages: 12