OpenCV grouping white pixels

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:45:43
Stefan

Loop through the image looking for white pixels. When you encounter one you use cvFloodFill with that pixel as seed. Then increment the fill value for every region so that each region has a different color. This is called labeling.

Adi

Yes, you can do it with cvFindContours(). It returns the pointer to the first sequence that was found. Using that pointer you can traverse through all of the sequences found.

    // your image converted to grayscale
    IplImage* grayImg = LoadImage(...);

    // image for drawing contours onto
    IplImage* colorImg = cvCreateImage(cvGetSize(grayImg), 8, 3);

    // memory where cvFindContours() can find memory in which to record the contours
    CvMemStorage* memStorage = cvCreateMemStorage(0);

    // find the contours on image *grayImg*
    CvSeq* contours = 0;
    cvFindContours(grayImg, memStorage, &contours);

    // traverse through and draw contours
    for(CvSeq* c = contours; c != NULL; c = c->h_next) 
    {
         cvCvtColor( grayImg, colorImg, CV_GRAY2BGR );
         cvDrawContours(
                        colorImg,
                        c,
                        CVX_RED,
                        CVX_BLUE,
                        0, // Try different values of max_level, and see what happens
                        2,
                        8
         );
    }

Besides this method, I would advise you to take a look at cvBlobs or cvBlobsLib. Latter one is integrated in OpenCV 2.0 as official blob detection lib.

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