-2

I am working on a rehabilitation application that relies on four ARuco markers,i need to draw on the four markers in the exercise sequence i.e. the object appears on the first marker, when the patient's hand reaches the object, it moves to the next marker, etc... . I could draw on only the first marker by selecting its marker id, now i need to make a delay to draw on the next marker, here is the code:

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));
        }
        
M_Mohy
  • 1
  • 1
  • isn't `Sleep(int)` what you are looking for ? – user Jun 25 '20 at 20:10
  • [https://en.cppreference.com/w/cpp/thread/sleep_for](https://en.cppreference.com/w/cpp/thread/sleep_for) – drescherjm Jun 25 '20 at 20:19
  • By `delay` you mean `wait a fixed amount of time to do something` or you mean `wait for something to happen (the user hand reach object) and then do something` ? – Vinícius A. L. Souza Jun 25 '20 at 20:46
  • 1
    You can create your own *spin loop*. The `chrono` library may have a sleep function you can use. Operating systems may have a sleep() function. IMHO, delay is wasting CPU cycles doing nothing. A sleep suspends your task (or thread), so that other tasks or threads can be executed. There is also the concept of waiting for a specific event (you'll have to search your OS to see if it supports this). – Thomas Matthews Jun 25 '20 at 21:17
  • @ViníciusA.L.Souza I mean a time between drawing on the first marker, and the following one. – M_Mohy Jun 28 '20 at 07:42

2 Answers2

1

Use the std::chrono library to measure time that has passed, when your desired delay has passed, execute the code you want at that time.

Here is a simple example using a while loop which checks if 100 milliseconds have passed

#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;
}

No sleep necessary either.

GuidedHacking
  • 3,628
  • 1
  • 9
  • 59
0

You can use the headers chromo and thread. After the function, you can put the sleep(); anywhere with the amount of seconds in the brackets. You can use decimals and whole numbers.

#include <iostream>
#include <chromo>
#include <thread>
void sleep(float seconds){
clock_t startClock = clock();
float secondsAhead = seconds * CLOCKS_PER_SEC;
// do nothing until the elapsed time has passed.
while(clock() < startClock+secondsAhead);
return;
}
int main(){
sleep(2); 
cout << "Hello " << endl;
sleep(2.5);
cout << "World!" << endl;

return 0;   
}