Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV grouping white pixels

I've done the hard work, turning my iSight camera on my MacBook into an infrared camera, converted it, set the threshold etc.. and now have an image that looks something like this:

alt text

My problem is now; I need to know how many blobs are on my image by grouping the white pixels. I don't want to use cvBlob/cvBlobsLib, I'd rather just use what is already in OpenCV.

I can loop through the pixels and group them by checking for (thresholded) touching white pixels, but I'm guessing there is probably a really easy way of doing this from OpenCV?

I'm guessing I can't use cvFindContours as this will retrieve all the white pixels in one big array, rather than separating them into "groups". Could anyone recommend? (Note these are not circles, just the light emitted from little IR LEDs)

Many Thanks in advance!
tommed

like image 558
tommed Avatar asked Mar 04 '10 10:03

tommed


2 Answers

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.

like image 126
Stefan Avatar answered Nov 03 '22 20:11

Stefan


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.

like image 41
Adi Avatar answered Nov 03 '22 22:11

Adi