我正在尝试为一组点计算最佳拟合平面,并使用SVD计算由ax+by+cz+d=0
给出的平面方程。使用SVD计算最佳拟合平面方程时的误差
我已经实现了SVD并成功地获得了飞机的正常,但是我无法计算d
。
经过一番挖掘,我用方程中计算出来的质心替换计算d
,但我得到的值不正确。我确定这是一个不正确的值,因为我将它与RANSAC方法进行比较。
我的代码实现如下
pcl::ModelCoefficients normal_extractor::plane_est_svd(pcl::PointCloud<pcl::PointXYZ>::ConstPtr point_cloud)
{
Eigen::MatrixXd points_3D(3,point_cloud->width);
//assigning the points from point cloud to matrix
for (int i=0;i<point_cloud->width;i++)
{
points_3D(0,i) = point_cloud->at(i).x;
points_3D(1,i) = point_cloud->at(i).y;
points_3D(2,i) = point_cloud->at(i).z;
}
// calcaulating the centroid of the pointcloud
Eigen::MatrixXd centroid = points_3D.rowwise().mean();
//std::cout<<"The centroid of the pointclouds is given by:\t"<<centroid<<std::endl;
//subtract the centroid from points
points_3D.row(0).array() -= centroid(0);
points_3D.row(1).array() -= centroid(1);
points_3D.row(2).array() -= centroid(2);
//calculate the SVD of points_3D matrix
Eigen::JacobiSVD<Eigen::MatrixXd> svd(points_3D,Eigen::ComputeFullU);
Eigen::MatrixXd U_MAT = svd.matrixU();
//std::cout<<"U matrix transpose is:"<<U_MAT<<std::endl<<std::endl<<"U matrix is:"<<svd.matrixU()<<std::endl;
/*********************************************************************************************
* caculating d by sybstituting the centroid back in the quation
* aCx+bCy+cCz = -d
********************************************************************************************/
//double d = -((U_MAT(0,2)*points_3D(0,1))+ (U_MAT(1,2)*points_3D(1,1)) + (U_MAT(1,2)*points_3D(1,2)));
double d = -((U_MAT(0,2)*centroid(0))+ (U_MAT(1,2)*centroid(1)) + (U_MAT(1,2)*centroid(2)));
pcl::ModelCoefficients normals;
normals.values.push_back(U_MAT(0,2));
normals.values.push_back(U_MAT(1,2));
normals.values.push_back(U_MAT(2,2));
normals.values.push_back(d);
return(normals);
}
我得到的结果是
RANSAC方法:
a = -0.0584306 b = 0.0358117 c = 0.997649 d = -0.161604
SVD方法:
a = 0.0584302 b = -0.0357721 c = -0.99765 d = 0.00466139
从结果,我相信法线计算得很好(虽然方向相反),但d
的值不正确。我不知道我要去哪里错。任何帮助真的很感激。
在此先感谢..
如果' d'计算错误,请检查您正在使用的公式。最后一项('U_MAT(1,2)*质心(2)')是否不正确(而不是'U_MAT(2,2)')? – 1201ProgramAlarm
当然是,非常感谢。出于某种原因,我有选择性地对此视而不见。 @ 1201ProgramAlarm再次感谢你。 – spacemanspiff