Calculate mean for vector of points

后端 未结 4 2130
甜味超标
甜味超标 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-09 00:31

    InputArray does a good job here. You can simply call

    cv::Mat mean_;
    cv::reduce(points, mean_, 01, CV_REDUCE_AVG);
    // convert from Mat to Point - there may be even a simpler conversion, 
    // but I do not know about it.
    cv::Point2f mean(mean_.at(0,0), mean_.at(0,1)); 
    

    Details:

    In the newer OpenCV versions, the InputArray data type is introduced. This way, one can send as parameters to an OpenCV function either matrices (cv::Mat) either vectors. A vector will be interpreted as a float matrix with three channels, one row, and the number of columns equal to the vector size. Because no data is copied, this transparent conversion is very fast.

    The advantage is that you can work with whatever data type fits better in your app, while you can still use OpenCV functions to ease mathematical operations on it.

提交回复
热议问题