10

I have a vector of a 2-dimensional points in OpenCV

std::vector<cv::Point2f> points;

I would like to calculate the mean values for x and y coordinates in points. Something like:

cv::Point2f mean_point; //will contain mean values for x and y coordinates
mean_point = some_function(points); 

This would be simple in Matlab. But I'm not sure if I can utilize some high level OpenCV functions to accomplish the same. Any suggestions?

Alexey
  • 5,898
  • 9
  • 44
  • 81

4 Answers4

15

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<float>(0,0), mean_.at<float>(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<Vec3f> 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.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
Sam
  • 19,708
  • 4
  • 59
  • 82
  • Thanks! However, conversion from Mat to Point using `cv::Point2f mean(mean_(0), mean_(1));` didn't work. – Alexey Jul 19 '12 at 19:31
  • This one should: `cv::Point2f mean(mean_.at(0), mean_.at(1));` – Sam Jul 19 '12 at 19:54
  • tested, it works. double checked `cv:reduce(...)` parameters - should be `cv::reduce(points, mean_, CV_REDUCE_AVG, 1);`, thx. – Alexey Jul 19 '12 at 20:28
  • 1
    Maybe the definition has changed, but currently reduce is `void reduce(InputArray src, OutputArray dst, int dim, int rtype, int dtype=-1 )` http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#reduce - rtype comes after dim so that should be `cv::reduce(points, mean_, 1, CV_REDUCE_AVG);` I think some other changes are necessary to make this run without exceptions I'll follow up with. – Lucas Walter Apr 09 '14 at 16:58
  • simpler way to convert from Mat to Point https://stackoverflow.com/questions/18523894/conversion-of-3x1-or-1x3-cvmat-to-cvpoint3d – ezekiel May 11 '21 at 04:52
11

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
6

You can use stl's std::accumulate as follows:

cv::Point2f sum = std::accumulate(
    points.begin(), points.end(), // Run from begin to end
    cv::Point2f(0.0f,0.0f),       // Initialize with a zero point
    std::plus<cv::Point2f>()      // Use addition for each point (default)
);
cv::Point2f mean = sum / points.size(); // Divide by count to get mean
smocking
  • 3,689
  • 18
  • 22
0

Add them all up and divide by the total number of points.

jrad
  • 3,172
  • 3
  • 22
  • 24