Out of curiosity I ran a performance test comparing OpenCV‘s own blob extraction method using cv::findContours to the popular cvBlob library for blob extraction. Both basically accomplish the same, however their interface differs significantly, which is why they’re not easily interchangable once one has settled to use one of them in a program. Most notably the cvBlob library still uses the (for usage in C++ projects) old-fashioned C-style API for OpenCV, and therefore also requires the programmer to pass the images and data in the respective format (IplImage* instead of cv::Mat, and so on…). Word is on the street that a nice C++ interface will follow sometime.
The performance tests were run on a not-so-modern desktop computer (Intel® Core™2 Duo CPU E8400 @ 3.00GHz × 2) running a 64 Bit Ubuntu 11.10 and 4 GB of RAM. I used pre-build binaries of OpenCV 2.3.1 found in this launchpad ppa. CvBlob 0.10.3 was then build from source. Using a quickly hacked C++ program the following test-setting was implemented:
A series of square images of different sizes (1600, 3200, 6400, 8500, 10600 and 12800 px side length) containing a different numbers of blobs (9, 16, 36, 64, 144, 289, 400, 625, 1024) was generated on the fly, resulting in 6 * 9 = 54 images to analyse. The blobs were generated as equally spaced circles, so a typical image looks like this:
For each image blob extraction was performed five times by each library, and the mean of these runs used as the resulting duration. I drew the resulting diagrams using python scripts, matplotlib and numpy. The complete source files to (hopefully) reproduce the test results can be found here (by the way, why is it that wordpress only allows uploading certain filetypes?!).
It seems that cvBlob is generally the faster method. Although both algorithms seem to scale comparably, using cvBlob always outperforms OpenCV’s findContours method.
As the duration difference diagram shows that the higher the number of blobs and the bigger the image size, the bigger the difference in computation time between the methods is. However, the biggest speedup can be achieved when working with smaller images containing a great number of blobs.
My performance test shows that the cvBlob library performs slightly better than OpenCV’s own methods. However, one has to note that only the extraction time itself was measured. CvBlobs requires the programmer to provide a „label image“, in which the blobs will be painted in different colours. For larger image sizes, the allocation of this image can take up quite an amount of time, so it would be wise to pre-allocate it. If you fail to do so, and allocate a new label image each time blob extraction is done, you will find cvBlob outperformed by the OpenCV method by far. It should also be mentioned that using the C-style interface of the cvBlob library can be quite cumbersome in a C++ program, when everywhere else you use the OpenCV’s C++ API.
If you detect any error or inaccuracy in this test (e.g. by using some methods in sub-optimal ways), please let me know (e.g. by leaving a comment) so I can improve it and present more precise results.