简体   繁体   中英

is capture time affected with imwrite opencv?

I am using C++ and opencv to capture camera images. Within this process as shown below in my code,I also measure capturing duration in milliseconds by using gettimeofday() before and after the capturing image.

Mat IMG; 
unsigned long ms;
VideoCapture cap(0);
struct timeval tp1,tp2;
while(1)
{
   gettimeofday(&tp1,NULL);
   cap>>IMG;
   gettimeofday(&tp2,NULL);
   ms=10000000*(tp1.tv_sec-tp2.tv_sec)+(tp1.tv_usec-tp2.tv_usec);
   cout<<ms/1000<<endl;
}

I know my camera can go up to maximum 60 frames per seconds. Therefore this code will output values of 15~17 ms. Now I want to save my images, therefore I use imwrite() function for that and add it after the second time I call gettimeofday() as shown below:

Mat IMG; 
unsigned long ms;
VideoCapture cap(0);
int cc=0;
struct timeval tp1,tp2;
while(1)
{
   gettimeofday(&tp1,NULL);
   cap>>IMG;
   gettimeofday(&tp2,NULL);
   ms=10000000*(tp1.tv_sec-tp2.tv_sec)+(tp1.tv_usec-tp2.tv_usec);
   cc=cc+1;
   imwrite("IMG_"+std::to_string(cc)+".png",IMG);
   cout<<ms/1000<<endl;
}

Now in this case the output will be 5~6 ms! and if I put the second call to gettimeofday() after the image writing I will get the same values of 15~17ms. How is that possible? Thanks in advance.

This happens because you only measure the time waiting on the VideoCapture .

In the first example, extracting the next frame will always block until it is ready (and only spend time there), meaning that you will see values around the inverse of your frame rate.

In the second example, the first frame should take equally long to read. However, then you spend time writing the image to the file. While this happens, the camera will start recording the next frame - meaning that when you next ask it to give you an image, part of the time needed to do that will already have elapsed, so your waiting period is shorter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM