2011/11/21
[OpenCV] Linear Least Square calculation using OpenCV
출처: http://mariotapilouw.blogspot.com/2011/04/least-square-using-opencv.html
OpenCV를 이용한 Linear Least Square 를 계산하는 방법에 대한 글이다. 이 내용은 Fitting 을 하기 위해 중요한 것으로 기록 차원에서 발췌해 두었다. 역시 실제 참고는 원문을 이용하기 바란다.
Least Square method is an important tool for fitting either a plane or surface.This article will explain briefly on how we can use OpenCV for performing Linear Least Square for solving simultaneous equation. This technique can be applied for line or surface fitting.
As an example, an equation: Z = alpha * X + beta + h * delta, which has three unknown variables alpha, beta and h. The known variables are Z, X, and delta.
We have to create a matrix for the known variables according to the number of available data.
Then the data can be put into the matrix, assume that the data for Z are obtained from an array data:
after that, call the least square solver using CvSolve and if nothing is wrong with the data, the result can be directly accessed by accessing the matRes matrix
Finally, clean the memory of the matrices:
That's it. Hope it helps :)
(the source code is formatted using http://formatmysourcecode.blogspot.com/, nice stuff!)
<
Original Post : http://neodreamer-dev.tistory.com/619
OpenCV를 이용한 Linear Least Square 를 계산하는 방법에 대한 글이다. 이 내용은 Fitting 을 하기 위해 중요한 것으로 기록 차원에서 발췌해 두었다. 역시 실제 참고는 원문을 이용하기 바란다.
내용보기
Least Square method is an important tool for fitting either a plane or surface.This article will explain briefly on how we can use OpenCV for performing Linear Least Square for solving simultaneous equation. This technique can be applied for line or surface fitting.
As an example, an equation: Z = alpha * X + beta + h * delta, which has three unknown variables alpha, beta and h. The known variables are Z, X, and delta.
We have to create a matrix for the known variables according to the number of available data.
CvMat * matZ = cvCreateMat((dataLength), 1, CV_32FC1);
CvMat * matX = cvCreateMat((dataLength), 3, CV_32FC1);
CvMat * matRes = cvCreateMat(3, 1, CV_32FC1);
Then the data can be put into the matrix, assume that the data for Z are obtained from an array data:
//fill data
for(int i=0;i<dataLength;i++)
{
cvmSet(matZ, i, 0, data[i]);
cvmSet(matX, i, 0, (i*OBJSCALE));
cvmSet(matX, i, 1, 1);
cvmSet(matX, i, 2, 1);
}
after that, call the least square solver using CvSolve and if nothing is wrong with the data, the result can be directly accessed by accessing the matRes matrix
cvSolve(matX, matZ, matRes, CV_SVD);
double h = cvmGet(matRes, 2, 0);
double beta = cvmGet(matRes, 1, 0);
double alpha = cvmGet(matRes, 0, 0);
Finally, clean the memory of the matrices:
cvReleaseMat(&matZ);
cvReleaseMat(&matX);
cvReleaseMat(&matRes);
That's it. Hope it helps :)
(the source code is formatted using http://formatmysourcecode.blogspot.com/, nice stuff!)
<
Original Post : http://neodreamer-dev.tistory.com/619
Labels:
Least Square
,
OpenCV
,
TistoryOldPost
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment