is-6ren">
gpt4 book ai didi

c++ - OpenCV OpenGLDrawCallback 没有被调用

转载 作者:行者123 更新时间:2023-11-30 16:31:26 25 4
gpt4 key购买 nike

我使用以下代码初始化 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/

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