绘制凸度缺陷C OPENCV

Drawing Convexity Defects C++ OpenCV

本文关键字:OPENCV 缺陷 绘制      更新时间:2023-10-16

从下面的代码中,我能够用标记为小圆圈的质心画出最大的轮廓,将船体标记为黄线。如何绘制凸性缺陷?我应该使用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>用于船体的轮廓太多了。我该如何简化?

下面的代码来自另一个Stackoverflow问题,但并未显示如何在将凸缺损绘制到垫子图像上使用缺陷变量。如何实现?

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。我更喜欢垫子。

您可以使用'cvdrawContours()'绘制凸赫尔操作的结果,但是您确实需要正确设置参数,以实现这一目标。我有一个示例,但它使用" cvconvexhull2()"和iplimages,但是它应该以垫子和其他凸操作的方式工作:

IplImage* src; //the image where the contours are detected on
IplImage frm;  //the image you want the results to be drawn on
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours = NULL;
cvFindContours(src, storage, &contours, sizeof (CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
for (CvSeq* c = contours; c != NULL; c = c->h_next) {
            CvSeq* dest = NULL;
            CvMemStorage* hullStorage = cvCreateMemStorage(0);
            dest = cvConvexHull2(c, hullStorage, CV_CLOCKWISE, 1);
            cvDrawContours(frm, dest, cvScalarAll(255), cvScalarAll(255), 0, 2, 8);
            cvReleaseMemStorage(&hullStorage);
        }
cvReleaseMemStorage(&storage);