0

I am just a starter in c++, i need to draw object on existing four markers with a delay between each marker and the other, i tried to use Chrono_ sleep_until, but it causes my program to lag, and even the object is instantly appearing and disappearing onsecond marker, i know that there is something wrong but can not guess what it may be, any help?

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) == 32) {
            cv::fillPoly(right, &pts, &num, 1, cv::Scalar(255, 0, 0));

        }
        std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now() + std::chrono::seconds(2);
        std::this_thread::sleep_until(timePoint);
    
    
            if (ids.at(i) == 45) {
            
                cv::fillPoly(right, &pts, &num, 1, cv::Scalar(255, 0, 0));
                break;
            
        
            }
M_Mohy
  • 1
  • 1
  • `sleep_until` only works accurately when you define the end time before the loop and increment it within the loop - otherwise the time will drift. Is this what you need? https://stackoverflow.com/questions/39987806/accurate-sampling-in-c/39988284#39988284 – Galik Jun 30 '20 at 06:31
  • @Galik I did so, and there is no lag anymore, but the second marker which has id 45, does not wait for the defined time, and appear, disappear blinking repeatedly. – M_Mohy Jun 30 '20 at 18:29

1 Answers1

0

sleep is great for PoCs and quick fixes for small problems, but for most serious projects you may want to consider using a timer, wait until the timer hits your delay and then continue. Then the rest of your code is still executing and no thread is sleeping.

This is an over-simplified solution just to give you the concept, it will call DrawB() 1 second after calling DrawA() without sleeping

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

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

bool bDoDrawA = true;
bool bDoDrawB = false;

void DrawA()
{
    std::cout << "DrawA()" << std::endl;

    bDoDrawB = true;
    bDoDrawA = false;
}

void DrawB()
{
    std::cout << "DrawB()" << std::endl;

    bDoDrawB = false;
    bDoDrawA = true;
    start = Clock::now();
}

int main()
{
    while (true)
    {
        if (bDoDrawA)
        {
            DrawA();
        }
        
        if (bDoDrawB)
        {
            now = Clock::now();
            duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);

            if (duration.count() >= 2000)
            {
                DrawB();
            }
        }
    }

    return 0;
}

GuidedHacking
  • 3,628
  • 1
  • 9
  • 59