【问题标题】:C# Emgucv turn off webcamC# Emgucv 关闭网络摄像头
【发布时间】:2018-06-01 17:17:22
【问题描述】:

目前我的程序可以打开网络摄像头然后动态捕捉人脸,但是我不知道如何停止摄像头,因为即使窗户关闭它也会继续捕捉人脸。

private static VideoCapture _cameraCapture;

public VideoSurveilance()
    {
        InitializeComponent();
        Run();

    }


    void Run()
    {

        try
        {
            _cameraCapture = new VideoCapture();


        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return;
        }

        _fgDetector = new 
        Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
        _blobDetector = new CvBlobDetector();
        _tracker = new CvTracks();

        Application.Idle += ProcessFrame;
    }

    private void btnStopCamera_Click(object sender, EventArgs e)
    {

        _cameraCapture.Pause();//not working
        _cameraCapture.Stop();//not working
        _cameraCapture.Dispose();//worked but crashed due to memory issue

        this.Close();

        faceManipulate fm = new faceManipulate();
        fm.Show();

内存问题已解决。但是Dispose会导致进程框架为Null Reference Object。

void ProcessFrame(object sender, EventArgs e)
    {
        Mat frame = _cameraCapture.QueryFrame();
        Mat smoothedFrame = new Mat();
        CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); 
}

【问题讨论】:

    标签: c# webcam emgucv


    【解决方案1】:

    你已经解决了这个问题,你应该调用 Dispose 方法。

    CameraCapture 实现了 DisposableObject,您不应该将其作为静态变量,而应将其保留为变量并在完成后处理。

    我看到你说它“工作但由于内存问题而崩溃”,如果这仍然是一个问题,请在下面发布一个问题或评论来描述内存问题。

    【讨论】:

    • 谢谢。是否可以停止 ProcessFrame 方法?
    • 您应该在尝试查询帧之前检查 _cameraCapture != null 是否
    • 谢谢.....问题已解决。正如你所说,需要使用 if else 语句检查 _cameraCapture。
    • 很高兴能为您提供帮助,请接受答案,以免其他人认为这仍然需要答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2014-09-26
    • 2022-01-11
    相关资源
    最近更新 更多