简体   繁体   English

opencv waitkey,不精确,无法正常工作吗?

[英]opencv waitkey, imprecise, not working o so?

I'm developing a video player with C++ and OpenCV, and need high precision, but when I make a cv::waitKey, the function never waits the specified miliseconds: 我正在使用C ++和OpenCV开发视频播放器,并且需要高精度,但是当我制作cv :: waitKey时,该函数从不等待指定的毫秒数:

    tWait.resetAndRestart();
    cv::waitKey((int)(30));
    realDelay = tWait.getElapsedMsec();
    cout << "realDelay :: " << realDelay.count() << "\n";

This is the awesone output 这是Awesone的输出

...
realDelay :: 25
realDelay :: 24
realDelay :: 25
realDelay :: 21
realDelay :: 21
realDelay :: 24
realDelay :: 24
realDelay :: 20
realDelay :: 25
....

Any idea? 任何想法?

Edit: This is the timer initialization, but I think that the problem it's not here. 编辑:这是计时器初始化,但我认为问题不在这里。

void timer::start() {
    //Start the timer
    started = true;

    //Unpause the timer
    paused = false;

    //Get the current clock time    
    //startMs = std::chrono::duration_cast<milliseconds>(high_resolution_clock::now() - startTime);
    startTime = std::chrono::high_resolution_clock::now();  
}

void timer::reset()
{
    paused = false;
    started = false;
    startTime = std::chrono::high_resolution_clock::now(); 
    pauseMs = std::chrono::milliseconds(0);
}

inline std::chrono::milliseconds timer::getElapsedMsec( void )
{
//  return std::chrono::duration_cast<std::chrono::milliseconds>(high_resolution_clock::now());
    //If the timer is running
    if( started == true )
    {
        //If the timer is paused
        if( paused == true )
        {
            //Return the number of ticks when the timer was paused
            return pauseMs;
        }
        else
        {
            //Return the current time minus the start time
            return std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::high_resolution_clock::now() - startTime);
        }    
    }

    //If the timer isn't running
    return std::chrono::milliseconds(0);   
}

Finally, I found a good solution: First, I compute the desired delay, then use the minimum value in cvWaitKey and measure the real delay, and then use std::this_thread::sleep_for (who is really exact)with the corrected value: 最后,我找到了一个很好的解决方案:首先,计算所需的延迟,然后使用cvWaitKey中的最小值并测量实际延迟,然后将带有正确值的std :: this_thread :: sleep_for(实际上是准确的)使用:

    delayMeasure0 = std::chrono::high_resolution_clock::now(); 
    cv::waitKey((int)(1));          
    delayMeasure1 = std::chrono::high_resolution_clock::now(); 
    delta = std::chrono::duration_cast<std::chrono::milliseconds>(delayMeasure1-delayMeasure0);
    computedDelay -= delta;

    std::this_thread::sleep_for(computedDelay);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM