c++ - OpenCV OpenGLDrawCallback 没有被调用

标签 c++ c qt opencv opengl

我使用以下代码初始化 OpenCV 窗口:

cv::VideoCapture * stream = new cv::VideoCapture("stream_ip");
if (!stream->isOpened()){ 
    printf("Couldn't open stream! %s\n", strerror(errno));
}

//We create window with OpenGL enabled.
cv::namedWindow("rtsp_stream", cv::WINDOW_OPENGL);

//Make it fullscreen (I also tried with fixed screen size without luck.)
cv::setWindowProperty("rtsp_stream", cv::WND_PROP_FULLSCREEN, cv::WINDOW_FULLSCREEN);

//Set OpenGL context to use this window.
cv::setOpenGlContext("rtsp_stream");

//Set openGlDrawCallback.
cv::setOpenGlDrawCallback("rtsp_stream", on_opengl, NULL);

//This is the material that the image will be rendered on.
cv::Mat frame;
char k;
bool continueStream = true;

while (continueStream) {

    //We read data from the stream and write it on the frame.
    if((stream->read(frame)) != 0){
        //Then we display/render the image using imshow.
        cv::imshow("rtsp_stream", frame);
        k = cv::waitKey(1);

        //I'm not sure if updateWindow needs to be manually called to make openGLDrawCallback or if imshow calls it automatically after done rendering. So I have tried with and without it.
        //cv::updateWindow("rtsp_stream");

        switch(k){
        case 0x1b: //ESC key
            printf("Closing stream.\n");
            continueStream = false;
            break;
        }
    }
}

open_gl函数只是简单地打印一些文本以查看 opengldrawcallback 是否被调用。我还确保我在 OpenCV 上启用了 OpenGL 和 QT std::cout << cv::getBuildInformation() << std::endl; 。我尝试从许多不同的网站和来源找到解决方案,包括 this book 。程序在各个方面都按预期工作,除了 openglcallback 永远不会被调用。非常感谢所有的帮助。

最佳答案

好的。正如我昨天发现的那样,cv::imshow 阻止我在该窗口上使用 OpenGL 命令。

因此,为了克服这个问题,我必须做的是读取 cv::Mat 上的数据,就像以前一样。但是,我必须首先将数据存储在纹理上,然后在屏幕上渲染该纹理,而不是使用 cv::imshow 直接在窗口上渲染该 cv::Mat 。为了将数据存储在纹理上,我使用了以下方法:

/**
*texture: Pointer to OpenGL texture that we want to render our stream on.
*data: cv::Mat that contains the data we want to render.
**/
void storeStreamToTexture(GLuint texture, cv::Mat* data){
    //Bind the texture we want to render to.
    glBindTexture(GL_TEXTURE_2D, texture);
    //we flip the Mat to start reading from the beginning.
    cv::flip(*data, *data, 0);
    //Store mat data to our texture.
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, data->cols, data->rows, 0, GL_BGR, GL_UNSIGNED_BYTE, data->data);
}

之后我们可以像使用 OpenGL 上的任何其他纹理一样使用我们的纹理。希望将来有人发现这很有用! :)

关于c++ - OpenCV OpenGLDrawCallback 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50582999/

相关文章:

c - 如何将变量传递给另一个函数?

c++ - QObject::connect: 没有这样的信号(类名)(信号名)(atribure)

c++ - 未为 QTableView 行调用 QStyledItemDelegate 的 sizeHint 方法

c++ - Qt 连接 lambda

c++ - 模板选择 const 引用而不是 const 指针

c++ - %s 的 printf() 的不同运行时检查?

c - 库的头文件中的结构定义和编译差异

c++ - 有什么办法可以在网页上显示 C++?

c++ - 关闭cin与C scanf同步的弊端

c++ - 我如何在 Qt 中使用 itemFromIndex