Calculate mean for vector of points

后端 未结 4 2131
甜味超标
甜味超标 2021-02-09 00:16

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

4条回答
  •  孤独总比滥情好
    2021-02-09 00:48

    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.

提交回复
热议问题