【问题标题】:open CV -how can I write a code for a timer打开 CV - 我如何为计时器编写代码
【发布时间】:2015-06-29 00:05:08
【问题描述】:

如何为 open cv (c++) 编写代码,我想在其中放置一个 5 秒计时器,所以我想跟踪一个对象并在 5 秒后拾取该对象。

如何为计时器部分编写代码

提前致谢

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    您可以使用 Songho 的实现:http://www.songho.ca/misc/timer/timer.html。它非常简单方便。另一个优点:它适用于 Windows、Linux 和 Unix。

    Songho 的计时器包含两个文件,Timer.hTimer.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.
    }
    

    【讨论】:

    • 谢谢你,应该有很大帮助。抱歉,我没有正确解释。应该是手臂一直在跟踪球,但是如果球保持静止5秒钟,手臂就会把它捡起来
    • 我已尝试根据您评论中的详细信息更新我的答案。
    • 我设法添加了那部分代码,但是我仍然遇到错误: if (x >= 640 / 3 && x = 160 && y
    • 我仍然收到一些错误,来自这些函数:timer 和 timerStarted
    猜你喜欢
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多