ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

calculate depth from point cloud xyz

asked 2014-03-19 02:21:10 -0600

Tal gravatar image

From a single point in PointCloud2 ('sensor_msgs/PointCloud2), a value of x,y,z. I want to know how to calculate the depth to that point.
For example from point (57,341)
I get (x,y,z)=(-0.6895000338554382, 0.2666066884994507, 1.3790000677108765)
How i calculate the depth from this information ? It is only z value? what the meaning of negative at x?

Thanks for the help.

edit retag flag offensive close merge delete

Comments

How did you get the value of x,y,z from PointCloud2?

DragonZ gravatar image DragonZ  ( 2021-12-09 09:36:11 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-03-19 02:48:19 -0600

fivef gravatar image

The x,y,z values of a point cloud point give the position relative to the frame defined in the message header. E.g. the Kinect frame. If you want to calculate the distance between your camera's lens and a specific point you only need to determine the length of the point's vector. vec_length = square_root(x^2 + y^2 + z^2).

edit flag offensive delete link more

Comments

thank you!

Tal gravatar image Tal  ( 2014-03-19 03:40:12 -0600 )edit
1

answered 2021-06-25 00:49:52 -0600

askkvn gravatar image

updated 2021-06-25 01:13:30 -0600

The below code:

  • Subscribed to /camera/depth/image_raw rostopic,
  • Convert ROS image to OpenCV image
  • Get image dimension
  • Get global max and min value,
  • Get depth value at specific point,

Code:

#include <iostream>
#include <ros/ros.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/image_encodings.h>
#include <cv_bridge/cv_bridge.h>

#include <opencv2/highgui/highgui.hpp>

using namespace std;

static const uint32_t MY_ROS_QUEUE_SIZE = 1000;

void imgcb(const sensor_msgs::Image::ConstPtr& msg)
{
    try {
        cv_bridge::CvImageConstPtr cv_ptr;
        cv_ptr = cv_bridge::toCvShare(msg);

        //get image dimension once
        static bool runOnce = true;
        if (runOnce){
            cout << "Image dimension (Row,Col): " << cv_ptr->image.rows << " x " << cv_ptr->image.cols << endl;
            runOnce = false;
        }

        //get global max depth value
        double max = 0.0;
        cv::minMaxLoc(cv_ptr->image, 0, &max, 0, 0);
        std::cout << "Max value: " << max << endl;

        //get global min depth value
        double min = 0.0;
        cv::minMaxLoc(cv_ptr->image, &min, &max, 0, 0);
        std::cout << "Min value: " << min << endl;

        //get depth value at a point
        float distanceVal = cv_ptr->image.at<float>(100, 100);
        std::cout << "Distance value: " << distanceVal << "m" << endl;

    } catch (const cv_bridge::Exception& e) {
        ROS_ERROR("cv_bridge exception: %s", e.what());
    }
}

int main(int argc, char* argv[])
{
    ros::init(argc, argv, "foo");

    std::cout << "Getting Image depth value!" << std::endl;

    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("camera/depth/image_raw", MY_ROS_QUEUE_SIZE, imgcb);

    ros::spin();

    std::cout << "Done" << std::endl;

    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-03-19 02:21:10 -0600

Seen: 3,536 times

Last updated: Jun 25 '21