gpt4 book ai didi

c# - OpenCV 网络摄像头帧到 OpenGL 纹理

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:00 27 4
gpt4 key购买 nike

我正在使用 C# 并使用 OpenTK(OpenGL 包装器)和 EmguCV(OpenCV 包装器)。

我想做的很容易理解:获取网络摄像头视频流并将其放在 GLControl 上。

  1. 我有一个名为 Capturer 的静态,它有一个方法可以捕获帧并将其作为 cv::Mat 返回 包装对象:

    internal static void Initialize()
    {
    cap = new VideoCapture(1);
    cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 25);
    cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 1920);
    cap.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 1080);
    }

    internal static Mat GetCurrentFrame()
    {
    mat = cap.QueryFrame();
    if (!mat.IsEmpty)
    {
    return mat;
    }
    return null;
    }
  2. 现在在我的 GLControl Load 事件 中我初始化捕获器和 OpenGL:

        Capturer.Initialize();

    GL.ClearColor(Color.Blue);
    GL.Enable(EnableCap.Texture2D);

    GL.Viewport(-glControl1.Width, -glControl1.Height, glControl1.Width * 2, glControl1.Height * 2);
  3. 最后,在 GLControl Paint 事件中:

        GL.Clear(ClearBufferMask.ColorBufferBit);

    GL.MatrixMode(MatrixMode.Projection);
    GL.LoadIdentity();

    Mat m = Capturer.GetCurrentFrame();
    if (m != null)
    {
    GL.GenTextures(1, out textureId);
    GL.BindTexture(TextureTarget.Texture2D, this.textureId);

    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)TextureMinFilter.Nearest);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)TextureMagFilter.Linear);

    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (float)TextureWrapMode.Clamp);
    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (float)TextureWrapMode.Clamp);

    GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, 1920, 1080, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgr, PixelType.UnsignedByte, m.DataPointer);
    }
    m.Dispose();

    glControl1.SwapBuffers();
    glControl1.Invalidate();

这是一个完整的蓝屏。我认为错误在 m.DataPointer 上。

(我已经尝试使用 m.Bitmap 属性使用 Bitmap 渲染帧,它可以工作,但性能很差。)

最佳答案

绘制一个包围 GLControl 的矩形解决了这个问题:

            GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
GL.TexCoord2(0, 1); GL.Vertex2(0, 1);
GL.TexCoord2(1, 1); GL.Vertex2(1, 1);
GL.TexCoord2(1, 0); GL.Vertex2(1, 0);
GL.End();
m.Dispose();

请务必在绘制框架后释放对象,以免内存不足。

关于c# - OpenCV 网络摄像头帧到 OpenGL 纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45279061/

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