(opencv) merge contours together

喜夏-厌秋 提交于 2019-12-05 18:23:44

Did you try this?

std::vector<cv::Point> points;
points.insert(points.end(), contour1.begin(), contour1.end());
points.insert(points.end(), contour2.begin(), contour2.end());
convexHull(cv::Mat(points), contour);

PS. For some applications, it may be better to use approxPoly() rather than convexHull(). Just try both.

PPS. Try smoothing the resulting contour with gaussian. It also can be helpful.

I came across a similar problem. In my case I created an empty sequence then I filled it with the points of each contour, after that I fitted a bounding ellipse with that sequence. Here is my code segment...

CvMemStorage *storage = cvCreateMemStorage ();
CvMemStorage *storage1 = cvCreateMemStorage ();
CvSeq *contours = 0;

//find contour in BInv
cvFindContours (BInv, storage, &contours, sizeof(CvContour), CV_RETR_LIST,CV_CHAIN_APPROX_NONE ,cvPoint(0,0)); 

//creating empty sequence of CvPoint
CvSeq* seq = cvCreateSeq(CV_SEQ_ELTYPE_POINT/*| CV_SEQ_KIND_SET | CV_SEQ_FLAG_SIMPLE*/,sizeof(CvSeq),sizeof(CvPoint),storage1);

//populating seq with all contours
for(; contours!=0; contours = contours->h_next)
  for(int i=0;i<contours->total;i++)
     {  
         CvPoint* p;
        p = (CvPoint*)cvGetSeqElem (contours, i );
        cvSeqPush(seq,p);   
      }

//bounding box and drawing
CvBox2D bbox=cvMinAreaRect2(seq, NULL );
cvEllipseBox(color,bbox,cvScalarAll(0),5,8,0);

hope this helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!