美文网首页
PCLcallbackPoint

PCLcallbackPoint

作者: 天天向上xz | 来源:发表于2021-04-08 19:39 被阅读0次

    Installation

    • ubuntu18.04 install PCL

    Code

    main.cpp

    #include <string>
    #include <pcl/io/pcd_io.h>
    #include <pcl/point_cloud.h>
    #include <pcl/point_types.h>
    #include <pcl/visualization/pcl_visualizer.h>
     
    using PointT = pcl::PointXYZRGB;
    using PointCloudT = pcl::PointCloud<PointT>;
     
    // 用于将参数传递给回调函数的结构体
    struct CallbackArgs {
        PointCloudT::Ptr clicked_points_3d;
        pcl::visualization::PCLVisualizer::Ptr viewerPtr;
    };
     
    void pickPointCallback(const pcl::visualization::PointPickingEvent &event, void *args) {
        CallbackArgs *data = (CallbackArgs *) args;
        if (event.getPointIndex() == -1)
            return;
        PointT current_point;
        event.getPoint(current_point.x, current_point.y, current_point.z);
        data->clicked_points_3d->points.push_back(current_point);
        // 绘制红色点
        pcl::visualization::PointCloudColorHandlerCustom<PointT> 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() {
    
        std::string file_name("point_cloud_PCD_23685214_1080_06-04-2021-16-34-45.pcd");
    
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
        // pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
        pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("viewer"));
        
        // 加载点云
        if (pcl::io::loadPCDFile(file_name, *cloud) == -1) {
            std::cerr << "could not load file: " << file_name << std::endl;
        }
        std::cout << "size: " << cloud->points.size() << std::endl;
    
        // 显示点云
    
        // 显示为热力图 打开pcl::PointCloud<pcl::PointXYZ>...
        // pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> fildColor(cloud, "z");
        // viewer->addPointCloud(cloud, fildColor, "cloud");
    
        // 显示原rgb数据
        pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
        viewer->addPointCloud(cloud, "cloud");
    
        // 显示坐标轴方向,X(红色)Y(绿色 )Z (蓝色)
        viewer->addCoordinateSystem(1.0);
    
        // 通过设置照相机参数使得从默认的角度和方向观察点云
        // viewer->initCameraParameters ();
    
        // viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);
        
        // 添加点拾取回调函数
        CallbackArgs  cb_args;
        PointCloudT::Ptr clicked_points_3d(new PointCloudT);
        cb_args.clicked_points_3d = clicked_points_3d;
        cb_args.viewerPtr = pcl::visualization::PCLVisualizer::Ptr(viewer);
        viewer->registerPointPickingCallback(pickPointCallback, (void*)&cb_args);
        std::cout << "Shift+click on three floor points, then press 'Q'..." << std::endl;
     
        viewer->spin();
        std::cout << "done." << std::endl;
     
        while (!viewer->wasStopped())
        {
            viewer->spinOnce(100);
        }
        return 0;
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)
    
    # Enable C++11
    set(CMAKE_CXX_FLAGS "-std=c++11")
    set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
    
    # Define project name
    project(PclCallbackPoint)
    
    # PCL
    find_package(PCL REQUIRED)
    # Find OpenCV
    find_package(OpenCV REQUIRED)
    
    # Add includes
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    include_directories( ${PCL_INCLUDE_DIRS} ) 
    
    link_directories(${PCL_LIBRARY_DIRS})
    add_definitions(${PCL_DEFINITIONS})
    
    # Declare the executable target built from your sources
    add_executable(PclCallbackPoint main.cpp)
    
    # Link your application with other libraries
    target_link_libraries(PclCallbackPoint ${PCL_LIBRARIES} ${OpenCV_LIBS})
    
    

    Use

    mkdir build
    cd build
    cmake ..
    make 
    
    # 有point_cloud_PCD_23685214_1080_06-04-2021-16-34-45.pcd的目录执行
    cd ..
    ./build/PclCallbackPoint
    

    参考

    相关文章

      网友评论

          本文标题:PCLcallbackPoint

          本文链接:https://www.haomeiwen.com/subject/npwvkltx.html