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

# Get average distance from a depth Rect image

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, but it shows random numbers. I also tried mean.val. '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->image.at<float>(240,320));
Dest->image.convertTo(depth16S, CV_16S, 30);
cv::imwrite("Depth_image.png", depth16S);
//ROS_INFO("DEPTH_IMAGE SAVED");

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


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 close merge delete

Sort by » oldest newest most voted 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 (don't know if this makes any difference)

more