c++ - OpenCV 2质心

标签 c++ opencv image-processing centroid

我试图找到轮廓的质心,但在用 C++ (OpenCV 2.3.1) 实现示例代码时遇到了问题。谁能帮帮我?

最佳答案

要找到轮廓的质心,可以使用矩量法。并且函数是用 OpenCV 实现的。

查看这些矩函数 (central and spatial moments)。

以下代码摘自 OpenCV 2.3 文档教程。 Full code here.


/// Find contours
findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Get the moments
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
 { mu[i] = moments( contours[i], false ); }

///  Get the mass centers:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
 { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); } 

还有 check out this SOF ,虽然它是用 Python 编写的,但它会很有用。它找到轮廓的所有参数。

关于c++ - OpenCV 2质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9074202/

相关文章:

c++ - 观察者模式——传递信息给观察者

python - PyCharm 中的 OpenCV 代码 : Process finished with exit code 0

java - 加载太多 JPEG 文件(100 秒)导致内存不足错误

opencv - 使用 OpenCV 从 YUV 颜色空间转换为 RGB

java - 如何将图像转换为 postscript 形状

c++ - OpenCV:查找二进制 Mat 图像的所有非零坐标

c++ - 运算符重载 >> 和私有(private)成员

c++ - 如何将焦点设置在特定的小部件上

C++:尝试使用等效的 STL 算法消除原始循环

linux -/bin/sh : 1: arm-linux-gnueabihf-strip: not found