ラベル 画像処理 の投稿を表示しています。 すべての投稿を表示
ラベル 画像処理 の投稿を表示しています。 すべての投稿を表示

2013/10/20

C#で画像処理-WEBカメラの使用-

Lv_ZEROです。

前回予告したように今回はUSB接続のWEBカメラから画像を読み込む方法を紹介していきたいと思います。
windows環境でUSB接続のWEBカメラを使用するには2通りの方法があります。1つは"DirectShow"を使う方法で、もう一つは"VFW(video for windows)"を使う方法です。"AForge.NET"では前者の"DirectShow"を使ってWEBカメラから画像を読み込む方法が実装されています。

それではプログラムの説明をしていきます。

ライブラリを使用するために「プロジェクト(P)」→「参照の追加(R)」から
・AForge.Video.dll
・AForge.Video.DirectShow.dll
をプロジェクトに追加しておいてください。さらに、
using AForge.Video;
using AForge.Video.DirectShow;
もプログラムに追加しておいてください。

WEBカメラを使用する時のプログラムの流れは以下のようになります。
①接続されている全てのビデオデバイスの取得
//ビデオ入力デバイスのみ取得
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
②使用するビデオデバイスのオブジェクト生成
//VideoDevices中のデバイス1つを選択し、そのオブジェクトを生成
int Index;
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[Index].MonikerString);
//最初に見つかったビデオデバイスを使用する場合、以下のようにIndex = 0とすればよい
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
③フレームサイズなど、ビデオデバイスの設定
//ビデオデバイスの提供する機能一覧を配列に格納
VideoCapabilities[] videoCapabilities = videoSource.VideoCapabilities;
//提供されているフレームサイズをコンボボックスのアイテムに追加
foreach ( VideoCapabilities capabilty in videoCapabilities )
{
    comboBox.Items.Add( string.Format( "{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height ) );
}
comboBox.SelectedIndex = 0;
//コンボボックスで選択されているのフレームサイズに設定
videoSource.VideoResolution = videoCapabilities[comboBox.SelectedIndex];
//このプログラムのままだと"SelectedIndex"が常に0となってしまうので、別のメソッドで設定するするように
④新しいフレームが来たら発生するイベントの登録
//新しいフレームが来たら"video_NewFrame"という名のメソッドが呼ばれる
//"=="でなく"+="になっている点に注意
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
⑤ビデオデバイスの起動
//ビデオデバイスの起動
videoSource.Start();
⑥新しいフレームが来たら処理(繰り返し)
//新しいフレームが来たら実行されるメソッド
private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    //新しいフレーム"eventArgs.Frame"のコピーを"Bitmap"に格納して扱う
    Bitmap img = (Bitmap)eventArgs.Frame.Clone();
    //ここで画像処理などを行う
    pictureBox.Image = img;//画像を"pictureBox.Image"に表示
}
⑦ビデオデバイスの停止
//ビデオデバイスの停止
videoSource.SignalToStop();
//完全に停止するまで待つ
videoSource.WaitForStop();
//これ以上使用しないならオブジェクトを空にする
videoSource = null;

細かいメソッドの役割は以上です。これを組み合わせてプログラムを作ります。
まだ紹介していないメソッドなどもありますが、基本的にこれだけ分かっていれば、自分で調べて実装することもできるでしょう。

最後に、実際にwindowsフォームで作ったプログラムを公開しておきます。
using System;
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 AForge.Video;
using AForge.Video.DirectShow;

namespace CSharpで画像処理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //接続されている全てのビデオデバイス情報を格納する変数
        private FilterInfoCollection videoDevices;
        //使用するビデオデバイス
        private VideoCaptureDevice videoDevice;
        //ビデオデバイスの機能を格納する配列
        private VideoCapabilities[] videoCapabilities;

        //ウィンドウが生成された時
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox2.Enabled = false;
            button1.Enabled = false;
            button2.Enabled = false;
        }

        //"デバイス取得"ボタンが押された時
        private void button3_Click(object sender, EventArgs e)
        {
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count != 0)
            {
                comboBox1.Items.Clear();
                foreach (FilterInfo device in videoDevices)
                {
                    comboBox1.Items.Add(device.Name);
                }
                comboBox1.SelectedIndex = 0;
                comboBox2.Enabled = true;
                button1.Enabled = true;
            }
            else
            {
                comboBox1.Items.Clear();
                comboBox1.Items.Add("デバイスが見つかりません");
                comboBox1.SelectedIndex = 0;
            }
            
        }

        //デバイスが変更された時
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            videoDevice = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoCapabilities = videoDevice.VideoCapabilities;
            comboBox2.Items.Clear();
            foreach (VideoCapabilities capabilty in videoCapabilities)
            {
                comboBox2.Items.Add(string.Format("{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height));
            }
            comboBox2.SelectedIndex = 0;
            videoCapabilities = videoDevice.VideoCapabilities;
            comboBox2.Items.Clear();
            foreach (VideoCapabilities capabilty in videoCapabilities)
            {
                comboBox2.Items.Add(string.Format("{0} x {1}", capabilty.FrameSize.Width, capabilty.FrameSize.Height));
            }
            comboBox2.SelectedIndex = 0;
        }

        //フレームサイズが変更された時
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            videoDevice.VideoResolution = videoCapabilities[comboBox2.SelectedIndex];
        }

        //"起動"ボタンが押された時
        private void button1_Click(object sender, EventArgs e)
        {
            videoDevice.NewFrame += new NewFrameEventHandler(videoDevice_NewFrame);
            videoDevice.Start();
            button1.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = false;
            comboBox1.Enabled = false;
            comboBox2.Enabled = false;
        }

        //新しいフレームが来た時
        void videoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();
            //ここで画像処理
            pictureBox1.Image = img;
        }

        //"停止"ボタンが押された時
        private void button2_Click(object sender, EventArgs e)
        {
            if (videoDevice.IsRunning)
            {
                videoDevice.SignalToStop();
                videoDevice.WaitForStop();
            }
            pictureBox1.Image = null;
            button1.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = true;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
        }

        //ウィンドウを閉じる時
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (videoDevice.IsRunning)
            {
                videoDevice.SignalToStop();
                videoDevice.WaitForStop();
                videoDevice = null;
            }
        }

        

        
    }
}

本来ならtry-catch構文でエラー回避するべきですが、コードが見ずらくなると思ったので実装していません。

これでUSB接続のWEBカメラから画像を読み込むことができましたので動画像処理も可能となりました。
次からは"AForge.NET"での画像処理を試していきたいと思います。

2013/10/18

C#で画像処理-AForge.NETの導入-

Lv_ZEROです。

C#で画像処理と言えば"OpenCV Sharp"が有名ですが、個人的には導入が面倒だったり、うまくいかなかったりと導入が大変でした。そこで、導入が簡単で、尚且つ、高性能な画像処理系のオープンソースはないかと探していたところ"AForge.NET Framework"というのを見つけました。

"AForge.NET"はコンピュータビジョンと人工知能の分野で開発や研究のために開発されたC#のためのフレームワークだそうです。主な機能は以下の内容のようです。

  • 画像処理(Kinectも対応しているようだ)
  • ニューラルネットワーク
  • 遺伝的アルゴリズム
  • 機械学習
  • ファジィ論理
  • Lego Mindstormなどのためのライブラリ

機能だけ見るとロボット工学において必要なものばかりです。しかしながら、少ししか使用していないので何が出来て、どれくらいの性能なのか、まだわかりません。情報も少なく、英語のドキュメントが唯一の手がかりな感じですが、少しずつ使い方を覚えていきたいと思います。

"AForge.NET"のホームページは以下のURLから行けます。(もちろん英語です)
http://www.aforgenet.com/

それでは、"AForge.NET"の導入方法を紹介します。

1.公式HPから"AForge.NET Framework"をダウンロードします。

2.ダウンロードしたファイルを解凍し、「Release」フォルダの中にあるdllから必要なものだけプロジェクトフォルダの中にコピーします(もちろんコピーしなくてもできます)。

3."Visual Studio"の「プロジェクト」→「参照の追加」→「参照」をクリックして、必要なdllを選択し、プロジェクトに追加できたら導入完了です。

超簡単でしたね。これでライブラリの使い方さえわかれば直ぐに開発が始められます。

"AForge.NET"による画像処理のイメージフォーマットは"Bitmap"です。しかしながら、一丸に"Bitmap"といえど、8bpp,16bpp,24bpp,32bppなどといったようにフォーマットが異なります。
"AForge.NET"ではグレースケールなら8bpp、RGBなら24bppとフォーマットを定めているようです。そこで適切なフォーマットに変換してくれるメソッドがあります。
// 画像の読み込み
System.Drawing.Bitmap image = (Bitmap) Bitmap.FromFile( fileName );
// 適切なフォーマットに変換
AForge.Imaging.Image.FormatImage( ref image );
あとは適切なフォーマットになったBitmapを使って色々処理をしていく形になります。

今日はここら辺で、次回はUSBカメラから画像を取り込む方法を紹介したいと思います。