Search code examples
copencvdetectionvideo-processinggeometry

opencv circle detection with video


is there a way to wait until something happens(while loop) or wait wait 10 seconds before doing a circle detection, but still displaying the video. i have tried it with while loop but if condition is not met, frames will not be displayed as code does not get the cvShow\iamge().


Solution

  • Yes, it's possible, but you will have to use threads. Declare a global variable bool exec_circle_detection = false; and start a 2nd thread. On this thread, call sleep(10) to wait for 10 seconds and then change exec_circle_detection to true.

    In the main thread, inside the frame grabbing loop, you check if the boolean variable is set to true, and in case it isn't, you won't process the frame. This part would look something like this (in C):

    char key = 0;
    while (key != 27) // ESC
    {    
      frame = cvQueryFrame(capture);
      if (!frame) 
      {
          fprintf( stderr, "!!! cvQueryFrame failed!\n" );
          break;
      }
    
      if (exec_circle_detection)
      {
          // execute custom processing
      }
    
      // Display processed frame
      cvShowImage("result", frame);
    
      // Exit when user press ESC
      key = cvWaitKey(10);
    }
    

    If you plan to do the circle detection once every 10 seconds, you will need to change exec_circle_detection to false after you execute the custom processing. On the secondary thread, adjust your code so there is a while loop changing exec_circle_detection to true every 10 seconds.