c# Opencvsharp 3 camcorder capture on windows 窗体

标签 c# opencv

大家好,我是 opencvsharp 编程的新手。我正在尝试制作一个程序,通过图片框流式传输我的相机 View 。 while 循环使我的程序崩溃。没有循环它工作正常,虽然它只显示一张图片。我正在使用 opencvsharp3。

    VideoCapture capture;
    Mat frame;
    Bitmap image;


public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    if (button1.Text.Equals("Start"))
    {
        frame = new Mat();
        capture = new VideoCapture();
        capture.Open(2);
        capture.Read(frame);
        Bitmap image = BitmapConverter.ToBitmap(frame);
        while (true)
        {
            pictureBox1.Image = image;
        }
        button1.Text = "Stop";
    }
    else
    {
        capture.Release();
        button1.Text = "Start";
    }
}

更新:感谢 GuidoG 的评论,我已经弄明白了。

    VideoCapture capture;
    Mat frame;
    Bitmap image;
    private Thread camera;
    int isCameraRunning = 0;

    private void CaptureCamera()
    {
      camera = new Thread(new ThreadStart(CaptureCameraCallback));
      camera.Start();
    }

    private void CaptureCameraCallback()
    {
        frame = new Mat();
        capture = new VideoCapture();
        capture.Open(2);
        while (isCameraRunning == 1)
        {
            capture.Read(frame);
            image = BitmapConverter.ToBitmap(frame);
            pictureBox1.Image = image;
            image = null;
        }

    }
    public Form1()
    {
        InitializeComponent();

    }

   private void button1_Click(object sender, EventArgs e)
    {
         if (button1.Text.Equals("Start"))
            {
            CaptureCamera();
            button1.Text = "Stop";
            isCameraRunning = 1;
            }
            else
            {
            capture.Release();
            button1.Text = "Start";
            isCameraRunning = 0;
            }
    }

}
}

最佳答案

private void CaptureCameraCallback()
{
    frame = new Mat();
    capture = new VideoCapture();
    capture.Open(2);
    while (isCameraRunning == 1)
    {
        capture.Read(frame);
        image = BitmapConverter.ToBitmap(frame);
        pictureBox1.Image = image;
        image = null;
    }

}

该部分应放在 beginBeginInvoke() 中。 您需要创建一个委托(delegate)函数

关于c# Opencvsharp 3 camcorder capture on windows 窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39450823/

相关文章:

c# - 在 Windows 窗体中使用 Windows 7 风格的菜单条

python - 如何为 openCV SVM 格式化数据

c++ - OpenCv - 内存位置 C++ 处的 cv::Exception

iOS:使用哪种增强现实 SDK 用于虚拟试衣间?

python - OpenCV MSER 检测文本区域 - Python

java - 通过网络写入数据

c# - 一种使用 ThreadPool 跟踪失败工作人员的可靠方法

javascript - 在 jQuery : equivalent to Html. Raw() 中将字符串从服务器转换为 HTML?

c++ - 如何使用OpenCV识别夜空照片中的星星?

c# - 在 Mono 上运行 eBay C# SDK 时出现未处理的异常