[CC]平面拟合
常见的平面拟合方法一般是最小二乘法。当误差服从正态分布时,最小二乘方法的拟合效果还是很好的,可以转化成PCA问题。
当观测值的误差大于2倍中误差时,认为误差较大。采用最小二乘拟合时精度降低,不够稳健。
提出了一些稳健的方法:有移动最小二乘法(根据距离残差增加权重);采用2倍距离残差的协方差剔除离群点;迭代重权重方法(选权迭代法)。
MainWindow中的平面拟合方法,调用了ccPlane的Fit方法。
void MainWindow::doActionFitPlane()
{
doComputePlaneOrientation(false);
} void MainWindow::doActionFitFacet()
{
doComputePlaneOrientation(true);
} static double s_polygonMaxEdgeLength = ;
void MainWindow::doComputePlaneOrientation(bool fitFacet)
{
ccHObject::Container selectedEntities = m_selectedEntities;
size_t selNum = selectedEntities.size();
if (selNum < )
return; double maxEdgeLength = ;
if (fitFacet)
{
bool ok = true;
maxEdgeLength = QInputDialog::getDouble(this,"Fit facet", "Max edge length (0 = no limit)", s_polygonMaxEdgeLength, , 1.0e9, , &ok);
if (!ok)
return;
s_polygonMaxEdgeLength = maxEdgeLength;
} for (size_t i=; i<selNum; ++i)
{
ccHObject* ent = selectedEntities[i];
ccShiftedObject* shifted = ;
CCLib::GenericIndexedCloudPersist* cloud = ; if (ent->isKindOf(CC_TYPES::POLY_LINE))
{
ccPolyline* poly = ccHObjectCaster::ToPolyline(ent);
cloud = static_cast<CCLib::GenericIndexedCloudPersist*>(poly);
shifted = poly;
}
else
{
ccGenericPointCloud* gencloud = ccHObjectCaster::ToGenericPointCloud(ent);
if (gencloud)
{
cloud = static_cast<CCLib::GenericIndexedCloudPersist*>(gencloud);
shifted = gencloud;
}
} if (cloud)
{
double rms = 0.0;
CCVector3 C,N; ccHObject* plane = ;
if (fitFacet)
{
ccFacet* facet = ccFacet::Create(cloud, static_cast<PointCoordinateType>(maxEdgeLength));
if (facet)
{
plane = static_cast<ccHObject*>(facet);
N = facet->getNormal();
C = facet->getCenter();
rms = facet->getRMS(); //manually copy shift & scale info!
if (shifted)
{
ccPolyline* contour = facet->getContour();
if (contour)
{
contour->setGlobalScale(shifted->getGlobalScale());
contour->setGlobalShift(shifted->getGlobalShift());
}
}
}
}
else
{
ccPlane* pPlane = ccPlane::Fit(cloud, &rms);
if (pPlane)
{
plane = static_cast<ccHObject*>(pPlane);
N = pPlane->getNormal();
C = *CCLib::Neighbourhood(cloud).getGravityCenter();
pPlane->enableStippling(true);
}
} //as all information appears in Console...
forceConsoleDisplay(); if (plane)
{
ccConsole::Print(QString("[Orientation] Entity '%1'").arg(ent->getName()));
ccConsole::Print("\t- plane fitting RMS: %f",rms); //We always consider the normal with a positive 'Z' by default!
if (N.z < 0.0)
N *= -1.0;
ccConsole::Print("\t- normal: (%f,%f,%f)",N.x,N.y,N.z); //we compute strike & dip by the way
PointCoordinateType dip = , dipDir = ;
ccNormalVectors::ConvertNormalToDipAndDipDir(N,dip,dipDir);
QString dipAndDipDirStr = ccNormalVectors::ConvertDipAndDipDirToString(dip,dipDir);
ccConsole::Print(QString("\t- %1").arg(dipAndDipDirStr)); //hack: output the transformation matrix that would make this normal points towards +Z
ccGLMatrix makeZPosMatrix = ccGLMatrix::FromToRotation(N,CCVector3(,,PC_ONE));
CCVector3 Gt = C;
makeZPosMatrix.applyRotation(Gt);
makeZPosMatrix.setTranslation(C-Gt);
ccConsole::Print("[Orientation] A matrix that would make this plane horizontal (normal towards Z+) is:");
ccConsole::Print(makeZPosMatrix.toString(,' ')); //full precision
ccConsole::Print("[Orientation] You can copy this matrix values (CTRL+C) and paste them in the 'Apply transformation tool' dialog"); plane->setName(dipAndDipDirStr);
plane->applyGLTransformation_recursive(); //not yet in DB
plane->setVisible(true);
plane->setSelectionBehavior(ccHObject::SELECTION_FIT_BBOX); ent->addChild(plane);
plane->setDisplay(ent->getDisplay());
plane->prepareDisplayForRefresh_recursive();
addToDB(plane);
}
else
{
ccConsole::Warning(QString("Failed to fit a plane/facet on entity '%1'").arg(ent->getName()));
}
}
} refreshAll();
updateUI();
}
ccPlane的fit方法:
ccPlane* ccPlane::Fit(CCLib::GenericIndexedCloudPersist *cloud, double* rms/*=0*/)
{
//number of points
unsigned count = cloud->size();
if (count < 3)
{
ccLog::Warning("[ccPlane::Fit] Not enough points in input cloud to fit a plane!");
return 0;
} CCLib::Neighbourhood Yk(cloud); //plane equation
const PointCoordinateType* theLSPlane = Yk.getLSPlane();
if (!theLSPlane)
{
ccLog::Warning("[ccPlane::Fit] Not enough points to fit a plane!");
return 0;
} //get the centroid
const CCVector3* G = Yk.getGravityCenter();
assert(G); //and a local base
CCVector3 N(theLSPlane);
const CCVector3* X = Yk.getLSPlaneX(); //main direction
assert(X);
CCVector3 Y = N * (*X); //compute bounding box in 2D plane
CCVector2 minXY(0,0), maxXY(0,0);
cloud->placeIteratorAtBegining();
for (unsigned k=0; k<count; ++k)
{
//projection into local 2D plane ref.
CCVector3 P = *(cloud->getNextPoint()) - *G; CCVector2 P2D( P.dot(*X), P.dot(Y) ); if (k != 0)
{
if (minXY.x > P2D.x)
minXY.x = P2D.x;
else if (maxXY.x < P2D.x)
maxXY.x = P2D.x;
if (minXY.y > P2D.y)
minXY.y = P2D.y;
else if (maxXY.y < P2D.y)
maxXY.y = P2D.y;
}
else
{
minXY = maxXY = P2D;
}
} //we recenter the plane
PointCoordinateType dX = maxXY.x-minXY.x;
PointCoordinateType dY = maxXY.y-minXY.y;
CCVector3 Gt = *G + *X * (minXY.x + dX / 2) + Y * (minXY.y + dY / 2);
ccGLMatrix glMat(*X,Y,N,Gt); ccPlane* plane = new ccPlane(dX, dY, &glMat); //compute least-square fitting RMS if requested
if (rms)
{
*rms = CCLib::DistanceComputationTools::computeCloud2PlaneDistanceRMS(cloud, theLSPlane);
plane->setMetaData(QString("RMS"),QVariant(*rms));
} return plane;
}
Efficient Ransac shape extract插件调用的模板类Plane实现,可以看到使用的Jacobi特征值分解的方法实现。
template< class PointT >
template< class PointsForwardIt, class WeightsForwardIt >
bool Plane< PointT >::Fit(const PointType &origin, PointsForwardIt begin,PointsForwardIt end, WeightsForwardIt weights)
{
MatrixXX< PointType::Dim, PointType::Dim, ScalarType > c, v;
CovarianceMatrix(origin, begin, end, weights, &c);
VectorXD< PointType::Dim, ScalarType > d;
if(!Jacobi(c, &d, &v))
{
//std::cout << "Jacobi failed:" << std::endl;
//std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl
// << "cov:" << std::endl
// << c[0][0] << c[1][0] << c[2][0] << std::endl
// << c[0][1] << c[1][1] << c[2][1] << std::endl
// << c[0][2] << c[1][2] << c[2][2] << std::endl;
//std::cout << "recomp origin:" << std::endl;
//PointT com;
//Mean(begin, end, weights, &com);
//std::cout << "origin = " << origin[0] << "," << origin[1] << "," << origin[2] << std::endl;
//std::cout << "recomp covariance:" << std::endl;
//CovarianceMatrix(com, begin, end, weights, &c);
//std::cout << "cov:" << std::endl
//<< c[0][0] << c[1][0] << c[2][0] << std::endl
//<< c[0][1] << c[1][1] << c[2][1] << std::endl
//<< c[0][2] << c[1][2] << c[2][2] << std::endl;
//std::cout << "weights and points:" << std::endl;
//WeightsForwardIt w = weights;
//for(PointsForwardIt i = begin; i != end; ++i, ++w)
// std::cout << (*i)[0] << "," << (*i)[1] << "," << (*i)[2]
// << " weight=" << (*w) << std::endl;
return false;
}
for(unsigned int i = 0; i < PointType::Dim; ++i)
d[i] = Math< ScalarType >::Abs(d[i]);
EigSortDesc(&d, &v);
_normal = PointType(v[PointType::Dim - 1]);
_d = -(_normal * origin);
return true;
}
[CC]平面拟合的更多相关文章
- 数据的平面拟合 Plane Fitting
数据的平面拟合 Plane Fitting 看到了一些利用Matlab的平面拟合程序 http://www.ilovematlab.cn/thread-220252-1-1.html
- 蛙蛙推荐: TensorFlow Hello World 之平面拟合
tensorflow 已经发布了 2.0 alpha 版本,所以是时候学一波 tf 了.官方教程有个平面拟合的类似Hello World的例子,但没什么解释,新手理解起来比较困难. 所以本文对这个案例 ...
- 三维点集拟合:平面拟合、RANSAC、ICP算法
ACM算法分类:http://www.kuqin.com/algorithm/20080229/4071.html 一: 拟合一个平面:使用SVD分解,代码里面去找吧 空间平面方程的一般表达式为: A ...
- RANSAC介绍(Matlab版直线拟合+平面拟合)
https://blog.csdn.net/u010128736/article/details/53422070
- PCL利用RANSAC自行拟合分割平面
利用PCL中分割算法. pcl::SACSegmentation<pcl::PointXYZ> seg; ,不利用法线参数,只根据模型参数得到的分割面片,与想象的面片差距很大, pcl:: ...
- 使用matlab进行空间拟合
假设有这么一组数据, x=[4 5 6 7 8 4 8 10]'; y=[56 56 56 56 56 60 60 60]';z=[6 6 6 9 6 19 6 6]'; 要求出其平面方程z=C+Ax ...
- 【Matlab&Mathematica】对三维空间上的点进行椭圆拟合
问题是这样:比如有一个地心惯性系的轨道,然后从轨道上取了几个点,问能不能根据这几个点把轨道还原了? 当然,如果知道轨道这几个点的速度的情况下,根据轨道六根数也是能计算轨道的,不过真近点角是随时间变动的 ...
- RANSAC算法笔记
最近在做平面拟合,待处理的数据中有部分噪点需要去除,很多论文中提到可以使用Ransac方法来去除噪点. 之前在做图像配准时,用到了Ransac算法,但是没有去仔细研究,现在好好研究一番. 参考: ht ...
- 45、Docker 加 tensorflow的机器学习入门初步
[1]最近领导天天在群里发一些机器学习的链接,搞得好像我们真的要搞机器学习似的,吃瓜群众感觉好神奇呀. 第一步 其实也是最后一步,就是网上百度一下,Docker Toolbox,下载下来,下载,安装之 ...
随机推荐
- Oracle查询实用命令
1.设置每行的长度: SET LIN[ESIZE] 200; 2.设置分页数量: SET PAGES[IZE] 50; 3.查看表空间相关信息: select file_id, tablespace_ ...
- 最大公约数和最小公倍数--java实现
代码: //最大公约数 public int gcd(int p,int q){ if(q == 0) return p; return gcd(q, p % q); } //最小公倍数 public ...
- 【Unity3d游戏开发】Unity3D中常用的物理学公式
马三最近在一直负责Unity中的物理引擎这一块,众所周知,Unity内置了NVIDIA公司PhysX物理引擎.然而,马三一直觉得只会使用引擎而不去了解原理的程序猿不是一位老司机.所以对一些常用的物理学 ...
- CentOS系统配置记录
1. 挂載 ntfs: 确定已经安装了rpmforge软件库的源.在线安装使用 yum install 命令 含有 rpmforge源. yum install fuse ntfs-3g -y 安装后 ...
- js的三种继承方式及其优缺点
[转] 第一种,prototype的方式: //父类 function person(){ this.hair = 'black'; this.eye = 'black'; this.skin = ' ...
- Android服务(Service)研究
Service是android四大组件之一,没有用户界面,一直在后台运行. 为什么使用Service启动新线程执行耗时任务,而不直接在Activity中启动一个子线程处理? 1.Activity会被用 ...
- 12.super关键字
①在java中使用super关键字来调用父类的成分
- python与正则表达式
匹配一个字符: . 任意非\n字符 [...] \d \D digit \s \S space \w \W word 匹配前一个字符的多个: * 0->> + 1->> ? 0 ...
- hibernate关联关系笔记
Hibernate关联关系笔记 单向N:1 * 有连接表:在N方使用<join>/<many-to-one>.1方无需配置与之关联的持久化类. * 没有连接表:在N方使用& ...
- PowerGUI错误-Microsoft SharePoint is not supported with version 4 of the Microsoft .Net Runtime
PowerGUI是个写powershell的神器,相比于PowerShell ISE,它那断点和按步追踪的能力不知让多少脚本狂们神魂颠倒.. 今天我也下载了一个放到测试环境里打算玩玩,结果出师不利,一 ...