Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any version of CvBlobs library which works with cv::Mat?

Tags:

c++

opencv

I have just discovered CvBlobsLib that can be a bless but it sadly uses IplImage.
Is there any chance that there is a new cv::Mat-style edition I just don't happen to find it?

EDIT:

It turned out that I happened to discover 2 distinct libraries, CvBlobsLib and CvBlobs, yeehaa. :)
I saw CvBlobsLib is less used than cvBlobs, that is NOT on opencv willowgarage but on google code. I welcome answers for both libraries, though, as both work with IplImage. :)

like image 822
Barney Szabolcs Avatar asked Feb 18 '23 09:02

Barney Szabolcs


2 Answers

EDIT: I'm talking about cvBlobs in this answer, sorry that I messed it up with cvBlobsLib ...

I've been looking for this too, but didn't came up with any library, which uses the new image structure.

But actually you can always do this: IplImage iplImg = mat; and just use &iplimg wherever you need an IplImage*.

I used cvBlobs this way successful in a few projects:

#include <cvblob.h>
using namespace cvb;

// load image
cv::Mat mat = cv::imread("image.jpg");

// convert cv::Mat to IplImage
IplImage img = mat;

// convert to grayscale
IplImage *gray = cvCreateImage( cvGetSize(&img), IPL_DEPTH_8U, 1 );
cvCvtColor( &img, gray, CV_BGR2GRAY );

// get binary image
cvThreshold( gray, gray, 150, 255, CV_THRESH_BINARY );

// get blobs
IplImage *labelImg = cvCreateImage( cvGetSize(gray), IPL_DEPTH_LABEL, 1 );
CvBlobs blobs;
unsigned int result = cvLabel( gray, labelImg, blobs );

// render blobs in original image
cvRenderBlobs( labelImg, blobs, &img, &img );

// *always* remember freeing unused IplImages
cvReleaseImage( labelImg );
cvReleaseImage( gray ); 

// convert back to cv::Mat
cv::Mat output( &img );
like image 121
dom Avatar answered Mar 23 '23 00:03

dom


Actually, the real C++ version of @moosgummi's answer looks something like this:

#include <cvblobs.h>
using namespace cvb;
using namespace cv;
// load image
Mat mat = imread("image.jpg");

// convert to grayscale
Mat gray; cvtColor(mat, gray, CV_BGR2GRAY);

// get binary image
threshold( gray, gray, 150, 255, CV_THRESH_BINARY );

// get blobs
Mat labelImg; labelImg.create( gray.size(), IPL_DEPTH_LABEL ); // need to check if IPL_DEPTH_LABEL is the right type...not sure
CvBlobs blobs; 
IplImage iplLabelImg = labelImg; // do not release this!
unsigned int result = cvLabel( gray, &iplLabelImg, blobs );

// render blobs in original image
IplImage iplMat = mat; // do not release this!
cvRenderBlobs( &iplLabelImg, blobs, &iplMat, &iplMat);

// for sake of compatibility with moosgummi:
cv::Mat output = mat;

And you need a wrapper class for CvBlobs say class cvb::Blobs with a lazy_copy_refcounted private CvBlobs data and some conversion operators to const CvBlobs and to CvBlobs. Also needed a wrapper function for cvLabel, cvb::label(), which does the castings for us, C++ programmers.

like image 38
Barney Szabolcs Avatar answered Mar 23 '23 00:03

Barney Szabolcs