I have a vector of a 2-dimensional points in OpenCV
std::vector points;
I would like to calculate the mean values for x and
Since OpenCV's Point_
already defines operator+
, this should be fairly simple. First we sum the values:
cv::Point2f zero(0.0f, 0.0f);
cv::Point2f sum = std::accumulate(points.begin(), points.end(), zero);
Then we divide to get the average:
Point2f mean_point(sum.x / points.size(), sum.y / points.size());
...or we could use Point_
's operator*
:
Point2f mean_point(sum * (1.0f / points.size()));
Unfortunately, at least as far as I can see, Point_
doesn't define operator /
, so we need to multiply by the inverse instead of dividing by the size.