【问题标题】:How to delay in c++?如何在 C++ 中延迟?
【发布时间】:2020-06-25 20:05:51
【问题描述】:

我正在开发一个依赖于四个 ARuco 标记的康复应用程序,我需要在运动序列中绘制四个标记,即对象出现在第一个标记上,当患者的手到达对象时,它会移动到下一个标记等...我可以通过选择其标记 id 仅在第一个标记上绘制,现在我需要延迟绘制下一个标记,这里是代码:

std::vector<int> ids;
    std::vector<std::vector<cv::Point2f> > corners;
    cv::aruco::detectMarkers(image, marker_dict, corners, ids);

    // Draw markers using opencv tool
    cv::aruco::drawDetectedMarkers(mid, corners, ids);

    // Draw markers custom
    for (size_t i = 0; i < corners.size(); ++i)
    {

        // Convert to integer ponits
        int num = static_cast<int>(corners[i].size());
        std::vector<cv::Point> points;
        for (size_t j = 0; j < corners[i].size(); ++j)
            points.push_back(cv::Point(static_cast<int>(corners[i][j].x), static_cast<int>(corners[i][j].y)));
        const cv::Point* pts = &(points[0]);


        // Draw


        if (ids.at(i) == 45) {
            cv::fillPoly(right, &pts, &num, 1, cv::Scalar(255, 0, 0));
        }
        

【问题讨论】:

  • 不是Sleep(int) 你在找什么?
  • delay 是指wait a fixed amount of time to do something 还是wait for something to happen (the user hand reach object) and then do something
  • 您可以创建自己的旋转循环chrono 库可能有一个可以使用的睡眠功能。操作系统可能有一个 sleep() 函数。恕我直言,延迟是在浪费 CPU 周期什么都不做。睡眠暂停您的任务(或线程),以便可以执行其他任务或线程。还有等待特定事件的概念(您必须搜索您的操作系统以查看它是否支持)。
  • @ViníciusA.L.Souza 我的意思是在第一个标记和下一个标记之间绘制的时间。

标签: c++ opencv augmented-reality markers aruco


【解决方案1】:

使用 std::chrono 库来测量已经过去的时间,当你想要的延迟已经过去时,在那个时候执行你想要的代码。

这是一个使用 while 循环检查是否已过 100 毫秒的简单示例

#include <windows.h>
#include <iostream>
#include <chrono>

int main()
{

    using Clock = std::chrono::steady_clock;
    std::chrono::time_point<std::chrono::steady_clock> start, now;
    std::chrono::milliseconds duration;

    start = Clock::now();

    while (true)
    {

        now = Clock::now();
        duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);

        if (duration.count() >= 100)
        {
            //do stuff every 100 milliseconds
            start = Clock::now();
        }
    }

    return 0;
}

也不需要睡觉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2017-08-24
    • 2017-03-20
    • 2013-01-03
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多