Open Computer Vision

December 20, 2008

Red blob detection from video

This site has moved to



  1. i want the exact percentage of lemon yellow,green and brown color in an image. how can i accomplish this.

    Comment by avidev — April 29, 2009 @ 4:07 pm | Reply

    • You need to Experiment and for different images it is really different…Even i experimented for the Video

      Comment by colouredpages — May 2, 2009 @ 5:36 am | Reply

      • can u help me with the code or any function that helps to find the variance of the colors lemon yellow,green and brown within an image.

        Comment by avidev — May 2, 2009 @ 1:13 pm

      • just check the post “understanding color space basics”,…the program which you get there is meant to show the channels dispersed..put ur sample imagein the program you would get a good idea but make sure you know HSV color space well..

        Comment by colouredpages — May 4, 2009 @ 6:45 am

  2. Ok, I’m a little confused.

    On line 66 – 70 you are retrieving properties of “frame” without ever having assigned anything to it.

    On line 97 and 99 the for loops will never run because your “>” won’t allow it.

    I fixed all that in the code and now I get a seg fault because the step size I got was huge and is firing me beyond the end of the “data” variable.

    Have I missed something?



    Comment by Fiachra — May 14, 2009 @ 10:45 am | Reply

    • There was only a small mistake I should have placed a les than instead of a greater than rest of all is perfect for sure….

      Comment by colouredpages — May 14, 2009 @ 4:18 pm | Reply

    • friend line 66 to 70 are just the declarrations I am not using them unless i get frames in it…it is just like declaring an “int i,j;” i am using only after i retrieve frames ..have a look nicely…

      Comment by colouredpages — May 15, 2009 @ 5:49 am | Reply

  3. same as Fiachra, still wondering the if statement
    and then also, you apply datamono after doing thresholding, but then when you do erossion and dillation you use monoimgbot
    is monoimgbot and datamono the same?

    Comment by dwi pebrianti — May 15, 2009 @ 1:55 am | Reply

    • Now remove all the confusions. I have eliminated the colimgblot and i have converted the RGB frame to the HSV frame …so the RGB frame after the convversion no longer has the same frame which we look and enjoy….it is converted that is why I removed the cvshowimage(frame) from the program

      Now look carefully

      RGB “Frame” converted to the HSV “frame” then applying the conditions of the hupper, hlower and the sthreshold if satisfied make the corresponding pixel data high else low…simple…
      datamono is the datapointer to the monoimgbot they are related but not the same…..get it…datamono is a char pointer but monoimgbot is a Iplimage variable…so they are related but not the same… please reply to tell me you understood or not …i would be more than happy to explain

      Note:This program was not done in the pre version.

      Comment by colouredpages — May 15, 2009 @ 5:45 am | Reply

  4. error C2065: ‘plImage’ : undeclared identifier

    Which library is missing?

    Comment by raul — May 29, 2009 @ 11:08 am | Reply

    • The spelling is wrong and even if that is correct and you do not get it then you have not installed library properly…

      Comment by colouredpages — May 30, 2009 @ 4:34 pm | Reply

  5. Thank you Adam,

    Let me first tell you that the code is written in the OPenCV 1.0 and you may have to download that version to get it working and please tel me If you face any problems now(I have edited the code)(there was a bracket extra (}))..

    The best guide for you for coloe detection is the code in “color detection using color space basics post…It is working fine or rather Great…

    I would really make sure that no errors creep in the website now..

    Comment by colouredpages — June 11, 2009 @ 2:15 pm | Reply

  6. It would be nice if you could write something about different methods of color detection, like using histogram to match, using cvSetImageCOI and using cvCopy or cvSplit and cvMerge on the channels.

    Comment by Stan L — June 30, 2009 @ 10:52 pm | Reply

  7. Although the idea is correct – thank you – , the code has some bugs:
    – stepmono,channelsmono are not initialized,
    – as others mentioned >=hlower should be height;
    int width = image->width;
    int step = image->widthStep;
    int channels = image->nChannels;

    uchar *resultData = (uchar *) resultImage ->imageData;
    int stepmono = resultImage->widthStep;
    int channelsmono = resultImage->nChannels;
    IplImage *imageHSV = cvCloneImage(image);

    cvCvtColor(imageHSV, imageHSV, CV_RGB2HSV); /* Converting the color space */
    uchar *data = (uchar *) imageHSV->imageData;

    // clearing the result image

    for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
    if( (data[(height – i) * step + j * channels] = hupper) &&
    (data[(height – i) * step + j * (channels) + 1] > sthreshold) ) {
    resultData[(height – i) * stepmono + j * channelsmono+1] = 255;
    cvDilate( resultImage,resultImage,0,10);

    Comment by rics — July 14, 2009 @ 9:04 am | Reply

  8. Could you recommend a better implementation of the problem?

    Comment by rics — November 30, 2009 @ 1:49 pm | Reply

    • There are other methods….This is just one of them…:)

      Comment by colouredpages — November 30, 2009 @ 10:23 pm | Reply

    • Just to API calls are enough
      Did u ever heard about cvInRangeS()???
      Bha.. I can say this is maybe one example on how to access
      pixels in image..nothing else.

      Comment by Graphicspell — December 2, 2009 @ 11:27 am | Reply

      • If you observe I always encourage people to find out some other methods of doing things which is mentioned in this blog…!!

        Comment by colouredpages — December 2, 2009 @ 1:08 pm

      • I don’t want to add anything else to your observation.
        You are absolutely right.
        But please let me say what I think too.
        I didn’t say you havennot sufficient knowdledge, you are saying that…I merely would underline
        that since the title of your blog is “OpenCV” computer Vision you have the duty to teach things in the right way, so if you already have an API that does the job you can not propose a solution like the one in this post; this is more than I can accept from a site like this expecially when you said you are know many other methods of implementing all the programs here: Perhaps did you want to provide poor solutions and keep the best ones for you? What do I think? Please be honest with your reader al least.

        Peace bro.
        Take care.

        Comment by Graphicspell — December 2, 2009 @ 2:59 pm

      • To be truthful..I have just worked about 7 to 8 months in opencv…and these days I am just getting time to check the comments..But yes i make it a point to make sure that i work for atleast one hour in a week…Yes you may observe that in the upcoming months I have plans .most probably in the month of Jan I would post some new programs on many things..I do not even have access to a very good library….that is a major concern for me right now….I am printing out ebooks….!!

        Comment by colouredpages — December 3, 2009 @ 3:16 am

  9. Cannot see all the comment

    Comment by òlmà — December 1, 2009 @ 3:52 pm | Reply

  10. First i will mention that this site is very useful.really appreciate it.
    My problem is i want to extract an image from a bunch of frames as input.The extracted image should contain a ball and a line.then depending on the ball position i want to judge whether the ball is in or out.
    Please help me out.

    Comment by neeraj — January 13, 2010 @ 6:18 am | Reply

    • I am not sure if i have got you correctly but then what i have understood is that you have a ball and you want to make sure if the ball is in the line or not….so first of all detect the line which means that you just need to make sure if the ball you are detecting is above the line or below the line…..make use of the pointer coordinates……everything you possibly need is make use of spatial coordinates….upload a sample picture if needed more help..!!

      Comment by colouredpages — January 14, 2010 @ 4:57 am | Reply

      • First of all i am really sorry for late reply.I really apologise.but i am totally frustrated trying out varios methods of opencv,it may b due to lack of knowledge of opencv.but its true that watever i have learnt about opencv is from ur m really thankfull to you.
        Now my point is i am trying to build a pilot model of hawk-eye system for tennis.till date i am succeded in capturing a video and dividing it into frames and also saving them.then by looking at the frames manuaaly i select a frame whcich has the ball and the line,so by taking that frame i use some function to get the area of interest(ROI) which is circular.Also i am able to get pixel of ball and that line at runtime.these all are seperate codes.i.e for capturing and dividing,circular ROI,and to get pixels value i use differnt opencv codes.i have not combined it.
        so my task now is give those saved frames to some function which will give me a frame which a ball and line,process that frame only to get the relative position of both(ball & line).And then decide whether the ball is IN or OUT.u may find the image at

        thanks in advance.plz help me out..and i am really sorry for such a late reply.Forgive me for that.

        Comment by neeraj — May 3, 2010 @ 7:54 pm

      • Dear,

        It is not the lack of knowledge but point is you are confusing the matter which is easy.
        In the image you have given first identify the white line and then identify the ball …compare the spatial coordinates (x,y) position….according to your image i think you should only compare x coordinate….if xball> xline ball is on the right f the line else vice versa…..
        I suggest you make a flow chart first and then make the program …and if the camera and the line are stagnant then there is no need to detect the line….just find out the coordinates of the line once and keep comparing the stagnant coordinates (line coordinates with the ball)…

        Jai Shri Krishna.

        Comment by colouredpages — May 7, 2010 @ 5:22 am

  11. Hey thanks a lot..totally impressed by the solution..but again i need your help on how to get the spatial co-ordinates of the ball and line..?and yeah my line will be having static how to hard-code it and compare it with line co-ordinates???how to save them?
    Also now i want to save only those frames which will be having a ball and line,so as to minimize the memory please suggest me what can i do??thanks a lot….

    Comment by neeraj — May 12, 2010 @ 5:05 am | Reply

    • I would love to tell you how to do this but this is not the purpos for which i have created this site.please try to do this on your own…try for some days and if you are not abel to do this i wil tel you.Please take a pen and a paper and draw a line ….and a point …take some imaginary x and y coordinates and try and compare what x coordinate of a point before and after the line….sit back relax and with a cool mind think about this…!! saving is not a big thing, if some conditions are satisfied you can save the image….

      Comment by colouredpages — May 12, 2010 @ 5:39 am | Reply

      • Yeah i know your purpose..thats why i said i am totally impressed by your blog.i read nearly all blog of yours on opencv..the thing is i am trying for solution,the same way yo u told me But problem lies here dat i am running out of time i said please guide me..thanks a lot ,i will keep trying but please you also tell me if possible.i will be grateful if you could..

        Comment by neeraj — May 12, 2010 @ 7:56 am


        Comment by colouredpages — May 12, 2010 @ 8:59 am

      • hey thanks a lot…i saw your comment.on papaer it works but i am unable to get the co-ordinates in my program.and without co-ordinates i am nothing.stil i am working on it(your diagram),but can you tell me that i should apply trial and error method only?please let me know as soon as possible…

        Comment by neeraj — May 14, 2010 @ 12:45 pm

      • simple……
        let (x1,y1) be the coordinates of the ball and (x2,y2) be the coordinates of the line….so compare that coordinate
        see according to the previous figure for the line y=29 compare the x coordinate….of the presence of the ball …Eureka…!!
        if (y1==y1)
        if (x1<x2) ballbeforeline();
        else ballafterline();//ignoring equal or you can include that if you want..!!

        Comment by colouredpages — May 15, 2010 @ 1:13 pm

  12. How to solve blob occlusion problem in VC++?

    Comment by asadkhan — June 3, 2010 @ 5:45 pm | Reply

  13. Hi ,

    I compile the program successfully , but when i run the program i am getting following run time error
    when following like encounters

    CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC( ‘D’,’I’,’V’,’X’),fps,size) ;

    Output #0 , avi , to ‘monoimg.avi’ :
    Stream #0.0: Video mgeg4, yuv420p, q=2-31, 90k tbn
    [mpeg4 @ 0x37e5c0] framerate not set
    OpenCV Error: Bad Argument (Could not open codec ‘mpeg 4’:Unspecified Error) in unknown function , file
    C:\User\VP\ocv\opencv\src\highgui\cvcap_ffmpeg.cpp, line 1306

    Comment by Nihar — July 25, 2010 @ 8:19 am | Reply

    • Your computer is missing codecs…Install a codec pack that worked for me..!!

      Comment by colouredpages — July 27, 2010 @ 4:16 am | Reply

  14. Hello. my name is Humberto Reyes, I am a student of Mechatronics Engineering, and am working on my thesis, which is on Geodesic Active Contours, and I want to do it with opencv, and i need some help with that, any idea how could it be (some code examples)


    I comment here because i did not found any other place to comment

    Humberto Reyes
    Mechatronics Eng. Estudent

    Comment by Humberto Reyes — August 12, 2010 @ 5:54 am | Reply

    • Dear , See after you convert the image from some color space to the other color space you know numbers are just converted to numbers ….so treat the image to be the same before and after the conversion….what i mean by this is that you can just apply the thresholding as you do for a normal image….image does not change into something else the numbers in the image change from for example 200 number might change to something else like 5…so depends…..but you should know which ‘h’ or ‘i’ you need to apply thresholding to…see i would not be able to go through your code please….

      Comment by colouredpages — October 9, 2010 @ 6:15 am | Reply

  15. You have mentioned about a link to the algorithm but you haven’t given it.

    Comment by Khan — April 7, 2011 @ 4:34 am | Reply

  16. Hi,

    Here you extract the red using the hue values..

    I am trying to create a similar program that extracts white using saturation values.

    Any suggestions on how to do that? I was thinking the cvSplit function on a HSV image..


    Comment by Prem — August 1, 2011 @ 5:05 pm | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

Create a free website or blog at

%d bloggers like this: