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

Get average distance from a depth Rect image

asked 2014-07-15 22:57:23 -0500

Thiagopj gravatar image

updated 2014-07-16 09:21:41 -0500

I'm trying to get access to the average distance of some part of an image.

What i've done : Get image from ROS, conver to to OPENCV image, save depth image, crop the image using cv::Rect. So, i get the full depth image and the cropped one. When I access one pixel, like (240,320) i get the distance in meters. But now i need the distance from the rect image, and for this i'd like to get the average distance from all the pixels.

I tried to use cv::Scalar mean = cv::mean(ROI) passing the cropped image and then use mean[0], but it shows random numbers. I also tried mean.val[0]. '0' is because the others 3 channels is (i guess) for bgr and the image is grayscale.

Anyway, is there another way to get this average or am i doing it wrong ? I could do two loops for this problem, but i want to use opencv funtions.

Thank you!

EDIT [ code ]

  cv_bridge::CvImagePtr Dest = cv_bridge::toCvCopy(msg);
  ROS_INFO("Distance from center: %f", Dest-><float>(240,320));
  Dest->image.convertTo(depth16S, CV_16S, 30);
  cv::imwrite("Depth_image.png", depth16S);

ROI = depth16S(cv::Rect(140,220,200,200));
cv::Scalar mean = cv::mean(ROI);
ROS_INFO("MEAN: %f", mean.val[0]);

EDIT 2 [ solution ]

I realized that the distance information are in 'Dest' variable, not in depth16S. So, the solution is to crop the Dest and calculate the average using mean function.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-07-16 02:03:34 -0500

Wolf gravatar image

In principle what you've done should work. Did you create the ROI matrix something like:

cv::Mat roi( depth_image, cv::Rect( left, top, width, height ) );

Also you can apply the [] operator directly to the cv::Scalar like mean[0] (don't know if this makes any difference)

edit flag offensive delete link more


Thank you for the answer! Can you please see my Edit ? Maybe i'm doing something wrong. I declared depth16S and ROI as a global cv::Mat variable. The first ROS_INFO shows me the distance (meters). The second gives me random numbers.

Thiagopj gravatar image Thiagopj  ( 2014-07-16 07:14:15 -0500 )edit

Question Tools

1 follower


Asked: 2014-07-15 22:57:23 -0500

Seen: 1,299 times

Last updated: Jul 16 '14