【发布时间】:2015-06-29 00:05:08
【问题描述】:
如何为 open cv (c++) 编写代码,我想在其中放置一个 5 秒计时器,所以我想跟踪一个对象并在 5 秒后拾取该对象。
如何为计时器部分编写代码
提前致谢
【问题讨论】:
如何为 open cv (c++) 编写代码,我想在其中放置一个 5 秒计时器,所以我想跟踪一个对象并在 5 秒后拾取该对象。
如何为计时器部分编写代码
提前致谢
【问题讨论】:
您可以使用 Songho 的实现:http://www.songho.ca/misc/timer/timer.html。它非常简单方便。另一个优点:它适用于 Windows、Linux 和 Unix。
Songho 的计时器包含两个文件,Timer.h 和 Timer.cpp,您只需将它们添加到项目中即可。它的用法很简单(复制自上面的链接):
#include <iostream>
#include "Timer.h"
using namespace std;
int main()
{
Timer timer;
// start timer
timer.start();
// do something
...
// stop timer
timer.stop();
// print the elapsed time in millisec
cout << timer.getElapsedTimeInMilliSec() << " ms.\n";
return 0;
}
如果你不喜欢这个计时器,你仍然可以把它作为你自己的参考。
您没有添加很多关于您的用例的详细信息,但一个 hack-ish 解决方案可能包括在您开始跟踪的同时启动计时器,然后在每次迭代时检查计时器的值算法:
bool timerStarted = false;
bool reallyStationary = false;
while( ( ! reallyStationary ) && /* other conditions */ )
{
// Track the object ...
if ( /* object is stationary */ )
{
if ( timerStarted )
{
if ( timer.getElapsedTimeInSec() < 5 )
reallyStationary = false; // Continue the while-loop.
else
reallyStationary = true; // Leave the while-loop.
}
else
{
timer.start()
timerStarted = true;
}
}
else
{
if ( timerStarted )
{
timerStarted = false;
timer.stop();
}
}
}
if ( reallyStationary )
{
// Pick up an object.
}
【讨论】: