矢量中的最高值&lt; vector <point3f>&gt;

时间:2017-05-10 15:12:11

标签: c++ opencv c++11 vector

我知道如何找到数据类型的最高值,例如intfloatdouble等。但在这里,我正在使用x,{ {1}},&amp; y使用z进行坐标。那么有人可以帮助Point3f x y找到zstd::vectorstd::vector的最高值吗?

std::vector< std::vector<Point3f> > cluster_points;

为简单起见,我想说只想找到x轴中的最高值。

4 个答案:

答案 0 :(得分:2)

我不知道opencv,所以可以采用更简单的解决方案,但......

几个具有几个lambda函数的std::for_each()呢?

std::vector<std::vector<Point3f>> vvp { /* some data */ };

auto mx = vvp[0][0].x;
auto my = vvp[0][0].y;
auto mz = vvp[0][0].z;

std::for_each(vvp.cbegin(), vvp.cend(),
   [&](std::vector<Point3f> const & vp)
       { std::for_each(vp.cbegin(), vp.cend(),
            [&](Point3f const & p)
               { mx = std::max(mx, p.x);
                 my = std::max(my, p.y);
                 mz = std::max(mz, p.z); }); });

如果你可以使用C ++ 14,那么lambda函数带有auto个参数,双std::for_each()部分可以简单地写成

std::for_each(vvp.cbegin(), vvp.cend(), [&](auto const & vp)
 { std::for_each(vp.cbegin(), vp.cend(), [&](auto const & p)
    { mx = std::max(mx, p.x);
      my = std::max(my, p.y);
      mz = std::max(mz, p.z); }); });

所以没有明确Poinf3f并且可以被其他类似3d的类型使用。

答案 1 :(得分:1)

这是C ++ 14。

这是一个在客户端代码中没有显式循环的解决方案。

template<class F>
auto foreacher(F&& f) {
  return [f=std::forward<F>(f)](auto&& r)mutable{
    for (auto&& e:decltype(r)(r))
      f(decltype(e)(e));
  };
}

std::vector<std::vector<Point3f>> data = {whatever};
auto mx = data[0][0].x;
auto task = foreacher(foreacher([&](Point3f const& e){
  mx = (std::max)(mx, e.x);
}));

task(data);

我们使用我们的lambda来解决元素上的问题,然后我们将它包装在两个修饰符中,使它遍历参数的内容。

Live example

答案 2 :(得分:1)

您可以使用std::accumulate很好地映射到此任务:

const auto min = std::numeric_limits<float>::min();
Point3f init( min, min, min );
std::vector< std::vector<Point3f> > cluster_points;
auto max = std::accumulate( cluster_points.begin(), cluster_points.end(), init, []( const Point3f &p, const std::vector<Point3f> &v ) {
    return std::accumulate( v.begin(), v.end(), p, []( const Point3f &p1, const Point3f &p2 ) {
       return Point3f( std::max( p1.x, p2.x ), std::max( p1.y, p2.y ), std::max( p1.z, p2.z ) );
   }
} ) );

这只需要C ++ 11,使用C ++ 14可以通过在lambdas中使用auto参数来简化它

答案 3 :(得分:1)

虽然有几个很好的答案,但我只是写了一个 pure opencv 的答案,我会以最快的方式给你调查。

std::vector<Point3f> points;
// .. fill the array
Mat pointsMat = Mat(points).reshape(1); 

Quoting - 结果我们得到一个32FC1矩阵,有3列而不是32FC3矩阵,有1列。 pointsMat使用来自点的数据,并且在销毁时不会释放内存。但是,在这个特定的实例中,开发人员必须确保点的生命周期长于pointsMat。

然后,既然您拥有了所有Point3f的席子,就可以使用以下内容:

minMaxLoc(pointsMat, &minVal, &maxVal);

如果您希望std::vector<std::vector<Point3f>> All_points执行此操作,则可以制作一个列数为Mat的单个频道All_points.size() * 3,并使用相同的函数minMaxLoc。这将为您提供所有点集的minValmaxVal

您还可以获取minValmaxVal的位置:

minMaxLoc(pointsMat, &minVal, &maxVal, &minLoc, &maxLoc);

当然是重塑Mat

希望它有所帮助!

P.S。对C ++ 11和C ++ 14的回答

相关问题