Study/컴퓨터비전

moment를 이용한 주축 관련 계산

빠릿베짱이 2013. 12. 3. 01:04
반응형

논문 제목  : Computer vision for computer games

http://www.merl.com/papers/docs/TR96-35.pdf

CAM-Shift에도 같은 내용이 들어있음.

 

 

 

 

float GetTheta(CSvImage& coloring, BLOBELE& boundbox)
{
	double xc=0, yc=0;
	int cnt =0;
	vector<CPoint> vPoint;
	for(int y= boundbox.rt.top; y<= boundbox.rt.bottom; y++)
	{
		for(int x= boundbox.rt.left; x<= boundbox.rt.right; x++)
		{
			if(coloring.GetPixel(x,y) == boundbox.Coloring)
			{
				xc += x;
				yc += y;
				vPoint.push_back(CPoint(x,y));
			}
		}
	}
	xc /= vPoint.size();
	yc /= vPoint.size();
	for(int i=0; i< vPoint.size(); i++)
	{
		vPoint.at(i).x -= xc;
		vPoint.at(i).y -= yc;
	}
	double m00 = vPoint.size();
	double m11=0, m02=0, m20=0;

	for(int i=0; i< vPoint.size(); i++)
	{
		m11 += (vPoint.at(i).x * vPoint.at(i).y);
		m20 += (vPoint.at(i).x * vPoint.at(i).x);
		m02 += (vPoint.at(i).y * vPoint.at(i).y);
	}

	double a = m20 / m00 ;
	double b = 2 * (m11/m00);
	double c = m02/m00 ;
	double theta = atan2(b, (a-c))/2;
	double l1 = sqrt( ( (a+c) + sqrt(b*b + (a-c)*(a-c)) )/ 2);
	double l2 = sqrt( ( (a+c) - sqrt(b*b + (a-c)*(a-c)) )/ 2);	
	int lpx = (l1) *	cos(theta) ;
	int lpy = (l1) *	sin(theta) ;
// 	cvLine(coloring.m_Buff, cvPoint(xc,yc), cvPoint(xc + lpx, yc+lpy), cvScalar(0,0,255), 2);
// 	cvShowImage("anlge", coloring.m_Buff);
//	cvWaitKey(0);
	if(theta < 0 )
	{
		theta += CV_PI*2;
	}
	float aaa = theta/CV_PI *180;
	return theta/CV_PI *180;
}

반응형