我在 C++ 中使用 opencv 库,我正在尝试计算 vector<Point2f> difference
中包含的点的总和
Point 类具有 x 属性,即 float
.
float pointSumX(Point2f pt1,Point2f pt2)
{
return (pt1.x + pt2.x);
}
我定义了上面的函数,并从如下所示的 accumulate 中调用它。但它会引发错误。
float avgMotionX = accumulate(difference.begin(),difference.end(),0,pointSumX);
错误是:
error: could not convert ‘__init’ from ‘int’ to ‘cv::Point_’ __init = __binary_op(__init, *__first);
注意:我使用的是 C++11
最佳答案
float pointSumX(Point2f pt1, Point2f pt2)
应该是
float pointSumX(float lhs, const Point2f& rhs)
{
return lhs + rhs.x;
}
因为 lhs
是累加器。
另请注意,您应该调用它
std::accumulate(difference.begin(), difference.end(), 0.f, pointSumX); // 0.f instead of 0
返回 float
而不是 int
。
关于c++ - 与自定义对象一起使用 accumulate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39860788/