1.提取轮廓区域
if(img.channels()==3)
{
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
}
//二值化
cv::threshold(img,img,127,255,CV_THRESH_BINARY);
//2.轮廓提取
std::vector<std::vector<cv::Point>> contours_out; //所有轮廓
std::vector<cv::Vec4i> hierarchy;
cv::findContours(img, contours_out, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
// cv::drawContours(drawcon,contours_out,-1, cv::Scalar(0,255,0), CV_FILLED, 8);
// cv::imwrite("conto.jpg",drawcon); //输出轮廓提取图
对于提取到的轮廓区域,寻找质心
//图像中心Center(x0, y0)=(m10/m00,m01/m00)
cv::Moments moment = cv::moments(contours_out.at(index));
cv::Point midPoint= cv::Point(int(moment.m10/moment.m00),int(moment.m01/moment.m00));