我正在开发一个使用C ++和OpenCV的视频播放器,并且需要高精度,但是当我创建一个cv :: waitKey时,该函数永远不会等待指定的毫秒:
tWait.resetAndRestart();
cv::waitKey((int)(30));
realDelay = tWait.getElapsedMsec();
cout << "realDelay :: " << realDelay.count() << "\n";
这是awesone输出
...
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);
}
答案 0 :(得分: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);