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[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->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[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.