I wrote a small code snippet to merge two point clouds with ICP using PCL. However, no matter what I do I end up with the final point cloud containing only the first point cloud. A picture: 
#define _CRT_SECURE_NO_DEPRECATE
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/registration/icp.h>
#include <pcl/io/pcd_io.h>
#include <boost/make_shared.hpp>
#include "Dot3DReader.h"
int main(int argc, char** argv)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud1(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("D:/Imerso/PCL PointCloudExampleData/Frame1.pcd", *cloud1) == -1) //* load the file
{
PCL_ERROR("Couldn't read file Frame1.pcd \n");
return (-1);
}
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud2(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>("D:/Imerso/PCL PointCloudExampleData/Frame2.pcd", *cloud2) == -1) //* load the file
{
PCL_ERROR("Couldn't read file Frame2.pcd \n");
return (-1);
}
std::cout << "Read both files." << std::endl;
pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;
icp.setInputSource(cloud1);
icp.setInputTarget(cloud2);
boost::shared_ptr<pcl::PointCloud<pcl::PointXYZ>> Final(new pcl::PointCloud<pcl::PointXYZ>());
std::cout << "Starting aligning." << std::endl;
icp.align(*Final);
std::cout << "Finished aligning." << std::endl;
std::cout << "has converged:" << icp.hasConverged() << " score: " <<
icp.getFitnessScore() << std::endl;
// CloudViewer を生成
// (表示は別スレッドで実行される)
pcl::visualization::CloudViewer viewer("Final cloud Viewer");
// CloudViewer で点群を表示
viewer.showCloud(Final);
while (true) {
// ESC キーで終了
if (GetKeyState(VK_ESCAPE) < 0) {
return 0;
}
}
return 0;
}
As a bonus, I also want to preserve the colors of the points being aligned when using RGB points. How can I do that?
My guess is that it does not find a match and simply discards the second point cloud.
Thank you for your help.