pcl+opencv,将pcd点云投影为图片

#include 
#include 
#include 
#include 


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


	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);

	if (pcl::io::loadPCDFile("test.pcd", *cloud) == -1)//*打开点云文件
	{
		PCL_ERROR("Couldn't read file test_pcd.pcd\n");
		return(-1);
	}


	int num = cloud->points.size();

	cv::Mat img(1000, 1000, CV_8UC1, cv::Scalar(0, 0, 0));
	

	std::cout << "Loaded "
		<< cloud->width * cloud->height
		<< " data points from test_pcd.pcd with the following fields: "
		<< std::endl;
	//for (size_t i = 0; i < 5; ++i)
	//	std::cout << "    " << cloud->points[i].x
	//	<< " " << cloud->points[i].y
	//	<< " " << cloud->points[i].z << std::endl;

    for (int i = 0; i < num; i++)
    {

		if (cloud->points[i].x < -9 || cloud->points[i].x >= 9) //不在图片范围内的点去掉
		{
			continue;
		}
		if (cloud->points[i].y < -2 || cloud->points[i].y >= 2) //不在图片范围内的点去掉
		{
			continue;
		}
		if (cloud->points[i].z < -1 || cloud->points[i].z >= 5) //不在图片范围内的点去掉
		{
			continue;
		}

        int x = std::round((cloud->points[i].x - (-10)) * 50);

		// int y = std::round((cloud->points[i].y - (-3)) * 50);

		int z = std::round((cloud->points[i].z - (-2)) * 50);	
		img.at(z,x) = 255;
		//(row, col)


    }
	while (1) {
		cv::imshow("pcd", img);
		cv::waitKey(10);
	}

	return(0);
}

你可能感兴趣的:(opencv,计算机视觉,人工智能)