替代waitKey()用于更新OpenCV中的窗口 [英] Alternative to waitKey() for updating window in OpenCV

查看:419
本文介绍了替代waitKey()用于更新OpenCV中的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我所看到的所有示例和书籍都建议使用waitKey(1)强制重新绘制OpenCV窗口.看起来很怪异,而且太客气了.为什么不用时甚至还要等待1毫秒?

All examples and books I've seen so far recommends using waitKey(1) to force repaint OpenCV window. That looks weird and too hacky. Why wait for even 1ms when you don't have to?

还有其他选择吗?我尝试了cv :: updateWindow,但它似乎需要OpenGL,因此崩溃了.我在Windows上使用VC ++.

Are there any alternatives? I tried cv::updateWindow but it seems to require OpenGL and therefore crashes. I'm using VC++ on Windows.

推荐答案

我查看了源代码,正如@Dan Masek所说,似乎没有任何其他功能可以处理Windows消息.因此,我最终为VC ++编写了自己的小DoEvents()函数.以下是使用OpenCV在跳过所需的帧数的同时逐帧显示视频的完整源代码.

I looked in to source and as @Dan Masek said, there doesn't seem to be any other functions to process windows message. So I ended up writing my own little DoEvents() function for VC++. Below is the full source code that uses OpenCV to display video frame by frame while skipping desired number of frames.

#include <windows.h>
#include <iostream>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;
bool DoEvents();

int main(int argc, char *argv[])
{
    VideoCapture cap(argv[1]);
    if (!cap.isOpened())
        return -1;

    namedWindow("tree", CV_GUI_EXPANDED | CV_WINDOW_AUTOSIZE);
    double frnb(cap.get(CV_CAP_PROP_FRAME_COUNT));
    std::cout << "frame count = " << frnb << endl;

    for (double fIdx = 0; fIdx < frnb; fIdx += 50) {
        Mat frame;
        cap.set(CV_CAP_PROP_POS_FRAMES, fIdx);
        bool success = cap.read(frame);
        if (!success) {
            cout << "Cannot read  frame " << endl;
            break;
        }
        imshow("tree", frame);
        if (!DoEvents())
            return 0;
    }
    return 0;
}

bool DoEvents()
{
    MSG msg;
    BOOL result;

    while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
    {
        result = ::GetMessage(&msg, NULL, 0, 0);
        if (result == 0) // WM_QUIT
        {
            ::PostQuitMessage(msg.wParam);
            return false;
        }
        else if (result == -1)
            return true;    //error occured
        else
        {
            ::TranslateMessage(&msg);
            ::DispatchMessage(&msg);
        }
    }

    return true;
}

这篇关于替代waitKey()用于更新OpenCV中的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆