PCL/VTK 手动捕获点云数据点(pcd/stl)

利用PCL/VTK来手动的捕获到pcd格式或者stl格式的点云数据。

捕获pcd的点云数据

#include     
#include     
#include     
#include     

typedef pcl::PointXYZRGBA PointT_XYZ;
typedef pcl::PointCloud PointCloudT_XYZ;


// Mutex: //    
boost::mutex cloud_mutex;

struct callback_args{
	// structure used to pass arguments to the callback function    
	PointCloudT_XYZ::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;
	std::cout << "Picking event active" << std::endl;
	PointT_XYZ current_point;
	if (event.getPointIndex() != -1)
	{
		float x, y, z;
		event.getPoint(current_point.x, current_point.y, current_point.z);
		//std::cout << x << ";" << y << ";" << z << std::endl;  
		data->clicked_points_3d->points.push_back(current_point);

	}
	// Draw clicked points in red:    
	pcl::visualization::PointCloudColorHandlerCustom 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;
}

void main()
{

	//visualizer    
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud());
	boost::shared_ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));

	pcl::io::loadPCDFile("jaw_2PCD.pcd", *cloud);

	//viewer->addPointCloud(cloud, "bunny");    

	cloud_mutex.lock();    // for not overwriting the point cloud    

	// Display pointcloud:    
	viewer->addPointCloud(cloud, "jaw_2");

	// Add point picking callback to viewer:    
	struct callback_args cb_args;
	PointCloudT_XYZ::Ptr clicked_points_3d(new PointCloudT_XYZ);
	cb_args.clicked_points_3d = clicked_points_3d;
	cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(viewer);
	viewer->registerPointPickingCallback(pp_callback, (void*)&cb_args);

	std::cout << "Shift+click on three floor points, then press 'Q'..." << std::endl;

	// Spin until 'Q' is pressed:    
	viewer->spin();
	std::cout << "done." << std::endl;

	cloud_mutex.unlock();

	while (!viewer->wasStopped())
	{
		viewer->spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
}

运行结果

PCL/VTK 手动捕获点云数据点(pcd/stl)_第1张图片


利用vtk直接对stl数据进行手动选点

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);

// PointPickerInteractorStyle
class PointPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
	static PointPickerInteractorStyle* New();
	vtkTypeMacro(PointPickerInteractorStyle, vtkInteractorStyleTrackballCamera);

	virtual void OnLeftButtonDown()
	{
		std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] << " " << this->Interactor->GetEventPosition()[1] << std::endl;
		this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],
			this->Interactor->GetEventPosition()[1],
			0,  // always zero.
			this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
		double picked[3];
		this->Interactor->GetPicker()->GetPickPosition(picked);
		std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl;

		vtkSmartPointer sphereSource =
			vtkSmartPointer::New();
		sphereSource->Update();

		vtkSmartPointer mapper =
			vtkSmartPointer::New();
		mapper->SetInputConnection(sphereSource->GetOutputPort());
		vtkSmartPointer actor = vtkSmartPointer::New();
		actor->SetMapper(mapper);
		actor->SetPosition(picked);
		actor->SetScale(0.8);//设置显示点的大小
		actor->GetProperty()->SetColor(1.0, 0.0, 0.0);
		this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()->AddActor(actor);

		vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
	}
};

vtkStandardNewMacro(PointPickerInteractorStyle);

int main(int, char *[])
{
	// Read a stl file.
	vtkSmartPointer input1 = vtkSmartPointer::New();
	vtkSmartPointer reader1 = vtkSmartPointer::New();
	reader1->SetFileName("test.stl");
	reader1->Update();
	input1->DeepCopy(reader1->GetOutput());

	// Create a mapper and actor
	vtkSmartPointer mapper =
		vtkSmartPointer::New();
	mapper->SetInputConnection(reader1->GetOutputPort());
	vtkSmartPointer actor =
		vtkSmartPointer::New();
	actor->SetMapper(mapper);

	// Create a renderer, render window, and interactor
	vtkSmartPointer renderer =
		vtkSmartPointer::New();
	vtkSmartPointer renderWindow =
		vtkSmartPointer::New();
	renderWindow->Render();
	renderWindow->SetWindowName("PointPicker");
	renderWindow->AddRenderer(renderer);

	vtkSmartPointer pointPicker =
		vtkSmartPointer::New();

	vtkSmartPointer renderWindowInteractor =
		vtkSmartPointer::New();
	renderWindowInteractor->SetPicker(pointPicker);
	renderWindowInteractor->SetRenderWindow(renderWindow);

	vtkSmartPointer style =
		vtkSmartPointer::New();
	renderWindowInteractor->SetInteractorStyle(style);

	// Add the actor to the scene
	renderer->AddActor(actor);
	renderer->SetBackground(1, 1, 1);

	// Render and interact
	renderWindow->Render();
	renderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

运行结果:

PCL/VTK 手动捕获点云数据点(pcd/stl)_第2张图片



你可能感兴趣的:(三维重建)