简体   繁体   English

imwrite opencv是否会影响捕获时间?

[英]is capture time affected with imwrite opencv?

I am using C++ and opencv to capture camera images. 我正在使用C ++和opencv捕获相机图像。 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. 在下面的代码所示的过程中,我还通过在捕获图像之前和之后使用gettimeofday()来测量捕获持续时间(以毫秒为单位gettimeofday()

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. 我知道我的相机每秒最多可以拍摄60张图像。 Therefore this code will output values of 15~17 ms. 因此此代码将输出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: 现在我想保存图像,因此我使用imwrite()函数,并在第二次调用gettimeofday()之后添加它,如下所示:

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! 现在在这种情况下,输出将为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. 如果我在写入图像后第二次调用gettimeofday() ,我将获得15〜17ms的相同值。 How is that possible? 那怎么可能? Thanks in advance. 提前致谢。

This happens because you only measure the time waiting on the VideoCapture . 发生这种情况是因为您只测量等待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. 发生这种情况时,相机将开始记录下一帧-这意味着当您下次要求它给您提供图像时,执行该操作所需的部分时间已经过去,因此等待时间较短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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