点云数据归一化、统一化,很重要,比如,你做完一个模型后,发现鼠标控制模型时,根本不是以中心点控制,就是因为数据没有归一化等
pcl::PointCloud<pcl::PointXYZ>::Ptr normialize(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, pcl::PointXYZ minPt, pcl::PointXYZ maxPt)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_back(new pcl::PointCloud<pcl::PointXYZ>);
cloud_back->width = cloud_in->width;
cloud_back->height = cloud_in->height;
cloud_back->is_dense = cloud_in->is_dense;
cloud_back->resize(cloud_in->size());
double middlex, middley, middlez;
middlex = (maxPt.x + minPt.x) / 2;
middley = (maxPt.y + minPt.y) / 2;
middlez = (maxPt.z + minPt.z) / 2;
for (int i = 0; i < cloud_back->size(); i++)
{
cloud_back->points[i].x = cloud_in->points[i].x - middlex;
cloud_back->points[i].y = cloud_in->points[i].y - middley;
cloud_back->points[i].z = cloud_in->points[i].z - middlez;
}
//scale based on x range
double scalecoe = (maxPt.x - minPt.x) / 2;
for (int i = 0; i < cloud_back->size(); i++) {
cloud_back->points[i].x = cloud_back->points[i].x / scalecoe;
cloud_back->points[i].y = cloud_back->points[i].y / scalecoe;
cloud_back->points[i].z = cloud_back->points[i].z / scalecoe;
}
return cloud_back;
}
int main()
{
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::io::loadPLYFile<pcl::PointXYZ>("hand.ply", *cloud);
cout << "数据统一化前的最值" << endl;
pcl::PointXYZ minPt, maxPt;
pcl::getMinMax3D(*cloud, minPt, maxPt);
std::cerr << " " << minPt.x << " " << minPt.y << " " << minPt.z << std::endl;
std::cerr << " " << maxPt.x << " " << maxPt.y << " " << maxPt.z << std::endl;
cout << "数据统一化后的最值" << endl;
cloud = normialize(cloud, minPt, maxPt);
pcl::getMinMax3D(*cloud, minPt, maxPt);
std::cerr << " " << minPt.x << " " << minPt.y << " " << minPt.z << std::endl;
std::cerr << " " << maxPt.x << " " << maxPt.y << " " << maxPt.z << std::endl;
pcl::io::savePLYFile("Newhand.ply", *cloud);
return 0;
}