본문 바로가기

Programming/OpenCV

[OpenCVSharp] WPF와 OpenCVSharp 연동하기

WPF의 Image를 통해 OpenCV 에서 받아오는 웹캠 이미지를 출력.

//------------------------------------------------------------------------------
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//------------------------------------------------------------------------------

namespace Microsoft.Samples.Kinect.ColorBasics
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Linq;
    using System.Diagnostics;
    using System.Collections.Generic;

    using OpenCvSharp;
    using OpenCvSharp.Extensions;

    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        CvCapture cap;
        WriteableBitmap wb;
        System.Windows.Threading.DispatcherTimer TimerClock;
        const int frameWidth = 640;
        const int frameHeight = 480;

        /// 
        /// Initializes a new instance of the MainWindow class.
        /// 
        public MainWindow()
        {
            InitializeComponent();
        }

        /// 
        /// Execute startup tasks
        /// 
        /// object sending the event
        /// event arguments
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            if (InitWebCamera())
            {
                StartTimer();
            }
            else
            {
                MessageBox.Show("웹캠에 문제가 있습니다.");
            }
        }

        private bool InitWebCamera()
        {
            try
            {
                cap = CvCapture.FromCamera(CaptureDevice.DShow, 0);
                cap.FrameWidth = frameWidth;
                cap.FrameHeight = frameHeight;
                wb = new WriteableBitmap(cap.FrameWidth, cap.FrameHeight, 96, 96, PixelFormats.Bgr24, null);
                Image.Source = wb;
                ImageResult.Source = wb;

                return true;
            }
            catch
            {
                return false;
            }
        }

        private void StartTimer()
        {
            TimerClock = new System.Windows.Threading.DispatcherTimer();
            TimerClock.Interval = new TimeSpan(0, 0, 0, 0, 33);
            TimerClock.IsEnabled = true;
            TimerClock.Tick += new EventHandler(TimerClock_Tick);
        }

        void TimerClock_Tick(object sender, EventArgs e)
        {
            using (IplImage src = cap.QueryFrame())
            {
                WriteableBitmapConverter.ToWriteableBitmap(src, wb);
            }
        }

        /// 
        /// Execute shutdown tasks
        /// 
        /// object sending the event
        /// event arguments
        private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            TimerClock.IsEnabled = false;
            if (cap != null)
            {
                cap.Dispose();
            }
        }
    }
}