PCL 利用空间同名点求取旋转和平移矩阵

#include <iostream>
#include <pcl/common/common.h>
#include <pcl/common/angles.h>
#include <pcl/common/transforms.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/registration/transformation_estimation_svd.h>

using namespace std;

int main(int argc, char **argv) {

	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in (new pcl::PointCloud<pcl::PointXYZ> ());
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_out (new pcl::PointCloud<pcl::PointXYZ> ());

	cloud_in->width = 4;
	cloud_in->height = 1;
	cloud_in->is_dense = false;
	cloud_in->resize(cloud_in->width * cloud_in->height);

	cloud_out->width = 4;
	cloud_out->height = 1;
	cloud_out->is_dense = false;
	cloud_out->resize(cloud_out->width * cloud_out->height);

	//从文件中读取4对同名点
	FILE * fRead;
	fRead = fopen("C:\\Users\\Administrator\\Desktop\\pt.txt","r");

	cout<<"cloud_in : "<<endl;
	for (int i=0;i<cloud_in->points.size();i++)
	{
		double pt[3];
		fscanf(fRead,"%lf %lf %lf",pt,pt+1,pt+2);
		cloud_in->points[i].x = pt[0];
		cloud_in->points[i].y = pt[1];
		cloud_in->points[i].z = pt[2];

		cout<<cloud_in->points[i].x<<"  \t"<<cloud_in->points[i].y<<"  \t"<<cloud_in->points[i].z<<endl;
	}

	cout<<"cloud_out : "<<endl;
	for (int i=0;i<cloud_out->points.size();i++)
	{
		double pt[3];
		fscanf(fRead,"%lf %lf %lf",pt,pt+1,pt+2);
		cloud_out->points[i].x = pt[0];
		cloud_out->points[i].y = pt[1];
		cloud_out->points[i].z = pt[2];

		cout<<cloud_out->points[i].x<<"  \t"<<cloud_out->points[i].y<<"  \t"<<cloud_out->points[i].z<<endl;
	}

	//利用SVD方法求解变换矩阵
	pcl::registration::TransformationEstimationSVD<pcl::PointXYZ,pcl::PointXYZ> TESVD;
	pcl::registration::TransformationEstimationSVD<pcl::PointXYZ,pcl::PointXYZ>::Matrix4 transformation2;
	TESVD.estimateRigidTransformation (*cloud_in,*cloud_out,transformation2);
	//输出变换矩阵信息
	std::cout << "The Estimated Rotation and translation matrices (using getTransformation function) are : \n" << std::endl;
	printf ("\n");
	printf ("    | %6.3f %6.3f %6.3f | \n", transformation2 (0,0), transformation2 (0,1), transformation2 (0,2));
	printf ("R = | %6.3f %6.3f %6.3f | \n", transformation2 (1,0), transformation2 (1,1), transformation2 (1,2));
	printf ("    | %6.3f %6.3f %6.3f | \n", transformation2 (2,0), transformation2 (2,1), transformation2 (2,2));
	printf ("\n");
	printf ("t = < %0.3f, %0.3f, %0.3f >\n", transformation2 (0,3), transformation2 (1,3), transformation2 (2,3));

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值