You are on page 1of 42

8A52labs

A place for geeks

Search

About

OpenCV

Signal Processing

HPC

Posted on May 24, 2011

Previous Next

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.
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 //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);

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

18 19 20 21 22 23 24

25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

//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

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

AD VE R T ISE M E N T

the jennifer aniston security tapes


AD VE R T ISE M E N T

EXCLUSI VE: "stolen" security camera footage from jennifer aniston's house (secrets revealed by smartwater)

Share this: Like this:

Facebook 10

Twitter 1

Like

One blogger likes this.

This entry was posted in OpenCV and tagged blob detection, Detecing objects using blobs, tracking colored objects by 8a52labs. Bookmark the permalink.

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

61 THOUGHTS ON DETECTING BLOBS USING CVBLOBS LIBRARY

Hendy on June 20, 2011 at 2:39 pm said:

i tried your program,but i cant run it.. the error message tell me the problem is on my memory..what you suggest to me to fix it? help me please
Reply

8a52labs on June 22, 2011 at 1:23 pm said:

Hi Hendy, Can you kindly post your error here so that I can give a better answer . Have you used cvBlob library? It can be downloaded from http://code.google.com/p/cvblob/ Just unzip the folder and copy the header file to
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

include folder in OpenCV Then add the remaining files to the project
Reply

abilash on June 30, 2011 at 4:36 pm said:

Thanks buddy its worked well for me work and keep posting
Reply

I appreciate your

Pingback: Triggering keyboard events using image processing 8A52labs Pingback: Triggering Mouse Events 8A52labs

Muthu Kumar on September 14, 2011 at 2:43 am said:

hai i kumar doing master in taiwan, and i am reading ur


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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 isthe function/feature is not implemented (Odd-size DCTs 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?
Reply

8a52labs on September 21, 2011 at 1:40 pm said:

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
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Koko Zialcita on October 8, 2011 at 4:14 am said:

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.3src.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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Reply

8a52labs on October 8, 2011 at 4:26 am said:

Hi, Yeh,that is the correct file. Yeh,Add the source files also as they will be compiled along with your own code in main.c
Reply

Koko Zialcita on October 8, 2011 at 4:33 am said:

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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

thanks again for your help

8a52labs on October 8, 2011 at 4:52 am said:

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

Koko Zialcita on October 10, 2011 at 2:00 pm said:

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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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
Reply

niru on October 23, 2011 at 5:30 pm said:

hiii which func is used to count no. of blobs?? thnks in advnc


Reply

8a52labs on October 25, 2011 at 8:36 am said:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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)
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

label=it->second->label; //Releasing the blobs cvReleaseBlobs(blobs); return label; }


Reply

Ruben Pennino on November 14, 2011 at 8:53 pm said:

Hi The VS2010 ask me for a cxtypes.h header (belongs to the first version of OpenCV) and cant compile it. Any Idea?
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

yokhai on December 4, 2011 at 8:19 am said:

hello, Im using your code and I have some error compiling it, Ill be very glad if youll 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
Reply

8a52labs on December 4, 2011 at 8:21 am said:

Hi I think there is a problem with linking of libraries


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

and headers
Reply

yokhai on December 4, 2011 at 9:53 am said:

solved it the program starts to run , the camera turns on, and suddenly shuts down with (Live:6042): Gtk-WARNING **: Unable to locate theme engine in module_path: pixmap, error. what does it mean?
Reply

8a52labs on December 4, 2011 at 1:24 pm said:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Maybe u have not installed the GTK sudo aptitude install gtk2-engines-pixbuf might work

Unmesh on December 7, 2011 at 11:56 am said:

Hi.I tried your code in Codeblocks 8.02 on Ubuntu 10.04 LTSand 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..???
Reply

8a52labs on December 7, 2011 at 12:31 pm said:

Hi,
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

I used that function to get the resolution of the monitor. Unfortunately it is win32 function. I have no idea about its parallel in Linux but the nearest answer i found is this http://stackoverflow.com/questions/1153052/how-toprogrammatically-get-the-resolution-of-a-windowand-that-of-the-system-in Else u can replace with values of your screen resolution
Reply

Kian on December 20, 2011 at 10:03 am said:

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 .
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

whats wrong with that? Thank you!


Reply

8a52labs on December 20, 2011 at 12:57 pm said:

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
Reply

salva on December 27, 2011 at 12:16 pm said:

One day searching how to make cvbloob works. What Koko


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Zialcita said in your blog saved me. thanks both!


Reply

Dhut on February 6, 2012 at 2:55 am said:

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 ?
Reply

8a52labs on February 6, 2012 at 8:13 am said:

Try running the program as console. It seems there


open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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)
Reply

Dhut on February 12, 2012 at 9:36 pm said:

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.

8a52labs

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

on February 16, 2012 at 11:50 am said:

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

Dhut on February 6, 2012 at 6:38 pm said:

Hi, I just Include #pragma comment(lib, user32.lib) . and it starts working.. Thanks for sharing..
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Emmanuel Tapia on February 10, 2012 at 11:19 pm said:

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 //its on the line 23 I hope you can help me. Thanks for sharing.
Reply

8a52labs on February 11, 2012 at 3:54 am said:

Hi, You might have copied the code wrong. nbsp is the formatting error if u see line 23 there is nbsp replace that with line from blog
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Emmanuel Tapia on February 13, 2012 at 7:39 pm said:

Hi!! yes!! It was a newbie mistake. It works perfectly. Thanks for sharing!!!

Adil on August 19, 2012 at 8:24 pm said:

Hi Emmanuel Tapia! will you plz help me that how to run this code in VS2010 and OpenCV 2.3, I get list of errors like the following: error LNK2019: unresolved external symbol _cvSaveImageBlob referenced in function _main documents\visual studio 2010\Projects\testopencv2\testopencv2\sourcefile.obj testopencv2
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

. i have uploaded complete vs project, plz help me to fix it : https://sites.google.com/site/funitqta/files/testopencv2.rar? attredirects=0&d=1 its built in Visual studio 2010 and opencv 2.2. plz check it.
Reply

Emmanuel Tapia on February 14, 2012 at 10:02 pm said:

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!
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

8a52labs on February 16, 2012 at 12:03 pm said:

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;
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

// cout<<"Angle:"<<angle<<endl; return angle; }


Reply

zahir on February 16, 2012 at 10:04 am said:

Im getting this error : unresolved external symbol _cvFilterByArea referenced in function _main..I followed all the mentioned stepshelp
Reply

8a52labs on February 16, 2012 at 12:08 pm said:

Remove cvFilterByArea() and try executing the program If u still get the error it means cvblobs is not attached correctly
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Reply

zahir on February 16, 2012 at 3:43 pm said:

Im 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
Reply

Emmanuel Tapia

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

on February 28, 2012 at 8:09 pm said:

Try reinstalling opencv


Reply

Emmanuel Tapia on February 28, 2012 at 8:02 pm said:

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!
Reply

Emmanuel Tapia on March 2, 2012 at 7:44 pm said:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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
Reply

fanofkhali on March 10, 2012 at 9:10 am said:

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.

Jay on February 29, 2012 at 7:29 am said:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

How would you modify the code to detect blobs in an image instead of an input camera? Thank you in advance.
Reply

8a52labs on February 29, 2012 at 6:57 pm said:

Get the feed from an image for example IplImage* img=cvLoadImage(Address);


Reply

burack23 on March 22, 2012 at 11:06 am said:

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);
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

this line for yellow.Thank you.


Reply

8a52labs on March 22, 2012 at 2:55 pm said:

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
Reply

maddox on March 22, 2012 at 11:15 pm said:

what you mean with slider bars ??

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

maddox on March 22, 2012 at 10:25 pm said:

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.
Reply

burack23 on March 22, 2012 at 11:15 pm said:

Thanks for reply.

What do you mean with slider bars??

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Reply

8a52labs on March 23, 2012 at 5:47 pm said:

Check this post It will show how to use threshold for a specific color by adjust the slider bar http://8a52labs.wordpress.com/2011/05/17/thresholdingimages/
Reply

burack23 on March 26, 2012 at 11:53 am said:

when i change the slider there is no change in threshold. i tried changing the initial numbers of h1,s1,v1 and it worked. so why slider bar not working instantly??

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

8a52labs on March 26, 2012 at 2:14 pm said:

Have u tried the thresholding post? Study the code u will get an idea sliders are linked to variables which u have to mention in cvThreshold

ahmed albhy on March 23, 2012 at 12:39 am said:

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 ?
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

maddox on March 25, 2012 at 4:18 pm said:

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??
Reply

Alessandro on May 28, 2012 at 9:21 am said:

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
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

the color of the object? I would like to track a blue object Thanks.
Reply

8a52labs on May 29, 2012 at 7:17 am said:

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
Reply

Aman Agarwal on July 23, 2012 at 10:23 am said:

Hi, Your is very useful. I tried working with cvBlob but I am not
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

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. Its very urgent. Thanks
Reply

8a52labs on August 18, 2012 at 3:24 am said:

Hi, I am an opensource person and I dont use MS products All of my blog posts are tested using CodeBlocks
Reply

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Adil on August 13, 2012 at 9:11 pm said:

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.
Reply

8a52labs on August 18, 2012 at 3:30 am said:

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Hi, I am an opensource person and I dont use MS products All of my blog posts are tested using CodeBlocks and DeV C++
Reply

Adil on August 19, 2012 at 8:06 pm said:

ok friend , still thanks.

Leave a Reply
Enter your comment here...
open in browser PRO version
Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

Theme: Twenty Eleven

Blog at WordPress.com. |

Follow

Follow 8A52labs
Get every new post delivered to your Inbox.
Join 25 other followers

Enter your email address Sign me up


Pow ered by WordPress.com

open in browser PRO version

Are you a developer? Try out the HTML to PDF API

pdfcrowd.com

You might also like