回调函数和按键交互

高级功能:设置回掉函数进行交互、显示区域分割

按键事件:按a使图像显示和消失

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
// 包含相关头文件

// This function displays the help
void showHelp(char* program_name)
{
	std::cout << std::endl;
	std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
	std::cout << "-h or --help :  Show  help." << std::endl;
}

// 回掉函数所用数据结构
struct callback_args 
{
    bool* isShow;
    pcl::PointCloud<pcl::PointXYZ>::Ptr orgin_points;
    pcl::visualization::PCLVisualizer::Ptr viewerPtr;
};

// 按键事件回掉函数
void kb_callback(const pcl::visualization::KeyboardEvent& event, void* args)
{
	if (event.keyDown() && event.getKeyCode() == 'a')
	{
		std::cout << "a has pressed" << std::endl;
		struct callback_args* data = (struct callback_args*)args;
		if (*(data->isShow))
		{
			data->viewerPtr->removePointCloud("cloud");
			*(data->isShow) = false;
			std::cout << "remove" << std::endl;
		}
		else {
			data->viewerPtr->addPointCloud(data->orgin_points, "cloud");
			*(data->isShow) = true;
			std::cout << "add" << std::endl;
		}
	}
}



int main(int argc, char** argv)
{
	// Show help
	if (pcl::console::find_switch(argc, argv, "-h") || pcl::console::find_switch(argc, argv, "--help")) {
		std::cout << "没有help." << std::endl;
		return 0;
	}

	// Fetch point cloud filename in arguments | Works with PCD and PLY files
	std::vector<int> filenames;
	bool file_is_pcd = false;

	filenames = pcl::console::parse_file_extension_argument(argc, argv, ".ply");

	if (filenames.size() != 1) {
		filenames = pcl::console::parse_file_extension_argument(argc, argv, ".pcd");

		if (filenames.size() != 1) {
			showHelp(argv[0]);
			return -1;
		}
		else {
			file_is_pcd = true;
		}
	}

	// Load file | Works with PCD and PLY files
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

	if (file_is_pcd) {
		if (pcl::io::loadPCDFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}
	else {
		if (pcl::io::loadPLYFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}//load cloud
	//---------------------------------------------------------------------------------

	pcl::console::print_highlight("load cloud !\n");

	// 定义对象
	pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
	viewer->addPointCloud(cloud, "cloud");

	// 初始化参数
	bool isShow = true;
	struct callback_args kb_args;
	kb_args.isShow = &isShow;
	kb_args.orgin_points = cloud;
	kb_args.viewerPtr = viewer;

	// 设置回掉函数
	viewer->registerKeyboardCallback(kb_callback, (void*)&kb_args);

	// 1. 阻塞式
	viewer->spin();

	//system("pause");//不注释,命令行会提示按任意键继续,注释会直接跳出
	return 0;
}

点选取事件:只是显示一个更显眼的点覆盖原来的点

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
// 包含相关头文件

// This function displays the help
void showHelp(char* program_name)
{
	std::cout << std::endl;
	std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
	std::cout << "-h or --help :  Show  help." << std::endl;
}

// 回掉函数所用数据结构
struct callback_args 
{
	// structure used to pass arguments to the callback function
	pcl::PointCloud<pcl::PointXYZ>::Ptr clicked_points_3d;
	pcl::visualization::PCLVisualizer::Ptr viewerPtr;
};


void pp_callback(const pcl::visualization::PointPickingEvent& event, void* args)
{
	struct callback_args* data = (struct callback_args*)args;

	if (event.getPointIndex() == -1)
		return;
	int index = event.getPointIndex();
	std::cout << "index: " << index << std::endl;
	pcl::PointXYZ current_point;
	event.getPoint(current_point.x, current_point.y, current_point.z);
	data->clicked_points_3d->points.push_back(current_point);
	// Draw clicked points in red:
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red(data->clicked_points_3d, 255, 0, 0);
	data->viewerPtr->removePointCloud("clicked_points");
	data->viewerPtr->addPointCloud(data->clicked_points_3d, red, "clicked_points");
	data->viewerPtr->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, "clicked_points");
	std::cout << current_point.x << " " << current_point.y << " " << current_point.z << std::endl;
}

int main(int argc, char** argv)
{
	// Show help
	if (pcl::console::find_switch(argc, argv, "-h") || pcl::console::find_switch(argc, argv, "--help")) {
		std::cout << "没有help." << std::endl;
		return 0;
	}

	// Fetch point cloud filename in arguments | Works with PCD and PLY files
	std::vector<int> filenames;
	bool file_is_pcd = false;

	filenames = pcl::console::parse_file_extension_argument(argc, argv, ".ply");

	if (filenames.size() != 1) {
		filenames = pcl::console::parse_file_extension_argument(argc, argv, ".pcd");

		if (filenames.size() != 1) {
			showHelp(argv[0]);
			return -1;
		}
		else {
			file_is_pcd = true;
		}
	}

	// Load file | Works with PCD and PLY files
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

	if (file_is_pcd) {
		if (pcl::io::loadPCDFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}
	else {
		if (pcl::io::loadPLYFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}//load cloud
		
	pcl::console::print_highlight("load cloud\n");

	pcl::visualization::PCLVisualizer viewer;

	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green(cloud, 0, 255, 0);
	viewer.addPointCloud(cloud, green, "cloud");

	// Add point picking callback to viewer:
	struct callback_args cb_args;
	pcl::PointCloud<pcl::PointXYZ>::Ptr clicked_points_3d(new pcl::PointCloud<pcl::PointXYZ>);
	cb_args.clicked_points_3d = clicked_points_3d;
	cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(&viewer);
	viewer.registerPointPickingCallback(pp_callback, (void*)&cb_args);
	pcl::console::print_highlight("Shift+click on three floor points, then press 'Q'...\n");

	// Spin until 'Q' is pressed:
	viewer.spin();

	//system("pause");//不注释,命令行会提示按任意键继续,注释会直接跳出
	return 0;
}

区域选取事件

#include <iostream>

#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_cloud.h>
#include <pcl/console/parse.h>
#include <pcl/common/transforms.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/point_types.h>
// 包含相关头文件

// This function displays the help
void showHelp(char* program_name)
{
	std::cout << std::endl;
	std::cout << "Usage: " << program_name << " cloud_filename.[pcd|ply]" << std::endl;
	std::cout << "-h or --help :  Show  help." << std::endl;
}

// 回掉函数所用数据结构
struct callback_args {
	// structure used to pass arguments to the callback function
	pcl::PointCloud<pcl::PointXYZ>::Ptr orgin_points;
	pcl::PointCloud<pcl::PointXYZ>::Ptr chosed_points_3d;
	pcl::visualization::PCLVisualizer::Ptr viewerPtr;
};

void ap_callback(const pcl::visualization::AreaPickingEvent& event, void* args)
{
	struct callback_args* data = (struct callback_args*)args;
	std::vector<int> indiecs;

	if (!event.getPointsIndices(indiecs))
		return;
	for (int i = 0; i < indiecs.size(); ++i)
	{
		data->chosed_points_3d->push_back(data->orgin_points->points[indiecs[i]]);
	}

	// Draw clicked points in red:
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red(data->chosed_points_3d, 255, 0, 0);
	data->viewerPtr->removePointCloud("chosed_points");
	data->viewerPtr->addPointCloud(data->chosed_points_3d, red, "chosed_points");
	data->viewerPtr->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 5, "chosed_points");
	std::cout << "selected " << indiecs.size() << " points , now sum is " << data->chosed_points_3d->size() << std::endl;
}

int main(int argc, char** argv)
{
	// Show help
	if (pcl::console::find_switch(argc, argv, "-h") || pcl::console::find_switch(argc, argv, "--help")) {
		std::cout << "没有help." << std::endl;
		return 0;
	}

	// Fetch point cloud filename in arguments | Works with PCD and PLY files
	std::vector<int> filenames;
	bool file_is_pcd = false;

	filenames = pcl::console::parse_file_extension_argument(argc, argv, ".ply");

	if (filenames.size() != 1) {
		filenames = pcl::console::parse_file_extension_argument(argc, argv, ".pcd");

		if (filenames.size() != 1) {
			showHelp(argv[0]);
			return -1;
		}
		else {
			file_is_pcd = true;
		}
	}

	// Load file | Works with PCD and PLY files
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

	if (file_is_pcd) {
		if (pcl::io::loadPCDFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}
	else {
		if (pcl::io::loadPLYFile(argv[filenames[0]], *cloud) < 0) {
			std::cout << "Error loading point cloud " << argv[filenames[0]] << std::endl << std::endl;
			showHelp(argv[0]);
			return -1;
		}
	}//load cloud
	//-----------------------------------------------	
	pcl::console::print_highlight("load cloud\n");

	pcl::visualization::PCLVisualizer viewer;

pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> green(cloud, 0, 255, 0);
	viewer.addPointCloud(cloud, green, "cloud");

	// Add point picking callback to viewer:
	struct callback_args cb_args;
	cb_args.orgin_points = cloud;
	pcl::PointCloud<pcl::PointXYZ>::Ptr chosed_points_3d(new pcl::PointCloud<pcl::PointXYZ>);
	cb_args.chosed_points_3d = chosed_points_3d;
	cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(&viewer);
	viewer.registerAreaPickingCallback(ap_callback, (void*)&cb_args);
	pcl::console::print_highlight("press x enter slected model, then press 'Q'...\n");

	// Spin until 'Q' is pressed:
	viewer.spin();

	system("pause");//不注释,命令行会提示按任意键继续,注释会直接跳出
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值