OpenCV 2 Centroid

kes*_*vdv 10 c++ opencv image-processing centroid

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

Abi*_*n K 16

要查找轮廓的质心,可以使用矩量法.并且功能是OpenCV实现的.

查看这些时刻功能(中心和空间时刻).

下面的代码来自OpenCV 2.3 docs教程.完整代码在这里.


/// 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 ); } 
Run Code Online (Sandbox Code Playgroud)

请查看此SOF,虽然它是在Python中,但它会很有用.它找到轮廓的所有参数.


Ale*_*xey 5

如果您有轮廓区域的蒙版,则可以按如下方式找到质心位置:

cv::Point computeCentroid(const cv::Mat &mask) {
    cv::Moments m = moments(mask, true);
    cv::Point center(m.m10/m.m00, m.m01/m.m00);
    return center;
}
Run Code Online (Sandbox Code Playgroud)

当一个人有面具而不是轮廓时,这种方法很有用.在那种情况下,上述方法在计算上比使用cv::findContours(...)然后找到质量中心更有效.

这是源头