Skip to content

OpenCvSharp

Cross platform wrapper of OpenCV for .NET Framework.

VideoCapture Example

// ...
using OpenCvSharp;
// ...
namespace Test
{
    public partial class VisionForm : Form
    {
        private VideoCapture _vc;
        private VideoWriter _vw;
        private Mat _frame;
        private Timer _timer;

        // ...

        private void VisionForm_Load(object sender, EventArgs e)
        {
            _vc = new VideoCapture("rtsp://admin:[email protected]/h264");
            if (_vc.IsOpened() == false)
            {
                MessageBox.Show("Open Failes!!");
            }

            const int FPS = 30;

            OpenCvSharp.Size size = new OpenCvSharp.Size(_vc.FrameWidth, _vc.FrameHeight);
            _vw = new VideoWriter("Video.mp4", "H264", FPS, size);
            _frame = new Mat();

            _timer = new System.Windows.Forms.Timer();
            _timer.Interval = 1000 / FPS;
            _timer.Tick += new EventHandler(tickTimer);
            _timer.Start();
        }

        private void VisionForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            _timer.Stop();
        }

        private void VisionForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            _vc.Release();
            _vw.Release();
            _frame.Release();
        }

        void tickTimer(object sender, EventArgs e)
        {
            try
            {
                if (_vc.Read(_frame) == false)
                {
                    return;
                }

                if (_frame.Empty() == false)
                {
                    sVision.BackgroundImage = new System.Drawing.Bitmap(_frame.Cols,
                                                                        _frame.Rows,
                                                                        (int)_frame.Step(),
                                                                        System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                                                                        (IntPtr)_frame.Data);
                    _vw.Write(_frame);
                }
                else
                {
                    Console.WriteLine("ERROR!!!");
                }
            }
            finally
            {
                // EMPTY.
            }
        }

        // ...
    }
}

See also

Favorite site

References


  1. Opencvsharp.wiki.git-dd78013.zip (2017-01-16)