Reference 1:http://m.blog.csdn.net/article/details?id=51338298
The box selection vertex refers to article 1, and all of the code in this article is as follows:
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
using namespace Std;
PCL::P OINTCLOUD<PCL::P ointxyz>::P TR Cloud (New PCL::P OINTCLOUD<PCL::P ointxyz> ());
Boost::shared_ptr<pcl::visualization::P clvisualizer> Viewer (new pcl::visualization::P clvisualizer ("Viewer" ));
PCL::P OINTCLOUD<PCL::P ointxyz>::P tr clicked_points_3d (new PCL::P OINTCLOUD<PCL::P ointxyz>);
int num = 0;
std::vector< int> totalindices;
BOOL Myfind (int temp1,const vector<int>& temp)
{
for (int i = 0; i < temp.size (); i++)
{
if (Temp1 = = Temp[i])
return true;
}
return false;
}
void Pp_callback (const pcl::visualization::areapickingevent& event, void* args)
{
std::vector< int > indices;
if (event.getpointsindices (indices) = =-1)
Return
for (int i = 0; i < indices.size (); ++i)
{
Clicked_points_3d->points.push_back (cloud->points.at (indices[i));
Totalindices.push_back (Indices[i]);
}
Pcl::visualization::P ointcloudcolorhandlercustom<pcl::P ointxyz> Red (clicked_points_3d, 255, 0, 0)
Std::stringstream SS;
Std::string Cloudname;
SS << num++;
SS >> Cloudname;
Cloudname + = "_cloudname";
Viewer->addpointcloud (clicked_points_3d, Red, cloudname);
Viewer->setpointcloudrenderingproperties (pcl::visualization::P cl_visualizer_point_size, CloudName);
}
void Main ()
{
if (Pcl::io::loadpcdfile ("ISM_TRAIN_WOLF.PCD", *cloud))
{
Std::cerr << "Error:cannot Open File" << Std::endl;
Return
}
Viewer->addpointcloud (Cloud, "test");
Viewer->setcameraposition (0, 0,-2, 0,-1, 0, 0);
Viewer->registerareapickingcallback (Pp_callback, (void*) &cloud);
int i = 0;
while (!viewer->wasstopped ())
{
if (I < 60)
{
Viewer->spinonce (100);
Boost::this_thread::sleep (boost::p osix_time::microseconds (100000));
i++;
Print ();
}
Else
Break
Print ();
}
The point cloud for the ② box (to be removed) exists in another window Viewer2:
Boost::shared_ptr<pcl::visualization::P clvisualizer> viewer2 (new pcl::visualization::P Clvisualizer (" Viewer2 "));
PCL::P OINTCLOUD<PCL::P ointxyz>::P tr Outcloud (new PCL::P OINTCLOUD<PCL::P ointxyz> ());
Pcl::copypointcloud (*cloud, totalindices, *outcloud);
Viewer2->addpointcloud (Outcloud, "test2");
Viewer2->setcameraposition (0, 0,-2, 0,-1, 0, 0);
while (!viewer2->wasstopped ())
//{
Viewer2->spinonce (100);
Boost::this_thread::sleep (boost::p osix_time::microseconds (100000));
//}
The remaining point cloud in the ③ after removal is in another window Viewer3:
vector<int>finalindices;
for (int i = 0; i < 3400; i++)
{
if (Myfind (i, totalindices) = = False)
Finalindices.push_back (i);
}
Boost::shared_ptr<pcl::visualization::P clvisualizer> viewer3 (new pcl::visualization::P Clvisualizer (" Viewer3 "));
PCL::P OINTCLOUD<PCL::P ointxyz>::P tr Finalcloud (new PCL::P OINTCLOUD<PCL::P ointxyz> ());
Pcl::copypointcloud (*cloud, finalindices, *finalcloud);
Viewer3->addpointcloud (Finalcloud, "test3");
Viewer3->setcameraposition (0, 0,-2, 0,-1, 0, 0);
while (!viewer3->wasstopped ())
{
Viewer3->spinonce (100);
Boost::this_thread::sleep (boost::p osix_time::microseconds (100000));
}
}
In Window 1, first press the X key to start (/end) the box selection mode. (Press the R key to move the point of view to the center of the object (Wolf)) and then select the box to delete (shown in red). After the selection, in the Window 3 show the remaining points, the effect as shown (delete the wolf's tail):
On the basis of this article, it is easy to continue to refine other functions (such as box selection after the other key to delete the box selection, add the S key to save to PCD, etc.).