tutorial_code\ShapeDescriptors\moments_demo.cpp
步骤:
1、灰度化
2、滤波
3、Canny边缘检测
4、画轮廓 findContours()
5、画中心距
第一步:vector
for( size_t i = 0; i < contours.size(); i++ )
{ mu[i] = moments( contours[i], false ); }
第二步:计算中心
vector
for( size_t i = 0; i < contours.size(); i++ )
{ mc[i] = Point2f( static_cast
第三步:绘制质心
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
第四步:计算面积和周长
for( size_t i = 0; i< contours.size(); i++ )
{
printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", (int)i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
}
结果图如下:
参考博客:
https://www.cnblogs.com/mikewolf2002/p/3427564.html