Detecting blobs using cvblobs library

A blob in image processing is often defined as a group of connected pixels with same logical state. Blob analysis is often used to detect a moving object in  real time image processing.

cvblobs is a blob library for OpenCV available at http://code.google.com/p/cvblob/

To use this library in codeblocks just unzip the downloaded library and copy the cvblob.h header file to the headers directory in opencv in my case it is C:\opencv2.0\include\opencv.

Then add the rest of the files to your project by right clicking on the project and selecting add files.

Here is a example for tracking an yellow  colored object using this blob library


//OpenCV Headers
#include<cv.h>
#include<highgui.h>
//Input-Output
#include<stdio.h>
//Blob Library Headers
#include<cvblob.h>
//Definitions
#define h 240
#define w 320
//NameSpaces
using namespace cvb;
using namespace std;
int main()
{
//Structure to get feed from CAM
CvCapture* capture=cvCreateCameraCapture(0);
//Structure to hold blobs
CvBlobs blobs;
//Windows
cvNamedWindow("Live",CV_WINDOW_AUTOSIZE);
//Image Variables
IplImage *frame=cvCreateImage(cvSize(w,h),8,3);   //Original Image
IplImage *hsvframe=cvCreateImage(cvSize(w,h),8,3);//Image in HSV color space
IplImage *labelImg=cvCreateImage(cvSize(w,h),IPL_DEPTH_LABEL,1);//Image Variable for blobs
IplImage *threshy=cvCreateImage(cvSize(w,h),8,1); //Threshold image of yellow color

//Getting the screen information
int screenx = GetSystemMetrics(SM_CXSCREEN);
int screeny = GetSystemMetrics(SM_CYSCREEN);

while(1)
{
//Getting the current frame
IplImage *fram=cvQueryFrame(capture);
//If failed to get break the loop
if(!fram)
break;
//Resizing the capture
cvResize(fram,frame,CV_INTER_LINEAR );
//Flipping the frame
cvFlip(frame,frame,1);
//Changing the color space
cvCvtColor(frame,hsvframe,CV_BGR2HSV);
//Thresholding the frame for yellow
cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);
//Filtering the frame
cvSmooth(threshy,threshy,CV_MEDIAN,7,7);
//Finding the blobs
unsigned int result=cvLabel(threshy,labelImg,blobs);
//Rendering the blobs
cvRenderBlobs(labelImg,blobs,frame,frame);
//Filtering the blobs
cvFilterByArea(blobs,60,500);
for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it)
{
double moment10 = it->second->m10;
double moment01 = it->second->m01;
double area = it->second->area;
//Variable for holding position
int x1;
int y1;
//Calculating the current position
x1 = moment10/area;
y1 = moment01/area;
//Mapping to the screen coordinates
int x=(int)(x1*screenx/w);
int y=(int)(y1*screeny/h);
//Printing the position information
cout<<"X: "<<x<<" Y: "<<y<<endl;
}
//Showing the images
cvShowImage("Live",frame);
//Escape Sequence
char c=cvWaitKey(33);
if(c==27)
break;
}
//Cleanup
cvReleaseCapture(&capture);
cvDestroyAllWindows();

}

Here we are taking live feed from the cam and thresholding it for yellow color. Then we are smoothing it with a median filter and finding the blobs. Then we are rendering them so that we can see them. cvFilterByArea removes any blobs which are too big or too small to be called a blob. Then we are finding their moments,dividing them by their area we get the X and Y positions.

The result is as follows

105 thoughts on “Detecting blobs using cvblobs library

  1. i tried your program,but i can’t run it.. the error message tell me the problem is on my memory..what you suggest to me to fix it?

    help me please…

  2. Pingback: Triggering keyboard events using image processing « 8A52labs

  3. Pingback: Triggering Mouse Events « 8A52labs

  4. hai i kumar doing master in taiwan, and i am reading ur contents and very useful to me, can u tell me how to do dct idct for an rgb image, i tried using cvSplit, but when i use each channel in dct() the error msg is”the function/feature is not implemented (Odd-size DCT’s are not implemented in function cvDCT)” what is the solution for this,but wihen i used gray image it is working ,i dont know wat is the problem?

    • Hi,
      I am glad you are finding my blog useful.
      cvDCT supports only even numbered arrays like 2,4,6 etc…
      So I suggest you to pad the matrix if your array is odd numbered 🙂

  5. Hi, this is Koko and I would just like to clarify some things with cvBlob.

    first, this is the file you download right – cvblob-0.10.3-src.zip

    second, when you say “Then add the rest of the files to your project by right clicking on the project and selecting add files.”

    would the “rest of the files” be the source files also in the file that you download, being the C++ source files:
    cvaux
    cvblob
    cvcolo
    cvcontour
    cvlabel
    cvtrack

    thank you again for your support 😀

      • Thanks for the quick reply!

        I am happy to say that everything worked fine, this really helped me out. Was almost about to give up on cvBlob before I saw this XD

        thanks again for your help

      • I am happy that you found my blog useful.
        Yeh, that was the real intention. No one should suffer the frustration i suffered :-).

    • Hello again,

      Would you know a place where I can check for example uses of the functions in cvblob? I have been checking the function declarations in the document provided by the cvblob website, but the information is somewhat limited. One example would be ‘cvRenderBlob’, I have read that it can draw or print the data of the blobs. Well in most examples the blobs are being drawn, but I would like the data of the blobs to be printed.

      Thanks again for your help 🙂

    • Counting blobs is equal to counting the labels given in the render.
      My function for counting blobs is as follows

      //Function to find the number of blobs of the given color
      int findnumber(IplImage *frame,IplImage *threshy,IplImage *labelImg)
      {

      //Variable to hold no of blobs
      int label=0;
      //Structure to hold the blobs
      CvBlobs blobs;
      //Finding the blobs
      unsigned int result=cvLabel(threshy,labelImg,blobs);
      //Rendering the blobs
      cvRenderBlobs(labelImg,blobs,frame,frame);
      //Filtering the blobs
      cvFilterByArea(blobs,60,500);
      for (CvBlobs::const_iterator it=blobs.begin(); it!=blobs.end(); ++it)
      label=it->second->label;
      //Releasing the blobs
      cvReleaseBlobs(blobs);
      return label;

      }

  6. hello,

    I’m using your code and I have some error compiling it, I’ll be very glad if you’ll help me.
    the error I constantly get are :

    ./src/HelloWorld.o: undefined reference to symbol ‘cvFlip’
    and the same for all cv functions…

    what seems to be the problem in your opinion?

    thank you!!

    Yokhai
    en.yokhai@gmail.com

  7. Hi….I tried your code in Codeblocks 8.02 on Ubuntu 10.04 LTS…and i am getting an error saying that “GetSystemMetrics,SM_CXSCREEN,SM_CXSCREEN are not defined in this scope”
    Can you help me with that please..???

  8. Hi, thanks for the tutorial !

    I tried your program and installed openCV & cvBlob library step by step
    but still have an error LNK2019:cvFilterByArea cvRenderBlobs cvLabel ….
    what’s wrong with that?
    Thank you!

    • Have you added the sources files of cvBlob in your project (not only in the folder)? If you only add the files to your folder compiler will not compile it, so you need to add the files to your project.

      You need to copy the header to include folder in opencv

  9. Hello Like your Post,
    I am following everything . but I am getting error
    test.obj : error LNK2019: unresolved external symbol __imp__GetSystemMetrics@4 referenced in function _main
    1>C:\Users\dhut\documents\visual studio 2010\Projects\cexample\Debug\cexample.exe : fatal error LNK1120: 1 unresolved externals

    can you help me out please ?

    • Try running the program as console. It seems there is a linking error with GetSystemMetrics.
      Make sure you have included windows.h.
      If nothing works replace screenx and screeny with the values of ur monitor(pixel values for width and height)

      • Thanks For Reply.
        Can you tell me what I need to detect two blobs of same or different colors, locate their Centroids and find the distance between these two blobs?

        Plz guide me.

      • You can find that in my another blog post such as triggering keyboard events
        All u need to do is have two threshold image for two different colors say green and yellow
        and pass them to a blob detect functions 🙂

  10. Hi,
    I am trying to compile it using VS2010 and OpenCV 2.3 and I get the next error:

    Error C2065: ‘nbsp’: not declared identifier //it’s on the line 23

    I hope you can help me. Thanks for sharing.

  11. HI!,
    How can I get the angle of the blobs? (The angle of the green line that cross the moments in the image)
    Thanks for your answers! 🙂

    • double detectangle(IplImage *thresh,IplImage *mhi,double timestamp,IplImage *orient,IplImage *frame)
      {
      //Updating the motion history
      cvUpdateMotionHistory(thresh,mhi,timestamp,1);
      //Calculating motion gradient
      cvCalcMotionGradient( mhi,thresh, orient,.5,.05, 3 );
      //Rectangle
      CvRect rectangle;
      //Setting ROI
      rectangle=cvRect( 0, 0,w,h);
      // select component ROI
      cvSetImageROI(frame,rectangle );
      cvSetImageROI( mhi,rectangle );
      cvSetImageROI( orient,rectangle);
      cvSetImageROI(thresh,rectangle);
      //Calculating the angle
      double angle=cvCalcGlobalOrientation(orient,thresh, mhi, timestamp,1);
      angle=360.0-angle;
      // cout<<"Angle:"<<angle<<endl;
      return angle;
      }

  12. I’m getting this error : unresolved external symbol _cvFilterByArea referenced in function _main…..I followed all the mentioned steps…help

  13. I’m also getting the following errors…
    “1>testing.obj : error LNK2019: unresolved external symbol _cvFilterByArea referenced in function _main
    1>testing.obj : error LNK2019: unresolved external symbol _cvRenderBlobs referenced in function _main
    1>testing.obj : error LNK2019: unresolved external symbol _cvLabel referenced in function _main”
    But i have referenced the blob library in both project folder and also in the project in vc++…

    What could be t reason 😦

  14. Your samples are awesome!!
    How can I identify two or more blobs position?
    I want to label (or recognize) some blobs and use them as markers, then get the position of those blobs. For example, if y have only two blobs on screen, I want to label them as “1” and “2” and know their position as X1,Y1 and X2,Y2.

    Thanks for your knowledge! 🙂

    • I solved it, but what I want to know is if its possible to hold the blob label no matter if it changes its position, and how to do it

      • please post the code to identify more than one blob and to print their x and y coordinates as x1,y1 and x2 and y2
        and if you cracked how to hold the blob no. and keep on printing their individual x and y in a video please post it too….

  15. Hi there, thanks for these blog it is so useful for me.
    This program worked perfectly on my project and I want to ask how can I track other colors for example red? Can you give red color threshold levels?Because I did not understand
    cvInRangeS(hsvframe,cvScalar(23,41,133),cvScalar(40,150,255),threshy);
    this line for yellow.Thank you.

    • I am glad that you found it useful 🙂
      That line is for detecting yellow
      u can use a red object,use slider bars in place of values to get the value for detecting a red object
      I have discussed this in Thresholding, Please refer that post 🙂

  16. thanks for all the help firstly.
    we are working on a virtual mouse project that works with blob. we get lots of helps from your blog.
    we want to let user pick the color that he want to control mouse. i mean we want to make a code that gives thechance to pick color of blob to user. (with clicking on a color on the screen)
    can you give us some clues?? you are like our mentor.

    thanks again for all.

  17. you tutorial is very useful for me and i use it in my graduation project ,but i want help from you that i need more information about the blob not only there number ,i make some change and can detect red and green and blue at once but i need to different between them to use them so if you can tell me how to do this ?

  18. when i am using this code with ‘start with debugging’ it has problems with “msyuv.dll”.
    it is unloaded and compiler says
    fail toload c:\\WINDOWS\system32\msyuv.dll can not open or load PDB file.

    when i am using start without debuging its working fine but i have to fix it i guess to work with better achievement.
    any idea??

  19. Hi! Nice tutorial.
    I would let you know, that I just added to my project the cvbob.h file on the header section and it worked fine, too. (I am working with cvblob 0.10.4 and windows 7).
    Could you, please, give me some advices on how change the color of the object?
    I would like to track a blue object…
    Thanks.

    • To detect a blue object you just have to give proper values for tresholding blue objects.
      Refer to the post about tresholding
      You can use slider control to get the values by trail and error

  20. Hi,
    Your is very useful. I tried working with cvBlob but I am not able to link it properly. I am working with VS2010 and OpenCV 2.1. I downloaded that cvBlob src and header files and have added them as directed in your blog. Still I am getting this error:
    error C2065: ‘CvBlobs’ : undeclared identifier

    Please help me. It’s very urgent.
    Thanks

  21. your work is superb dear friend, but facing some problem in visual studio 2010.
    errors like this:
    error LNK2019: unresolved external symbol _cvSaveImageBlob referenced in function _main documents\visual studio 2010\Projects\testopencv2\testopencv2\sourcefile.obj testopencv2

    its my project ,plz download my project here and help me to fix it please:
    https://sites.google.com/site/funitqta/files/testopencv2.rar?attredirects=0&d=1

    plz any one can fix it, its built in Visual studio 2010 and opencv 2.2.

  22. Hi,
    I saw your videos at youtube and I have become fan of yours. Please make a complete tutorial for moving and controlling your mouse with webcam. Because I can’t do without tutorial. Please do just one complete tutorial for me and don’t forget to reply. THANKS in advance.

  23. Hi,
    I love you work its really clear and well written!
    I’m just wondering if you could do this with multiple object? What I mean is tracking 2 or more object using the same HSV color and yet still able to extract the X and Y position. Because, from what I understand, your code is calculating the center of the tracked object using the moment formula, and when 2 or more object with the same HSV value is introduced to the camera, the position of the tracked object would be the average of the sum of moment.
    Using multiple HSV colored object could be an option, since I need to track 5 different objects, the code will be really slow. Therefore I need a help from a master like you to enlighten me with this problem.
    I would really really appreciate if you could add this functionality to this code

  24. Hi everybody!!! this program is great and i thank you for working on this and share your knowledge,i’ve modified this code to show an avi and detect all the blobs and to calculate their area, i just wanna know if there is a way to calculate the color median or the average (i’m italian sorry for bad english) and than to build an image that show in a scale of red or another color the predominance of a color in all the video (i suppose that i also must calculate the moving of the camera and maybe i should subtract it )
    Hope somebody will help me!
    Thank you all!!!

  25. Hi,
    I am very much impressed with your blog & useful contents you posted.

    I have followed article on tracking color objects.

    I have question:
    How would i track face or any part of face like nose or eyes.
    If you have code or any idea regarding this plz share.

    Thanks.

  26. Hi,thank you for your great demonstration of cvBlob. in my program i need only to find position of blobs and do not imply those blobs on the frame(for the sake of time) . should I change the render function by myself or there is a choice to cancel that action.
    best wishes,cheers

  27. Hi!

    I’ve tried to run your program on Debian using Geany and OpenCV 2.4.3 besides the latest cvblob library but I haven’t had success so far. I receive the following warning message on the console: unused variable ‘result’. Would it be because of different versions of OpenCV?
    While running, either the video frame doesn’t show up or freezes and no tracking is shown…

    Thanks in advance

  28. Its truely awesome, had so many doubts, bt all got cleared after reading commnets 😀
    Executing this on ubuntu 12.04, so using static values for screen resolution, want some solution for tht..
    btw nice job, keep it up 🙂
    thnx..

  29. Pingback: Object Tracking on the Raspberry Pi with C++, OpenCV, and cvBlob « ProgrammaticPonderings

  30. Hello,

    Thanks for the usefull demonstration of cvblob ..i have a question and i would be greatfull if you can help .. after i extract number of blobs each on is given a label .. the number of label is given according to the position of blob i.e the top left blob will be given number one .. my question is how to re number the blobs in a way so that the top right blob is number one ?

    Thanks

    • Hi,
      Good question
      I was stuck there myself, I tried to modify label object but with little success
      been ages i worked on image processing,
      Please let me know if u find any method 🙂

      Cheers
      Chinni

  31. Hi,
    I am very new to C++. I have worked lot on Image processing algorithms using MATLAB…! I find openCV will help to handle images in C++ as similar to Matlab. I have installed VS2010 and included openCV. As a beginner not to develop and stuck with errors — used your live streaming code and got it compiled – run successfully 🙂 To work efficiently and develop algorithms beyond the library available functions the main task would be knowing to work on handling the pixel index and intensity values corresponding to it. Can you add up some demo which mainly focus on handling the pixels instead of using a single line function with x and y input/ output arguments….! ( or if you developed something of that sort — provide me the link). This would be really helpful for to carry on with C++ plus Image processing.

    Your blog is definitely helping many and undoubtedly serving me too 🙂 Thanks for the efforts and keep updating — keep serving 🙂

    • Hi Sriram,
      Good to know that you are transforming from MATLAB to embrace the powerful dark side :-).
      Yeh what you said is absolutely true.
      I suggest to look at cvMAT functions.
      Presently busy with school but sure I will keep it in mind as a subject for my next blog post

      Cheers
      8A52

  32. your tutorial is really interesting & helpful. I have a small problem ,when I compile I get this error
    fatal error C1083: Cannot open include file: ‘winable.h’: No such file or directory.Pleas be kind enough to reply

    • Hi,
      1. You can find that winable.h header easily else try using winuser.h. It is just an interface to hack into the OS (in crude statements)
      2. Spamming me with repeated comments won’t help you get the answer. All the comments are approved only when I approve them.

      Cheers
      8A52

  33. Hello dear, your project is really great. I am using OpenCv 2.4.6 with Visual Studio 2010. I have included cvblob.h and andded all necessary files with the project. I have a problem that when I run the project camera on and off automatically. What can I do now?

    • Hi Dejan
      I suggest try one of the samples which came along with the Opencv.
      I wish I can answer in more depth but I never used in any MS products to tell you with more authority

      Cheers
      8A52

  34. i am trying with your code but its giving me error regarding GetSystemMetrics. Error on cosole is like this
    yellow_object_track.cpp:13:32: error: ‘SM_CXSCREEN’ was not declared in this scope
    yellow_object_track.cpp:13:43: error: ‘GetSystemMetrics’ was not declared in this scope
    yellow_object_track.cpp:14:32: error: ‘SM_CYSCREEN’ was not declared in this scope
    yellow_object_track.cpp:14:43: error: ‘GetSystemMetrics’ was not declared in this scope

  35. Hi friend,
    For C language(Not C++), can use cvblob library? because I am writing c code with cvblob, but always error “cvblob.h:33:20: fatal error: iostream: No such file or directory
    compilation terminated.” How? Thanks for your help!!

  36. Hello,
    Is there a method to access the pixels of each blob( like getPIxellist in matlab).Because I needed to extract features from the pixels of the blob and aggregate them

  37. Informative tutorial ,, ! Thanks a lot
    I needed to track heads of people within a given room using webcam mounted in static position( bird eye view).
    I have been using cvBlob library with OpenCv , i am not able to track heads and count people so far pls help me

  38. I have problems about configuration opencv in codeblocks. I use windows. Can you tell me how to configure it? I have tried some ways. But still not working.

  39. Hello,
    your code helped me so much,
    I would just to know if the coordinates x and y indicate the position of the tracked object relative to the camera?
    If not, how could i do that?
    Thank you.

  40. hello friends. I’m trying to track ants with cvblobs library. but I do not understand how to keep the labels of each ant after these meet (collisions or unions) . Please could you help me? thank you very much

  41. Hello i’m agus,
    i’m going to use your code for tracking red color.
    but first i had a problem with configuring the cvblob library. i use opencv with Qt for my project. i’ve done install opencv, cmake, Qt. and already tested it. but when I want to do something with your code i got an error with header placing, it said ‘cvblob.h no such file in directory’. i already put the header in ‘C:\opencv\include\opencv’ and i add the rest of the file as a source file, then i got much warning from my compiler. pls help me. thank you.

  42. Hello, I would like to thank you for the wonderful tutorial, just one question: is there an equivalent function for GetSystemMetrics. You see I am running my code on ubuntu, and it seems like this particular function refers to the windows.h library ? Thank you very much.

  43. Hi thanks for the tutorial 🙂 its really helpful. after so much of struggle i was able to fix it. I have one problem. sometimes application not staring just try to open the web cam and application terminates automatically 😦 after trying five six times might program gets working 🙂 any idea about the problem ? Thanks a lot for the tutorial 🙂

  44. Hei, nice post anyway.
    But if you could make a ‘step by step video’ thats gonna be perfect to help new user of opencv like me. Keep it up!
    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s