OpenCV grab does nothing on webcam

后端 未结 2 937
猫巷女王i
猫巷女王i 2021-01-25 06:11

I\'ve been trying to simultaneously grab frames from two different cameras, but no matter how many times I call VideoCapture::grab(), there seems to be no effect. The frame retr

相关标签:
2条回答
  • 2021-01-25 07:04

    I ran my test and note very strange behavior of .garab and .retrive functions. This is example:

    cv::Mat input = cv::Mat(512, 512, CV_8UC1, cv::Scalar(0));
    cv::VideoCapture cap(0);
    
    while (true)
    {
        cap.grab();
        cap.retrieve(input, 5);
        cv::imshow("input", input);
        cv::waitKey(0);
    }
    

    If you press any key slowly, about every 5 seconds, and change something in front of the camera between pressing, the position of the object on the image will change every second image showing, that is, every second call of the .grab and .retrive functions.

    If you press any key quickly, about every 1 seconds, and also change something in front of the camera between pressing, the position of the object on the image will be changed every image showing.

    This circumstance tell about that this function can be used to sync cameras.

    0 讨论(0)
  • 2021-01-25 07:12

    Please try this code with the following instructions:

    1. before and while you start the program, hold a red sheet in front of the camera. That moment, the first .grab will be called.

    2. Once you see the black window popping up, remove the red sheet and hold a blue sheet or something else (except the red or the green sheet) in front of the camera. Then press keyboard key 'q'.

    3. Now you have 5 seconds time to change the scene again. Hold hold the green sheet in front of the camera and wait. The black window will be switched to one of your camera images.

      int main(int argc, char* argv[])
      {
          cv::Mat input = cv::Mat(512,512,CV_8UC1, cv::Scalar(0));
      
          cv::VideoCapture cap(0);
      
      
          while (cv::waitKey(10) != 'q')
          {
              cap.grab();
              cv::imshow("input", input);
          }
          cv::waitKey(5000);
          cap.retrieve(input);
      
          cv::imshow("input", input);
          cv::waitKey(0);
          return 0;
      }
      

    3 possible results:

    1. you see the red sheet: this means that the first grab was called and fixed the image, until a retrieve was called.

    2. you see blue sheet: this means every .grab call "removes" one image and the camera will capture a new image on the next call of .grab

    3. you see the green sheet: this means your .retrieve doesn't need a .grab at all and just grabs images automatically.

    For me, result 1 occurs, so you can't grab and grab and grab and just .retrieve the last image.


    Test 2: control about everything:

    • looks like you are right, on my machine no matter when or how often I call grab, the next image will be the one captured at the time when I called the previous .retrieve and the calls of .grab don't seem to influence the time position of capturing at all.

    Would be very interesting whether the same behaviour occurs for different (all) kind of cameras and operating systems.

    I've tested on the internal camera of a T450s and Windows 7.

    int main(int argc, char* argv[])
    {
        cv::Mat input = cv::Mat(512,512,CV_8UC1, cv::Scalar(0));
    
        cv::VideoCapture cap(0);
    
        bool grabbed;
        bool retrieved;
    
        while (true)
        {
            char w = cv::waitKey(0);
            switch (w)
            {
            case 'q':  return 0;
            case 27:  return 0;
            case ' ': retrieved = cap.retrieve(input); break;
            case 'p': grabbed = cap.grab(); break;
            }
    
            cv::imshow("input", input);
        }
    
        return 0;
    }
    

    In addition, this simple code seems to be off 1 frame for my camera (which therefore probably has a buffersize of 2??):

    while (true)
        {
            cap >> input;
    
            cv::imshow("input", input);
            cv::waitKey(0);
        }
    
    0 讨论(0)
提交回复
热议问题