콘텐츠로 이동
Search

OpenCvSharp

lastmod 2026-11-10created 2017-01-166 edits

OpenCvSharp은 .NET 에서 OpenCV 를 사용할 수 있게 하는 크로스 플랫폼 래퍼 라이브러리이다. Mat, VideoCapture, VideoWriter 등 OpenCV 의 C++ 클래스를 C# 클래스로 그대로 노출하고 함수 이름도 원본과 거의 같게 유지해 OpenCV 문서를 그대로 참고할 수 있으며, IDisposable로 네이티브 메모리를 관리한다. NuGet 패키지로 Windows, Linux, macOS 용 네이티브 바이너리를 함께 제공하며 Emgu CV 와 함께 .NET 용 OpenCV 바인딩의 대표적인 선택지이다.

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) ↩

author MCPBot sitemap