I'm using OpenCV to filter an image for certain colours, so I've got a binary image of the detected regions.
Now I want to erode those areas and then get rid of the smaller ones, and find the x,y coordinates of the largest 'blob'
I was looking for recommendations as to what the best library would be to use? I've seen cvBlob and cvBlobsLib but I'm not too sure how to set them up. Do I want to compile them along with the project or do I want to compile and install them to the system (like I did with OpenCV)?
I'm currently using the Code::Blocks IDE on Ubuntu (although that shouldn't restrict things)
I'm late to the party, but I'd just like to chime in that there is a way to do connected components in opencv, it's just not mainlined yet.
Update: It is mainlined, it's just been stuck waiting for 3.0 to release for multiple years. Linky to documentation
See http://code.opencv.org/issues/1236 and http://code.opencv.org/attachments/467/opencv-connectedcomponents.patch
Disclaimer - I'm the author.
You can use findContours
to do that, see the opencv manual and a Tutorial to find connected components.
Edit: Code from the tutorial (via Archive.org)
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
IplImage *img, *cc_color; /*IplImage is an image in OpenCV*/
CvMemStorage *mem;
CvSeq *contours, *ptr;
img = cvLoadImage(argv[1], 0); /* loads the image from the command line */
cc_color = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 3);
cvThreshold(img, img, 150, 255, CV_THRESH_BINARY);
mem = cvCreateMemStorage(0);
cvFindContours(img, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for (ptr = contours; ptr != NULL; ptr = ptr->h_next) {
CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
cvDrawContours(cc_color, ptr, color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}
cvSaveImage("result.png", cc_color);
cvReleaseImage(&img);
cvReleaseImage(&cc_color);
return 0;
}
Unfortunately OpenCV doesn't have any connected component labelling functionality, which seems like a serious omission for a computer vision library. Anyway I had a similar requirement recently so I implemented my own CCL routine - there are a couple of different algorithms described on the CCL Wikipedia page and they are both pretty simple to implement.
I think the best and easy option to work with blobs with OpenCV is to use cvBlob library. Its a complemntary library with OpenCV a its so easy to use.