728x90
300x250
[C#](OpenCVSharp 2.4.10) - 카메라 인식

 

이번에 소개할 것은 Nuget 패키지의 "OpenCVSharp-AnyCPU"를 설치하여 카메라 인식을 소개할 것입니다.

레거시 상으로는 지원하지 않으나 현재 가장 안정화된 버전이라고 할 수 있습니다.

(This time, we will introduce camera recognition by installing "OpenCVSharp-AnyCPU" of Nuget package.)

(It's not supported legacy, but it's still the most stable version.)

 


1. 프로젝트에 라이브러리 설치하기(Install the library in your project)

 

프로젝트(P)->Nuget 패키지->"OpenCVSharp-Any"를 검색합니다.

OpenCVSharp 4 사용을 권유하는 문구가 있겠으나 무시하고 2.4를 설치하도록 합니다.

(Search for Project (P)-> Nuget Package-> "OpenCVSharp-Any".)

(Some suggest that you use OpenCVSharp 4, but ignore it and install 2.4.)

 

 

 


2. 사용자 인터페이스 설계(User Interface Design)

 

PictureBoxlpl, Timer1을 배치합니다.

(Place PictureBoxlpl, Timer1.)

 

 

pictureBoxlpl1의 Size는 640 x 480으로 해줍니다.

(The size of pictureBoxlpl1 is 640 x 480.)

 

 

timer1의 Interval은 33입니다.

timer1의 Enabled는 true입니다.

(timer1 has an Interval of 33.)

(Enabled for timer1 is true.)

 

 

 

 

 

폼 환경의 Load, FormClosing을 더블클릭해서 소스코드 작성이 가능한 상태로 해줍니다.

(Double-click Load, FormClosing in the form environment to make the source code available.)

 

 

 

 

 

다음은 Timer1에 관한 설정입니다.

Tick를 더블클릭하여 소스코드 작성이 가능한 상태가 되도록 해줍니다.

(The following are the settings for Timer1.)

(Double click Tick to make it possible to write source code.)

 

 


3. 소스코드 입력하기(Enter the source code)

 

소스코드를 타이핑 합니다.(Type the source code.)

 


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;

namespace OpenCVSharp_VideoCapture
{
    public partial class Form1 : Form
    {
        CvCapture capture;                        // OpenCVSharp 4부터 미지원
        IplImage src;                                 // OpenCVSharp 4부터 미지원

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            src = capture.QueryFrame();
            pictureBoxIpl1.ImageIpl = src;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                capture = CvCapture.FromCamera(CaptureDevice.DShow, 0);

                /// Tip : 카메라를 2개 이상 이용한다면 0이 아닌 1로 입력하면 외부 카메라로 인식되어 출력됩니다.

                /// Tip : 동영상 파일 사용시 CvCapture.FromFile("경로")를 사용하여 동영상을 재생시킬 수 있습니다.
                capture.SetCaptureProperty(CaptureProperty.FrameWidth, 640);
                capture.SetCaptureProperty(CaptureProperty.FrameHeight, 480);
            }
            catch
            {
                timer1.Enabled = false;
            }

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cv.ReleaseImage(src);
            if (src != null)
                src.Dispose();
        }

    }

}

 


4. 시연(Demonstrate)

 

아래의 그림은 동영상 재생을 시연한 것입니다. 모자이크 처리하였습니다.

 

 

반응형

+ Recent posts