views:

574

answers:

2

Hi Guys, I've done the hard work, turning my iSight camera on my MacBook into an infared 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

+4  A: 

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

Stefan
+1, and @tommed: this is what's going on cvBlob's cvlabel.cpp anyway, so why reimplement it?
AVB
This works perfectly! Many Thanks!!
tommed
@AB: the reason I didn't want to use cvBlob was because I thought it was still a separate library (it was apparently recently merged with v 2.0), and I wanted to limit the amount of prereqs required for my program.
tommed
+2  A: 

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.

Adi
Ah I see, that's what I was doing wrong! Many thanks for that.
tommed