简体   繁体   中英

OpenCV getting points in contours

I've tried so far to find a way to know if a point (cvPoint) is in the same hole than another. My solution was to take the CvSeq resulting from the application of cvFindContours() and to fill those holes with a proper color to have a matrix of blobs. When it'll be done, knowing if a point belongs to the same contour than another point consists just in comparing the pixel value but I can't figure out why it doesn't work.

Unfortunately, it's a question who's not answered and I spent a lot of time in Google and StackOverflow (or maybe I'm really bad for finding the key words). Hope someone has a clue ;)

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;
        }
    }

The best way to do this would be to make use of the C++ API. With the C++ API you can output the hierarchy of you contours to make it a lot easier to determine if they belong to the same contour. You can find the explanation of the c++ variant of findContours here .

Example for going through the top loevel contours only:

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);
}

Building on diip_thomas 's answer, you can probably use pointPolygonTest to test whether or not a Point2f lies inside contour[i] (where contour is a vector<vector<Point> > and i is the i:th element of this vector). See this answer for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM