gpt4 book ai didi

c++ - opencv waitkey,不精确,不起作用?

转载 作者:行者123 更新时间:2023-11-28 06:50:03 25 4
gpt4 key购买 nike

我正在用 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);

关于c++ - opencv waitkey,不精确,不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24124746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com