0

I have following code in C# using emgu to camera capture:

    //video capture
    private Capture videoCapture = null;  //takes images from camera as image frames
    private Image<Bgr, Byte> videoCaptureImageFrame;
    private Image<Bgr, Byte> videoCaptureResizedFrame;
    //video capture

    private void ProcessFrame(object sender, EventArgs arg)
    {
        try
        {
            videoCaptureImageFrame = videoCapture.QueryFrame().ToImage<Bgr, Byte>();   

            if (videoCaptureImageFrame != null)
            {
                videoCaptureResizedFrame = videoCaptureImageFrame.Resize(960, 540, Emgu.CV.CvEnum.Inter.Cubic);                                        
                VideoCapturePictureBox.Image = videoCaptureResizedFrame.ToBitmap();                      
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show("Video capture error #1: " + ex.Message.ToString());
        }
    }

    public void VideoCaptureReleaseData()
    {
        if (videoCapture != null)
            videoCapture.Dispose();
    }

    //video capture  
    private void MainForm_Load(object sender, EventArgs e)
    {
        //Dispose of Capture if it was created before
        if (videoCapture != null) videoCapture.Dispose();

        //video capture
        if (videoCapture == null)
        {                
            try
            {
                videoCapture = new Capture(0);
                videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 1920);
                videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 1080);
                videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameCount, 25);

                Application.Idle += ProcessFrame;
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show("Video capture error #2: " + excpt.Message);
            }
        }
        //video capture
    }

This code works fine but I can see that from time to time Visual Studio 2015 show process memory consumption of 2GB of data. Sometimes I got following error:
"Video capture error #1: opencv: u != 0"
and application stops to show any camera output.

I assume I have some kind of memory leak in above code.
This is strange because I wrote this code according to tutorials.
Could you please help me what is wrong with this code?

Przemo
  • 193
  • 2
  • 16
  • All `Image` are disposable and should be disposed when possible. Even the output of `videoCapture.QueryFrame()` returns a `Image` and should be disposed. Ref: http://www.emgu.com/wiki/files/2.0.0.0/html/8dfb0679-4c54-4f17-37ba-7218cbd28e67.htm – Caramiriel Nov 27 '16 at 12:03
  • Thank you very much. Now I have 350 MB of process memory and no errors. – Przemo Nov 27 '16 at 12:19

0 Answers0