Point cloud using pcl visualizer

163 Views Asked by At

I have a pointcloud data which is a ply file. Using pcl library I tried visualising the data. But when trying to do a click event I'm getting the coordinates(435,234). My doubt is what will be unit of (435,234) as I'm trying to do a click event on the screen using pcl.

I have a doubt if it can be a pixel coordinates.Since it is a ply file visualisation using pcl, I want to know the unit or metric behind it.

#include <iostream>
#include <pcl/io/ply_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
#include <thread>
#include <chrono>
#include <pcl/filters/statistical_outlier_removal.h>
#include <Eigen/Core> 
#include <cmath> 


pcl::PointXYZRGB clicked_points[2];
int num_clicked = 0;

struct Point {
    double x;
    double y;
};

void mouseEventOccurred(const pcl::visualization::MouseEvent& event, void* viewer_void)
{
    pcl::visualization::PCLVisualizer* viewer = static_cast<pcl::visualization::PCLVisualizer*>(viewer_void);
    if (event.getButton() == pcl::visualization::MouseEvent::LeftButton && event.getType() == pcl::visualization::MouseEvent::MouseButtonRelease)
    {
        clicked_points[num_clicked % 2].x = event.getX();
        clicked_points[num_clicked % 2].y = event.getY();
        std::cout << "point_clicked->(" << clicked_points[num_clicked % 2].x << "," << clicked_points[num_clicked % 2].y << ")" << std::endl;
        num_clicked++;

        if (num_clicked % 2 == 0)//calculate distance for every pair of two clicked points
        {
            double distance = std::sqrt(std::pow(clicked_points[1].x - clicked_points[0].x, 2) + std::pow(clicked_points[1].y - clicked_points[0].y, 2));
            std::cout << "Distance between clicked points->" << distance << std::endl;
        }
    }
}

int main()
{
    pcl::console::setVerbosityLevel(pcl::console::L_ERROR);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::io::loadPLYFile("humanoid.ply", *cloud);
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZRGB>);
    pcl::visualization::PCLVisualizer viewer("Point_Cloud");
    viewer.setBackgroundColor(0.0, 0.0, 0.0);
    viewer.addPointCloud<pcl::PointXYZRGB>(cloud_filtered, pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB>(cloud_filtered),"filtered_cloud");
    viewer.registerMouseCallback(mouseEventOccurred, (void*)&viewer);

    while (!viewer.wasStopped()) {
        viewer.spinOnce(100, true);
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    return 0;
}

This is the code. Here I have to perform distance so when trying to find it by using click event I have a doubt in metric as the clicked coordinate is 2D.

Also in my case I have to find distance of a curved surface of a given object So is there a way to map the 2d point I click on screen with the original 3D data of the clicked part.

0

There are 0 best solutions below