我测试过,随便给一个vector<point>类型赋值,然后用drawcontour画出来,都是按点顺序画出来的闭合多边形。同样是经canny边界探测,然后findcontour出来的vector<point>类型,drawcontour为什么有不是闭合的。 
还有一个问题,明明看起来是闭合的边界,选择其中一个点用pointpolygontest测试结果确实不在其中,只有convexhull以后才能得到正确的结果。
忘大神解答

解决方案 »

  1.   

    能不能给个图片的处理结果看一下,canny出来有很多细小的毛刺,我觉的可能是这方面的原因
      

  2.   

    仅供参考:const static Scalar colors[15]={
        CV_RGB(  0,  0,128),
        CV_RGB(  0,128,  0),
        CV_RGB(  0,128,128),
        CV_RGB(128,  0,  0),
        CV_RGB(128,  0,128),
        CV_RGB(128,128,  0),
        CV_RGB(128,128,128),
        CV_RGB(160,160,160),
        CV_RGB(  0,  0,255),
        CV_RGB(  0,255,  0),
        CV_RGB(  0,255,255),
        CV_RGB(255,  0,  0),
        CV_RGB(255,  0,255),
        CV_RGB(255,255,  0),
        CV_RGB(255,255,255),
    };
    Mat img,gray,bw;
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
            findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
            if (!contours.empty()&&!hierarchy.empty()) {
                idx=0;
                n=0;
                vector<Point> approx;
                for (;idx>=0;idx=hierarchy[idx][0]) {
                    color=colors[idx%15];
    //              drawContours(smallImg,contours,idx,color,1,8,hierarchy);
                    approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
                    const Point* p = &approx[0];
                    int m=(int)approx.size();
                    polylines(smallImg, &p, &m, 1, true, color);
                    circle(smallImg,Point(p[0].x,p[0].y),2,color);
                    circle(smallImg,Point(p[1].x,p[1].y),1,color);                n++;
                    if (1==n) {
                        maxrect=boundingRect(Mat(contours[idx]));
                    } else {
                        brect=boundingRect(Mat(contours[idx]));
                        CvRect mr(maxrect),br(brect);
                        maxrect=cvMaxRect(&mr,&br);
                    }
                }
                circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
            }
            imshow("display",smallImg);
            waitKey(0);