c++ - `` const Mat* 样本 `` param in OpenCV' s `` calcCovarMatrix`` 函数的工作原理?

标签 c++ opencv opencv3.0

我试图在 OpenCV 中计算多个矩阵的协方差矩阵,看到了 calcCovarMatrix 的两个版本.我很好奇,想使用将 const Mat* samples, int nsamples 作为前 2 个参数的重载版本。

问题:samples 参数是什么?它是指向 Mats vector 的第一项的指针吗?为什么它本身不是 vector ?传递给它的是什么/参数如何工作?

P.S.:我不想使用该函数的其他重载版本!我想了解我询问的版本中使用的实际代码。

最佳答案

我确信 OpenCV 的作者更喜欢 const Mat*, int 对参数而不是 std::vector 因为这样更灵活。

想象一个必须处理一系列特定对象的函数。

在 C++ 中,一系列对象可以用 std::vector 存储。但是,如果该对象的系列是静态常量,即可以在编译时定义呢?该对象的普通旧 C 数组也可以完成这项工作。

可以处理这样一系列对象的函数可以接受 const std::vector&。如果应用于 C 数组,则必须构建一个临时 vector 实例。 C++ 代码相对简单,但它在胃中留下了一种反胃的感觉,因为必须将数组内容复制到临时 std::vector 实例中才能将其传递给函数。

相反的情况:该函数接受一个指向起始对象的指针和一个计数(就像在 C 中一样)。这样的函数可以应用于 C 数组以及 std::vector 因为 std::vector 提供了一个 data()方法,它提供一个指向其第一个元素的常量指针和一个 size() 方法。此外, vector 元素被认为是连续存储的,就像在 C 数组中一样。

所以,我的简单示例:

#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>

// Pi (from Windows 7 calculator)
const float Pi = 3.1415926535897932384626433832795;

struct Point {
  float x, y;
};

std::ostream& operator<<(std::ostream &out, const Point &point)
{
  return out << '(' << point.x << ", " << point.y << ')';
}

Point average(const Point *points, size_t size)
{
  assert(size > 0);
  Point sum = points[0];
  for (size_t i = 1; i < size; ++i) {
    sum.x += points[i].x; sum.y += points[i].y;
  }
  return { sum.x / (unsigned)size, sum.y / (unsigned)size };
}

static const Point square[] = {
  { -0.5f, -0.5f },
  { +0.5f, -0.5f },
  { +0.5f, +0.5f },
  { -0.5f, +0.5f }
};
static const size_t sizeSquare = sizeof square / sizeof *square;

int main()
{
  // process points of a static const square (using average() with an array)
  std::cout << "CoG of " << sizeSquare << " points of square: "
    << average(square, sizeSquare) << '\n';
  // build a tesselated circle
  std::vector<Point> circle;
  const unsigned n = 16;
  for (unsigned i = 0; i < n; ++i) {
    const float angle = i * 2 * Pi / n;
    circle.push_back({ std::sin(angle), std::cos(angle) });
  }
  // process points of that circle (using average() with a vector)
  std::cout << "CoG of " << circle.size() << " points of circle: "
    << average(circle.data(), circle.size()) << '\n';
  // done
  return 0;
}

输出:

CoG of 4 points of square: (0, 0)
CoG of 16 points of circle: (-5.58794e-09, 4.47035e-08)

Live Demo on coliru

为方便起见,可以为 std::vector 添加以下替代定义:

static inline Point average(const std::vector<Point> &points)
{
  return average(points.data(), points.size());
}

一个通用的解决方案将提供一个替代方案,它有两个迭代器,可以应用于任何容器。 (C++ 标准库中有很多这方面的例子。)

我只能假设 OpenCV 作者关注的是性能而不是灵 active (但这只是我个人的猜测)。

关于c++ - `` const Mat* 样本 `` param in OpenCV' s `` calcCovarMatrix`` 函数的工作原理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55407895/

相关文章:

c++ - 在 opencv2/3 中清除图像的最有效方法

opencv - OpenCV:检索轮廓中心的颜色

c - 我的 Opencv 应用程序处理速度非常慢

opencv3.0 - 在OPENCV 3.0中使用solvePnP函数时

opencv - Opencv使白色像素比图像中的其他像素更亮,以便在阈值设置后显示

c++ - 序列化部分工作 - boost::asio

c++ - C++ 中的循环依赖

c++ - 创建返回结构数组的 API 函数

c++ - opencv undefined 我的库有问题吗?

c++ - OpenCV Cuda 不是有类似于 findContours 的函数吗?