为了可视化点云识别算法的检测结果,需要将目标框进行显示。
主要绘制函数——pcl::visualization::PCLVisualizer 类中的addCube函数一共有三种方式添加立方体的方式
addCube (const pcl::ModelCoefficients &coefficients, const std::string &id="cube", int viewport=0);
addCube (const Eigen::Vector3f &translation, const Eigen::Quaternionf &rotation, double width, double height, double depth, const std::string &id="cube", int viewport=0);
addCube (float x_min, float x_max, float y_min, float y_max, float z_min, float z_max, double r=1.0, double g=1.0, double b=1.0, const std::string &id="cube", int viewport=0);
//对于第三种,如果min>max,不能画出立方体。缺颜色也不行
viewer->addCube(min.x, max.x, min.y, max.y, min.z, max.z, a[i], b[i], c[i], ss.str(), v2);
下面是绘制的源码
源码/*
* 任何点云格式均可,不要求点云带有RGB字段
*
*/
#include <iostream>
#include <pcl/point_types.h>
#include <pcl/io/ply_io.h>
#include <pcl/io/pcd_io.h>//pcd 读写类相关的头文件。
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/io.h>
#include <pcl/common/common.h>
#include <string>
#include <sstream>
#include <stdio.h>
using namespace std;
using namespace pcl;
using namespace io;
typedef struct _Bndbox_
{
float x;//中心点x坐标
float y;
float z;
float w;//矩形框长宽高
float l;
float h;
float heading;//方向
int id; //类别id
float score;
}BNDBOX;
int main()
{
PointCloud<PointXYZ>::Ptr cloud(new PointCloud<PointXYZ>);
char strFilepath[256] = "../pcd/1.pcd";//将自己的点云数据集的路径放在这里就可以了,我这里是一个校使馆的点云集
if (-1 == io::loadPCDFile(strfilepath, *cloud))
{ // 读取.pcd文件
cerr << "can't read file bunny.pcd" << endl;//如果未找到
return -1;
}
//int dian_shuliang=cloud.Size();
//printf("dian_shuliang=%d",dian_shuliang);
pcl::PointXYZ minPt, maxPt;
pcl::getMinMax3D (*cloud, minPt, maxPt);
cout << "minPt=" << minPt << "maxPt="<<maxPt<< endl;
std::cout << "点云大小:" << cloud->size() << std::endl;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0,205,205 ); // green
viewer->addPointCloud<pcl::PointXYZ>(cloud, single_color, "sample cloud");
BNDBOX boxitor = { 0 };;
//19.253149,-3.690310,-0.739302,3.767917,1.607678,1.544580,5.983136,0,0.740550
boxitor.x=19.253149;
boxitor.y=-3.690310;
boxitor.z=-0.739302;
boxitor.w=3.767917;
boxitor.l=1.607678;
boxitor.h=1.544580;
boxitor.heading=5.983136;
boxitor.id=0;
boxitor.score=0.740550;
stringstream ss;
ss << boxitor.id;
string boxname = ss.str();
//绕z轴旋转的角度调整
Eigen::AngleAxisf rotation_Vector(boxitor.heading, Eigen::Vector3f(0, 0, 1));
//绘制对象外接长方体
//参数为矩形的顶点,长宽高还有旋转角度以及长方体名称
//函数原型:addCube (const Eigen::Vector3f &translation, const Eigen::Quaternionf &rotation, double width, double height, double depth, const std::string &id="cube", int viewport=0);
viewer->addCube(Eigen::Vector3f(boxitor.x, boxitor.y, boxitor.z),
Eigen::Quaternionf(rotation_vector), boxitor.l, boxitor.w, boxitor.h, boxname);
//设置矩形框只有骨架
viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_REPRESENTATION, pcl::visualization::PCL_VISUALIZER_REPRESENTATION_WIREFRAME, boxname);
viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR, 1.0, 0.0, 0.0, boxname);
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
return 0;
}
效果图
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved