Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7625125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:09:03+00:00 2026-05-31T05:09:03+00:00

im detecting coloured balls in a video feed using opencv. i am recording the

  • 0

im detecting coloured balls in a video feed using opencv. i am recording the circles centre points. when the white ball moves i would like to do a detection for 10 seconds while displaying the video feed. after the 10 seconds i would like the next piece of code to run. im having trouble with this. code follows

while(1) 
{
    img = cvQueryFrame( capture );

    CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 7, 45, 40,0,25);

    float* p;
    CvScalar s,t;
    int num_red = 15;


    for (int i = 0; i < circles->total; i++)
    {
         // round the floats to an int
         p = (float*)cvGetSeqElem(circles, i);
         cv::Point center(cvRound(p[0]), cvRound(p[1]));
         int radius = cvRound(p[2]);

         //uchar* ptr;
         //ptr = cvPtr2D(img, center.y, center.x, NULL);
         //printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);


         s = cvGet2D(img,center.y-6, center.x);
         t = cvGet2D(img,center.y, center.x);//colour of circle
        printf("\nB: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);

        if (s.val[2]<50 && s.val[1]<50 && s.val[0] <50)
        {
            printf("Black Ball\n");

        }
        else if(s.val[2]>200 && s.val[1]>200 && s.val[0]>200)
        {
            printf("White Ball\n"); 
            xpos=center.x;
            ypos=center.y;
        }
        else if(s.val[2]>=250 && s.val[1]>=250 && s.val[0] <200)
            printf("Yellow Ball\n");
        else if(s.val[2]<70 && s.val[1]>80 && s.val[0] <70)
            printf("Green Ball\n");
        else if(s.val[2]>100 && s.val[1]<100 && s.val[0] <100)
            printf("Brown Ball\n");
        else if(s.val[2]<100 && s.val[1]<100 && s.val[0] >100)
            printf("Blue Ball\n");
        else if(s.val[2]>=250 && s.val[1]<250 && s.val[0] >=250)
            printf("Pink Ball\n");
        else if (s.val[2]<=255 && s.val[2]>=0 && s.val[1]<=255 && s.val[1]>=0 && s.val[0] <=255 && s.val[0]>=0)
        {
            printf("Red Ball\n");
            //num_red++;
        }

         // draw the circle center
         cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );

         // draw the circle outline
         cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );

         //display coordinates
         printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
    }

    printf("\nDetected %d balls\n", circles->total);

    if (xpos >= oldx + 10 || xpos<= oldx - 10 //wait for white ball movement here.
        && ypos >= oldy + 10 || ypos<= oldy - 10)
    {
        oldx = xpos;
        oldy = ypos;
        printf("White Ball Moving");

        //start 10second counter where balls are tracked and video displayed
        //after 10 seconds next code runs 
    }

    {
        //code to be run after 10 seconds counter but ony then
    }

}//end of while there is frame incoming

cvShowImage( "Vid", img );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:09:04+00:00Added an answer on May 31, 2026 at 5:09 am

    Tip: if you are going to share code, make sure it works. The idea is to always share a minimal example that reproduces the problem you are facing. This makes it easier for us to help you.

    Since your code is incomplete I had to fill the missing pieces.

    The code below retrieves frames from the camera, but I imagine your original code was reading from a video file. Anyway, the program below starts a 2nd thread (using pthreads) when a white ball is detected. It writes “White Ball detected” for 10 seconds and when the timer is done it writes “10sec passed”.

    Due to the lack of information, the timer will only be fired once during the lifetime of the application (after the the FIRST white ball is detected). I tried to make this procedure as simple as possible so you can change it if you want it.

    #include <stdio.h>
    #include "cv.h"
    #include "highgui.h"
    
    // global variable manipulated by the 2nd thread
    bool wait10sec = false;
    
    void* timer_thread(void* arg)
    {
        wait10sec = true;
        sleep(10);
        wait10sec = false;
    }
    
    int main(int argc, char** argv)
    {
        CvCapture *capture = 0;
        capture = cvCaptureFromCAM(-1);
        if (!capture)
        {
          fprintf(stderr, "!!! Cannot initialize webcam!\n" );
          return -1;
        }
    
        cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
    
        IplImage* img = NULL;
        IplImage* gray = NULL;
        CvMemStorage* storage = cvCreateMemStorage(0);
    
        int xpos = 0;
        int ypos = 0;
        int oldy = 0;
        int oldx = 0;
    
        // Variables used to control the timer
        bool white_ball_moved = false;
        pthread_t thread_id;
    
        // Variables used to write text over the image
        CvFont font;
        double hScale=1.0;
        double vScale=1.0;
        int    lineWidth=1;
        cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
    
        char key = 0;
        while (key != 27) // ESC quits the application
        {
            img = cvQueryFrame(capture);
            if (!img)
            {
                fprintf(stderr, "!!! Failed to retrive frame!\n" );
                break;
            }
    
            cvShowImage("result", gray);
    
            if (!gray)
            {
                gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
                if (!img)
                {
                    fprintf(stderr, "!!! Failed to allocate gray frame!\n" );
                    break;
                }
            }
    
            cvCvtColor(img, gray, CV_BGR2GRAY);
    
            CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 7, 45, 40,0,25);
    
    
            float* p;
            CvScalar s,t;
            int num_red = 15;
    
    
            for (int i = 0; i < circles->total; i++)
            {
                // round the floats to an int
                p = (float*)cvGetSeqElem(circles, i);
                cv::Point center(cvRound(p[0]), cvRound(p[1]));
                int radius = cvRound(p[2]);
    
                s = cvGet2D(img,center.y-6, center.x);
                t = cvGet2D(img,center.y, center.x);//colour of circle
                printf("\nB: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
    
                if (s.val[2]<50 && s.val[1]<50 && s.val[0] <50)
                {
                    printf("Black Ball\n");
                }
                else if(s.val[2]>200 && s.val[1]>200 && s.val[0]>200)
                {
                    printf("White Ball\n");
                    xpos=center.x;
                    ypos=center.y;
                }
                else if(s.val[2]>=250 && s.val[1]>=250 && s.val[0] <200)
                {
                    printf("Yellow Ball\n");
                }
                else if(s.val[2]<70 && s.val[1]>80 && s.val[0] <70)
                {
                    printf("Green Ball\n");
                }
                else if(s.val[2]>100 && s.val[1]<100 && s.val[0] <100)
                {
                    printf("Brown Ball\n");
                }
                else if(s.val[2]<100 && s.val[1]<100 && s.val[0] >100)
                {
                    printf("Blue Ball\n");
                }
                else if(s.val[2]>=250 && s.val[1]<250 && s.val[0] >=250)
                {
                    printf("Pink Ball\n");
                }
                else if (s.val[2]<=255 && s.val[2]>=0 && s.val[1]<=255 && s.val[1]>=0 && s.val[0] <=255 && s.val[0]>=0)
                {
                    printf("Red Ball\n");
                    //num_red++;
                }
    
                // draw the circle center
                cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
    
                // draw the circle outline
                cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
    
                //display coordinates
                printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
            }
    
            printf("\nDetected %d balls\n", circles->total);
    
            if (white_ball_moved && wait10sec)
            {
                // white ball detected: execute this code for the next 10 seconds
    
                cvPutText (img, "White Ball detected", cvPoint(200,400), &font, cvScalar(255,255,0));
            }
            else if (white_ball_moved && !wait10sec)
            {
                // white ball was detected and 10 seconds have passed
                cvPutText (img, "10sec passed", cvPoint(200,400), &font, cvScalar(255,255,0));
            }
    
            if (!white_ball_moved)
            {
                if (xpos >= oldx + 10 || xpos<= oldx - 10 //wait for white ball movement here.
                    && ypos >= oldy + 10 || ypos<= oldy - 10)
                {
                    oldx = xpos;
                    oldy = ypos;
                    printf("White Ball Moving");
    
                    white_ball_moved = true;
    
                    // start 2nd thread to do the counting
                    pthread_create(&thread_id, NULL, timer_thread, NULL);
                }
            }
    
            cvShowImage("result", img);
            key = cvWaitKey(33);
        } // end of while, no more frames will be retrieved
    
        pthread_join(thread_id, NULL);
        pthread_exit(NULL);
    
        return 0;
    }
    

    Compiled with:

    g++ balls.cpp -o balls -I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lpthread
    

    EDIT:

    Windows version:

    #include <process.h>
    #include <stdio.h>
    
    #include <cv.h>
    #include <highgui.h>
    
    // global variable manipulated by the 2nd thread
    bool wait10sec = false;
    
    void timer_thread(void* arg)
    {
        wait10sec = true;
        Sleep(10000); // 10s
        wait10sec = false;
    }
    
    int main(int argc, char** argv)
    {
        CvCapture *capture = 0;
        capture = cvCaptureFromCAM(-1);
        if (!capture)
        {
          fprintf(stderr, "!!! Cannot initialize webcam!\n" );
          return -1;
        }
    
        cvNamedWindow("result", CV_WINDOW_AUTOSIZE);
    
        IplImage* img = NULL;
        IplImage* gray = NULL;
        CvMemStorage* storage = cvCreateMemStorage(0);
    
        int xpos = 0;
        int ypos = 0;
        int oldy = 0;
        int oldx = 0;
    
        // Variables used to control the timer
        bool white_ball_moved = false;
    
        // Variables used to write text over the image
        CvFont font;
        double hScale=1.0;
        double vScale=1.0;
        int    lineWidth=1;
        cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);
    
        char key = 0;
        while (key != 27) // ESC quits the application
        {
            img = cvQueryFrame(capture);
            if (!img)
            {
                fprintf(stderr, "!!! Failed to retrive frame!\n" );
                break;
            }
    
            cvShowImage("result", gray);
    
            if (!gray)
            {
                gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
                if (!img)
                {
                    fprintf(stderr, "!!! Failed to allocate gray frame!\n" );
                    break;
                }
            }
    
            cvCvtColor(img, gray, CV_BGR2GRAY);
    
            CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 7, 45, 40,0,25);
    
    
            float* p;
            CvScalar s,t;
            int num_red = 15;
    
    
            for (int i = 0; i < circles->total; i++)
            {
                // round the floats to an int
                p = (float*)cvGetSeqElem(circles, i);
                cv::Point center(cvRound(p[0]), cvRound(p[1]));
                int radius = cvRound(p[2]);
    
                s = cvGet2D(img,center.y-6, center.x);
                t = cvGet2D(img,center.y, center.x);//colour of circle
                printf("\nB: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
    
                if (s.val[2]<50 && s.val[1]<50 && s.val[0] <50)
                {
                    printf("Black Ball\n");
                }
                else if(s.val[2]>200 && s.val[1]>200 && s.val[0]>200)
                {
                    printf("White Ball\n");
                    xpos=center.x;
                    ypos=center.y;
                }
                else if(s.val[2]>=250 && s.val[1]>=250 && s.val[0] <200)
                {
                    printf("Yellow Ball\n");
                }
                else if(s.val[2]<70 && s.val[1]>80 && s.val[0] <70)
                {
                    printf("Green Ball\n");
                }
                else if(s.val[2]>100 && s.val[1]<100 && s.val[0] <100)
                {
                    printf("Brown Ball\n");
                }
                else if(s.val[2]<100 && s.val[1]<100 && s.val[0] >100)
                {
                    printf("Blue Ball\n");
                }
                else if(s.val[2]>=250 && s.val[1]<250 && s.val[0] >=250)
                {
                    printf("Pink Ball\n");
                }
                else if (s.val[2]<=255 && s.val[2]>=0 && s.val[1]<=255 && s.val[1]>=0 && s.val[0] <=255 && s.val[0]>=0)
                {
                    printf("Red Ball\n");
                    //num_red++;               
                }
    
                // draw the circle center
                cvCircle(img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
    
                // draw the circle outline
                cvCircle(img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
    
                //display coordinates
                printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
            }
    
            printf("\nDetected %d balls\n", circles->total);
    
            if (white_ball_moved && wait10sec)
            {
                // white ball detected: execute this code for the next 10 seconds
    
                cvPutText (img, "White Ball detected", cvPoint(50,50), &font, cvScalar(255,255,0));
            }
            else if (white_ball_moved && !wait10sec)
            {
                // white ball was detected and 10 seconds have passed
                cvPutText (img, "10sec passed", cvPoint(50,50), &font, cvScalar(255,255,0));
            }
    
            if (!white_ball_moved)
            {
                if (xpos >= oldx + 10 || xpos<= oldx - 10 //wait for white ball movement here.
                    && ypos >= oldy + 10 || ypos<= oldy - 10)
                {
                    oldx = xpos;
                    oldy = ypos;
                    printf("White Ball Moving");
    
                    white_ball_moved = true;
    
                    // start 2nd thread to do the counting
                    _beginthread(timer_thread, 0, NULL);
                }
            }
    
            cvShowImage("result", img);
            key = cvWaitKey(33);
        } // end of while, no more frames will be retrieved
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm detecting @replies in a Twitter stream with the following PHP code using regexes.
I have little issue with detecting internet connection while using static IP on device.
Is there some way of detecting whether an enumerable built using LINQ (to Objects
How would one go about detecting a page refresh / F5 key push on
I have problem with detecting minimum words using jquery.validation.js. As i got a script
When detecting eyes using HaarDetectObject() function, we get the results (detectedObjects) like this: [((110,
I would like some help for detecting the following expressions with RegEx: string "(x/x)"
I am working on openCV for detecting the face .I want face to get
I'm trying to look for shapes in an image using OpenCV. I know the
I am tasked with detecting anomalies (known or unknown) using machine-learning algorithms from data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.