OpenCV网络摄像头帧到OpenGL纹理

时间:2017-07-24 11:09:13

标签: c# opencv opengl emgucv opentk

我正在使用C#并使用iconView(OpenGL包装器)和 let marker = GMSMarker() marker.iconView = UIView() //Your custom view here (OpenCV包装器)。

我想要做的事情很容易理解:抓住网络摄像头视频流并将其放在from django.db import transaction @receiver(post_save, sender=MyModel) def my_model_post_save(sender, instance, **kwargs): transaction.on_commit(lambda: handle_save_task.apply_async(args=(instance.pk,))) 上。

  1. 我有一个名为OpenTK的静态EmguCV,它有一个方法可以捕获一个帧并将其作为GLControl包装对象返回:< / p>

    class
  2. 现在我的Capturer cv::Mat我初始化了捕获器和OpenGL

    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;
    }
    
  3. 最后,在GLControl Load event 中:

        Capturer.Initialize();
    
        GL.ClearColor(Color.Blue);
        GL.Enable(EnableCap.Texture2D);
    
        GL.Viewport(-glControl1.Width, -glControl1.Height, glControl1.Width * 2, glControl1.Height * 2);
    
  4. 这显示完整的蓝屏。我认为错误发生在GLControl

    (我尝试使用属性Paint event使用 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(); 渲染帧并且它可以正常运行,但性能非常糟糕。)

1 个答案:

答案 0 :(得分:1)

绘制一个界定 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(); 的矩形解决了它:

    import android.support.v7.*;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;

    public class MainActivity extends AppCompatActivity {
    Spinner spinner1,spinner2;
    ArrayAdapter<CharSequence> adapter;
    int position1,position2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner1=(Spinner)findViewById(R.id.spinner);
        spinner2=(Spinner)findViewById(R.id.spinner2);
        adapter=ArrayAdapter.createFromResource(this,R.array.items,android.R.layout.simple_dropdown_item_1line);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner1.setAdapter(adapter);
        spinner2.setAdapter(adapter);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                position1=parent.getSelectedItemPosition();
                    if (position1==position2){
                        position2++;
                    }



                spinner2.setSelection(position2);


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                position2=parent.getSelectedItemPosition();
                if (position2==position1){
                    position1++;
                }
                spinner1.setSelection(position1);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


    }
}

请务必在绘制框架后丢弃对象,这样就不会耗尽内存。