Opencv求轮廓的中心点坐标
思路:
1.通过findContours找出图片中的轮廓;
2.minAreaRect找到最小外接矩形;
3.得到最小外接矩形的中心点坐标作为轮廓的中心坐标;
cv::Mat Imsrc,Imdest;
std::vector<std::vector<cv::Point>> contoursDest;
std::vector<cv::Vec4i> hierarchy;
cv::threshold(Imsrc, Imdest, 200, 255, cv::/*THRESH_BINARY*/THRESH_BINARY_INV);//THRESH_BINARY_INV不检测图片画布轮廓
cv::findContours(Imdest, contoursDest, hierarchy, cv::RETR_EXTERNAL/* RETR_TREE*/, cv::CHAIN_APPROX_SIMPLE);//RETR_EXTERNAL,内轮廓不重复计算
for (int c = 0; c < contoursDest.size(); ++c)
{
cv::RotatedRect rotateRect = cv::minAreaRect(contoursDest[c]);//轮廓最小外接矩形
QPoint pnt = QPoint(rotateRect.center.x, rotateRect.center.y);//最小外接矩形的中心点坐标
//画出最小外接矩形
cv::Point2f rect_points[4];
rotateRect.points(rect_points);
for (int i = 0; i < 4; i++)
{
line(img, rect_points[j], rect_points[(j + 1) % 4], Scalar(0, 255, 0), 2);
}
}