2017年10月2日月曜日

OpenCvSharpでカメラ画像を表示してみる

べた書きするとこんな感じ
カメラの番号が異なる場合はcameraPortを変更します
(タリーランプがついていれば大丈夫。異なっていればusingでexception発生)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OpenCvSharp;

namespace _01_cameraThrough
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowCamera();
        }

        static void ShowCamera()
        {
            //カメラ映像を表示
            int cameraPort = 0;
            using (var capture = Cv.CreateCameraCapture(cameraPort))
            {
                IplImage frame = new IplImage();

                //  W640 x H480 (default resolution)のウィンドウを作る
                int w = 640, h = 480;

                // カメラ映像の画サイズ設定
                Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, w);
                Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, h);

                //ESCキー押さない限り
                while (Cv.WaitKey(1) != 27)
                {
                    // 画像の取得
                    frame = Cv.QueryFrame(capture);

                    if (frame != null)
                    {
                        //frameを表示
                        Cv.ShowImage("Liveカメラ", frame);
                    }
                }
                //ESCキーが押されたら
                //全ての画像やwindowを削除
                Cv.DestroyAllWindows();
                capture.Dispose();
            }
        }
    }
}

参考にさせていただきました
http://www.thedesignium.com/development/8237

0 件のコメント:

コメントを投稿