gpt4 book ai didi

opencv - 使用openCV和openGL显示来自网络摄像头的图像

转载 作者:行者123 更新时间:2023-12-02 16:56:28 25 4
gpt4 key购买 nike

我正在使用openCV从网络摄像头捕获图片。然后,应将框架转换为openGL纹理并显示在屏幕上。我得到以下代码,但窗口仍为黑色。我是openGL的新手,对为什么它不起作用没有更多的想法。

int main ()
{
int w = 800,h=600;

glfwInit();

//configure glfw
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);


GLFWwindow* window = glfwCreateWindow(w, h, "OpenGL", NULL, nullptr); // windowed
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

initializeCapturing();

//init GL
glViewport(0, 0, w, h); // use a screen size of WIDTH x HEIGHT
glEnable(GL_TEXTURE_2D); // Enable 2D texturing

glMatrixMode(GL_PROJECTION); // Make a simple 2D projection on the entire window
glLoadIdentity();
glOrtho(0.0, w, h, 0.0, 0.0, 100.0);

glMatrixMode(GL_MODELVIEW); // Set the matrix mode to object modeling

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window

cv::Mat frame;
captureFromWebcam(frame,capture0);


/* OpenGL texture binding of the image loaded by DevIL */
GLuint texid;
glGenTextures(1, &texid); /* Texture name generation */
glBindTexture(GL_TEXTURE_2D, texid); /* Binding of texture name */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* We will use linear interpolation for magnification filter */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); /* We will use linear interpolation for minifying filter */
glTexImage2D(GL_TEXTURE_2D, 0,3, frame.size().width, frame.size().height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); /* Texture specification */


while(!glfwWindowShouldClose(window))
{
glfwPollEvents();

if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);

// Clear color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D,texid);
glMatrixMode(GL_MODELVIEW); // Operate on model-view matrix

/* Draw a quad */
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(0, 1); glVertex2i(0, h);
glTexCoord2i(1, 1); glVertex2i(w, h);
glTexCoord2i(1, 0); glVertex2i(w, 0);
glEnd();
glFlush();
glfwSwapBuffers(window);
}


releaseCapturing();
glfwTerminate();

return 1;
}

和其他程序
cv::VideoCapture capture0;
cv::VideoCapture capture1;

void captureFromWebcam(cv::Mat &frame, cv::VideoCapture &capture)
{
capture.read(frame);
}

bool initializeCapturing()
{
capture0.open(0);
capture1.open(1);

if(!capture0.isOpened() | !capture1.isOpened())
{
std::cout << "Ein oder mehrere VideoCaptures konnten nicht geöffnet werden" << std::endl;

if(!capture0.isOpened())
capture0.release();
if(!capture1.isOpened())
capture1.release();
return false;
}

return true;
}

void releaseCapturing()
{
capture0.release();
capture1.release();
}

最佳答案

好像您混合了在不同位置找到的几个代码片段。您正在请求OpenGL 3.2核心配置文件。但是,您使用的绘图代码是具有固定功能管道的即时模式,在核心配置文件中不可用。基本上,您需要一个现代的GL上下文,但是您选择的GL版本不再完全支持绘图代码。

作为快速解决方案,您可以删除以下几行:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

这样做应为您提供一些旧的GL上下文或一些现代上下文的兼容性配置文件,具体取决于您使用的GL实现的操作系统。但是,您可以由此期望至少以这种方式获得OpenGL 2.1(除非在完全不支持GL2的非常老的硬件上使用,但即使这样对于您的代码也可以)。其余代码应在这样的上下文中工作。

我仍然建议您学习现代GL,而不要使用此处使用的旧的,已过时的旧版东西。

关于opencv - 使用openCV和openGL显示来自网络摄像头的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28011692/

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