c++ - OpenCV3 - 访问标签质心

标签 c++ opencv

OpenCV3 有很多很酷的东西。我使用 connectedComponentsWithStats 访问统计数据数组进行了一些过滤,但有人可以告诉我如何访问每个标签的质心吗?

文档说这也是可能的,但我不明白。

Mat stats, centroids, labelImage;
int nLabels = connectedComponentsWithStats(input, labelImage, stats, centroids, connectivity);

最佳答案

centroids是一个 double 矩阵,有两列 (x, y),行数等于标签数。

您可以通过以下方式访问它:

Mat1i labels;
Mat1i stats;
Mat1d centroids;
int n_labels = connectedComponentsWithStats(img, labels, stats, centroids);

for (int i = 0; i < centroids.rows; ++i)
{
    cout << "x: " << centroids(i, 0) << " y: " << centroids(i, 1) << endl;
    circle(outputImage, Point(centroids(i, 0), centroids(i, 1)), 3, Scalar(0,255,0));
}

如果您声明 centroidsMat ,像这样访问它:centroids.at<double>(i,0)

关于c++ - OpenCV3 - 访问标签质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34321567/

相关文章:

c++ - 在 Linux 上用 C++ 实现线程安全的通用堆栈

arrays - cv.Save 和 cv.Load(数组)

c++ - cv::内存位置出现异常错误

c++ - Boost 是否提供打印 uBLAS 数据类型的工具?

java - 我可以通过字符串编译 C++ 源代码吗?

c++ - c++中双括号 "[[foo()]] type name;"语法的含义?

c++ - C++98 中的容器初始化

python - 导入 cv2 : ImportError: DLL load failed: windows 7 Anaconda 4. 3.0(64 位)Python 3.6.0

c++ - 为 OpenCV 编译 MATLAB 绑定(bind)

android - Eclipse 错误中适用于 Android 的 OpenCV 库