【问题标题】:OpenCV getting points in contoursOpenCV在轮廓中获取点
【发布时间】:2013-04-26 17:29:34
【问题描述】:

到目前为止,我已经尝试找到一种方法来了解一个点 (cvPoint) 是否与另一个点在同一个洞中。我的解决方案是采用 cvFindContours() 的应用产生的 CvSeq,并用适当的颜色填充这些孔以获得斑点矩阵。 当它完成时,知道一个点是否与另一个点属于同一轮廓只需要比较像素值,但我不知道为什么它不起作用。

不幸的是,这是一个没有人回答的问题,我在 Google 和 StackOverflow 上花了很多时间(或者我可能真的不擅长找到关键词)。希望有人有线索;)

IplImage *imgTemp = cvCreateImage(cvGetSize(getMorph()), (getMorph())->depth, 1);
    CvMemStorage *mem = cvCreateMemStorage();
    cvConvertImage(getMorph(), imgTemp);
    CvSeq *contours = NULL;
    cvFindContours(imgTemp, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);

    int colIt=255;
    for (CvSeq *ptr = contours; ptr != NULL; ptr = ptr->h_next) {
        if(ptr->v_next != NULL)
        {
            CvScalar color = CV_RGB( colIt,colIt,colIt);
            cvDrawContours(imgTemp, ptr->v_next, color, color, -1, CV_FILLED, 100);
            --colIt;
        }
    }

【问题讨论】:

    标签: c++ opencv contour image-segmentation


    【解决方案1】:

    最好的方法是使用 C++ API。使用 C++ API,您可以输出轮廓的层次结构,从而更容易确定它们是否属于同一轮廓。您可以找到 findContours here 的 c++ 变体的解释。

    仅通过顶部 loevel 轮廓的示例:

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(contourImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    int idx = 0;
    for (; idx >= 0; idx = hierarchy[idx][0]) {
     drawContours(image, contours, idx, Scalar(255), CV_FILLED);
    }
    

    【讨论】:

      【解决方案2】:

      基于diip_thomas 的答案,您可能可以使用pointPolygonTest 来测试Point2f 是否位于contour[i] 内(其中contourvector&lt;vector&lt;Point&gt; &gt;i 是i :这个向量的第一个元素)。示例见this answer

      【讨论】:

        猜你喜欢
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 2018-12-30
        • 2014-07-12
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多