从下面的代码中,我可以画出最大的轮廓,质心标记为一个小圆圈,船体为黄色线条 . 如何绘制凸性缺陷?我应该使用circle()函数还是drawContours()函数?
Mat bw;
Mat canny_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours( bw, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
int s = getBiggestContour(contours);
Mat drawing = Mat::zeros( src.size(), CV_8UC3 ); //UC1
Point2f mc = getCentroidPoint(contours[s]);
drawContours( drawing, contours, s, Scalar(255,255,255), -1, 8, hierarchy, 0, Point() );
circle( drawing, mc, 4, Scalar(0,0,255), 1, 8, 0 );
vector<vector<Point> >hull( contours[s].size() );
convexHull( Mat(contours[s]), hull[s], false );
drawContours( drawing, hull, s, Scalar(0,255,255), 1, 8, vector<Vec4i>(), 0, Point() );
上面的代码可以工作,但是只有一个轮廓可以使用,这是最大的轮廓,所以我认为使用vector> for hull太多了 . 我该如何简化?
下面的代码来自另一个stackoverflow问题,但它没有说明如何使用缺陷变量将凸起缺陷绘制到Mat图像上 . 怎么能实现这一目标?
vector<vector<int> > hullsI(contours.size());
vector<vector<Point> > hullsP(contours.size());
vector<vector<Vec4i> > defects(contours.size());
for(int i = 0; i <contours.size(); ++i){
//find the hulls
convexHull(contours[i], hullsI[i], false, false);
convexHull(contours[i], hullsP[i], false, true);
//find the defects
if (contours[i].size() >3 ){
convexityDefects(contours[i], hullsI[i], defects[i]);
}
}
我不想使用IplImage . 我更喜欢Mat .
1 回答
您可以使用'cvDrawContours()'绘制凸包操作的结果,但是您需要设置正确的参数才能实现 . 我有一个例子,但它使用'cvConvexHull2()'和IplImages但它应该以相同的方式用于Mat和其他的convexHull操作: