OpenCV 2中心 [英] OpenCV 2 Centroid

查看:219
本文介绍了OpenCV 2中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一个轮廓的质心,但我无法实现C ++(OpenCV 2.3.1)中的示例代码。

解决方案

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



检查这些时刻函数(中心和空间时刻)。



下面的代码取自OpenCV 2.3 docs教程。 此处填写完整代码






  ///查找轮廓
findContours(canny_output,contour, ,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE,Point(0,0));

///获取时刻
矢量< Moments> mu(contours.size());
for(int i = 0; i {mu [i] = moments(contours [i],false); }

///获得质心:
矢量< Point2f> mc(contoururs.size());
for(int i = 0; i {mc [i] = Point2f(mu [i]·m10 / mu [i]·m00, i] .m01 / mu [i] .m00); }






此外检查这个SOF ,虽然它是在Python中,它将是有用的。它找到轮廓的所有参数。


I am trying to find the centroid of a contour but am having trouble implementing the example code in C++ (OpenCV 2.3.1). Can anyone help me out?

解决方案

To find the centroid of a contour, you can use the method of moments. And functions are implemented OpenCV.

Check out these moments function (central and spatial moments).

Below code is taken from OpenCV 2.3 docs tutorial. 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 ); } 


Also check out this SOF, although it is in Python, it would be useful. It finds all parameters of a contour.

这篇关于OpenCV 2中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆