opencv 求连通区域的重心

主要步骤 用findContours获取连通域轮廓通过Moments对象获取轮廓所在重心findContours函数的讲解实现代码 #include#in

主要步骤

  • findContours获取连通域轮廓
  • 通过Moments对象获取轮廓所在重心
  • findContours函数的讲解

实现代码

#include
#include
using namespace std;
using namespace cv;int main()
{Mat src = imread("F:\\testdata\\test.jpg", 0);threshold(src, src, 0, 255, THRESH_OTSU);//获取图像轮廓vector>contours;	//每个轮廓中的点vectorhierarchy;			//轮廓的索引???	findContours(src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());for (int i = 0; i < contours.size(); ++i) {Mat tmp(contours.at(i));Moments moment=moments(tmp, false);if (moment.m00 != 0)//除数不能为0{int x = cvRound(moment.m10 / moment.m00);//计算重心横坐标int y = cvRound(moment.m01 /