Opencv waitkey,不精确,不工作
opencv waitkey, imprecise, not working o so?
我正在用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++中的memset函数工作不正常
- 通用线程池类工作不正常
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 给出不精确答案的递归Karatsuba算法
- 在 c++ 中逐个字符从字符串传递到 int 时不精确
- C++工作不统一,所有变量都定义
- 名为DLL的C++windows服务程序工作不正常
- C++-循环中的If语句工作不正常
- While循环和if/else语句工作不正常
- C++:最大数组值函数工作不正常
- VS为我提供了对构造函数的另一个解决方案,但我想知道为什么我的工作不起作用
- 特征矩阵 ros/cols 工作不正确
- EN_UPDATE工作不可靠
- 工会工作不正常
- 为什么这个循环运行不可见的代码?工作不正常
- C++STL映射键和值工作不正常
- C#/.NET定时器和Win32睡眠函数都是不精确的
- c++文件指针工作不正常
- 矢量 :<Mat>:size() 工作不畅
- Opencv waitkey,不精确,不工作