opencv向量点云可视化

#pragma once
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cmath>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
#include <boost/thread/thread.hpp>
#include <pcl/common/common_headers.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <Eigen/Core>
#include <Eigen/Geometry>
#include <Eigen/SVD>

using namespace std;
using namespace pcl;
using namespace cv;

pcl::PointXYZ cvTopcl(Point3f src)
{
	pcl::PointXYZ basic_point;
	basic_point.x = src.x;
	basic_point.y = src.y;
	basic_point.z = src.z;
	return basic_point;
}

pcl::PointCloud<pcl::PointXYZ>::Ptr getpclPtr(vector<Point3f> cvpoints)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud(new pcl::PointCloud<pcl::PointXYZ>);
	pcl::PointXYZ basic_point;
	if (cvpoints.size() > 0)
	{
		for (size_t i = 0; i < cvpoints.size(); i++)
		{
			basic_point = cvTopcl(cvpoints[i]);
			basic_cloud->points.push_back(basic_point);
		}
		basic_cloud->width = (int)basic_cloud->points.size();
		basic_cloud->height = 1;
	}

	return basic_cloud;
}


//点云可视化
boost::shared_ptr<pcl::visualization::PCLVisualizer>  shapesVis(pcl::PointCloud<pcl::PointXYZ>::ConstPtr  cloud1)
{
	//创建3D窗口并添加点云    
	boost::shared_ptr<pcl::visualization::PCLVisualizer>  viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
	viewer->setBackgroundColor(0, 0, 0);
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> g(cloud1, 0, 255, 0);
	viewer->addPointCloud<pcl::PointXYZ>(cloud1, g, "g");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "g");

	viewer->addCoordinateSystem(1.0);
	viewer->initCameraParameters();

	return (viewer);
}


void main()
{
	boost::shared_ptr<pcl::visualization::PCLVisualizer> PlaneViewer(new pcl::visualization::PCLVisualizer("3D Plane Viewer"));
	PlaneViewer->setBackgroundColor(0, 0, 0);
	PlaneViewer->addCoordinateSystem(1.0);
	PlaneViewer->initCameraParameters();

	while (true)
	{
		vector<Point3f>vec;
		for (int i = 0; i < 100; i++)
		{
			float x = rand() % 100;
			float y = rand() % 100;
			float z = rand() % 100;

			Point3f ttt = { x, y, z };

			vec.push_back(ttt);
		}

		pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr = getpclPtr(vec);
		pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> rgb(basic_cloud_ptr, 0, 255, 0);

		//创建3D窗口并添加点云??????????????????
		PlaneViewer->addPointCloud<pcl::PointXYZ>(basic_cloud_ptr, rgb, "Plane cloud");
		PlaneViewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "Plane cloud");

		PlaneViewer->spinOnce(10);

		PlaneViewer->removeAllPointClouds();
		PlaneViewer->removeAllShapes();
	}
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页