Opencv waitkey,不精确,不工作

opencv waitkey, imprecise, not working o so?

本文关键字:工作 不精确 Opencv waitkey      更新时间:2023-10-16

我正在用c++和OpenCV开发一个视频播放器,需要高精度,但是当我制作cv::waitKey时,该函数从不等待指定的毫秒数:

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

这是很棒的输出

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

任何想法?

编辑:这是定时器初始化,但我认为问题不在这里。

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

最后,我找到了一个好的解决方案:首先,我计算所需的延迟,然后使用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);