c_cpp 使用PCLVisualizer和Kinect v1 Grabber绘制点云
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 使用PCLVisualizer和Kinect v1 Grabber绘制点云相关的知识,希望对你有一定的参考价值。
#include "stdafx.h"
// Disable Error C4996 that occur when using Boost.Signals2.
#ifdef _DEBUG
#define _SCL_SECURE_NO_WARNINGS
#endif
#include "kinect_grabber.h"
#include <pcl/visualization/pcl_visualizer.h>
boost::mutex mutex;
void keyboard_callback( const pcl::visualization::KeyboardEvent& event, void* )
{
if( event.getKeyCode() && event.keyDown() ){
std::cout << "Key : " << event.getKeyCode() << std::endl;
}
}
void mouse_callback( const pcl::visualization::MouseEvent& event, void* )
{
if( event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && event.getButton() == pcl::visualization::MouseEvent::LeftButton ){
std::cout << "Mouse : " << event.getX() << ", " << event.getY() << std::endl;
}
}
int _tmain( int argc, _TCHAR* argv[] )
{
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer( new pcl::visualization::PCLVisualizer( "Point Cloud Viewer" ) );
viewer->registerKeyboardCallback( &keyboard_callback, "keyboard" );
viewer->registerMouseCallback( &mouse_callback, "mouse" );
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud;
boost::function<void( const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr& )> function =
[&cloud]( const pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr &ptr ){
boost::mutex::scoped_lock lock( mutex );
cloud = ptr;
};
boost::shared_ptr<pcl::Grabber> grabber = boost::make_shared<pcl::KinectGrabber>();
boost::signals2::connection connection = grabber->registerCallback( function );
grabber->start();
while( !viewer->wasStopped() ){
viewer->spinOnce();
if( cloud ){
if( !viewer->updatePointCloud( cloud, "cloud" ) ){
viewer->addPointCloud( cloud, "cloud" );
viewer->resetCameraViewpoint( "cloud" );
}
}
if( GetKeyState( VK_ESCAPE ) < 0 ){
break;
}
}
grabber->stop();
if( connection.connected() ){
connection.disconnect();
}
return 0;
}
#include "stdafx.h"
// Disable Error C4996 that occur when using Boost.Signals2.
#ifdef _DEBUG
#define _SCL_SECURE_NO_WARNINGS
#endif
#include "kinect_grabber.h"
#include <pcl/visualization/pcl_visualizer.h>
template <typename PointType>
class Viewer
{
typedef pcl::PointCloud<PointType> PointCloud;
typedef typename PointCloud::ConstPtr ConstPtr;
public:
Viewer( pcl::Grabber& grabber )
: viewer( new pcl::visualization::PCLVisualizer( "Point Cloud Viewer" ) )
, grabber( grabber )
{
}
void run()
{
viewer->registerMouseCallback( &Viewer::mouse_callback, *this );
viewer->registerKeyboardCallback( &Viewer::keyboard_callback, *this );
boost::function<void( const ConstPtr& ) > callback = boost::bind( &Viewer::cloud_callback, this, _1 );
boost::signals2::connection connection = grabber.registerCallback( callback );
grabber.start();
while( !viewer->wasStopped() ){
viewer->spinOnce();
ConstPtr cloud;
if( mutex.try_lock() ){
buffer.swap( cloud );
mutex.unlock();
}
if( cloud ){
if( !viewer->updatePointCloud( cloud, "Cloud" ) ){
viewer->addPointCloud( cloud, "Cloud" );
viewer->resetCameraViewpoint( "Cloud" );
}
}
if( GetKeyState( VK_ESCAPE ) < 0 ){
break;
}
}
grabber.stop();
if( connection.connected() ){
connection.disconnect();
}
}
private:
void cloud_callback( const ConstPtr& cloud )
{
boost::mutex::scoped_lock lock( mutex );
buffer = cloud;
}
void keyboard_callback( const pcl::visualization::KeyboardEvent& event, void* )
{
if( event.getKeyCode() && event.keyDown() ){
std::cout << "Key : " << event.getKeyCode() << std::endl;
}
}
void mouse_callback( const pcl::visualization::MouseEvent& event, void* )
{
if( event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && event.getButton() == pcl::visualization::MouseEvent::LeftButton ){
std::cout << "Mouse : " << event.getX() << ", " << event.getY() << std::endl;
}
}
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
pcl::Grabber& grabber;
boost::mutex mutex;
ConstPtr buffer;
};
int _tmain( int argc, _TCHAR* argv[] )
{
boost::shared_ptr<pcl::Grabber> grabber = boost::make_shared<pcl::KinectGrabber>();
Viewer<pcl::PointXYZRGB> viewer( *grabber );
viewer.run();
return 0;
}
以上是关于c_cpp 使用PCLVisualizer和Kinect v1 Grabber绘制点云的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 使用PCLVisualizer在多个Windows上绘制点云
c_cpp 将渲染图像转换为来自pcl :: visualization :: PCLVisualizer的cv :: Mat
pcl-qt使用QVTKWidget 与PCLVisualizer 显示雷达点云