python C#でopencvの例が山ほど上がっているが
vb.netが見当たらないのでアップしてみた
1.事前準備
vbのフォームアプリを作成
giuhubでOpenCvSharpと検索
OpenCvSharp4.Windowsと
OenCvSharp4.Extensionsをインストール
注意: OpenCvSharp4でなく OpenCvSharp4.Windows
2.ネットで検索するとpythonもC#もwhile文での取り込みで
なぜかvb.netでCv2.WaitKey()が利かない
よってtimerで作成する事にした
3.importsの定義
Imports OpenCvSharp
Imports OpenCvSharp.Extensions
4.フォーム共通エリアでキャプチャーとマットの定義
Dim capture As VideoCapture = Nothing
Private flame As Mat = Nothing
5.ピクチャ-ボックスの初期化とサイズ
‘PictureBoxのサイズに合わせて表示
PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
PictureBox2.SizeMode = PictureBoxSizeMode.Zoom
PictureBox1.Image = Nothing
PictureBox2.Image = Nothing
6.スタートボタン
VideoCaptureのインスタンスの作成
capture = New VideoCapture()
capture.Open(0)
If capture.IsOpened() = False Then
MsgBox(“capture initialization failed”, vbInformation)
End If
タイマースタート
Timer1.Interval = 30
Timer1.Start()
7.タイマーイベント
flame = New Mat()
capture.Read(flame)
If flame.Empty() Then
Exit Sub
End If
If flame.Size().Width > 0 Then
‘PictureBoxに表示 MatをBitMapに変換 PictureBox1.Image = BitmapConverter.ToBitmap(flame)
End If
8.ボタン2でキャプチャー
If flame Is Nothing Then Return End If
‘//PictureBoxに表示 MatをBitMapに変換
PictureBox2.Image = BitmapConverter.ToBitmap(flame)
9.ボタン3で終わり
Timer1.Stop()
capture.Dispose()
PictureBox1.Image = Nothing
10.全ソース
Imports OpenCvSharp Imports OpenCvSharp.Extensions Public Class Form1 Dim capture As VideoCapture = Nothing Private flame As Mat = Nothing Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'PictureBoxのサイズに合わせて表示 PictureBox1.SizeMode = PictureBoxSizeMode.Zoom PictureBox2.SizeMode = PictureBoxSizeMode.Zoom PictureBox1.Image = Nothing PictureBox2.Image = Nothing End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick flame = New Mat() capture.Read(flame) If flame.Empty() Then Exit Sub End If If flame.Size().Width > 0 Then 'PictureBoxに表示 MatをBitMapに変換 PictureBox1.Image = BitmapConverter.ToBitmap(flame) End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click capture = New VideoCapture() capture.Open(0) If capture.IsOpened() = False Then MsgBox("capture initialization failed", vbInformation) End If Timer1.Interval = 30 Timer1.Start() End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If flame Is Nothing Then Return End If '//PictureBoxに表示 MatをBitMapに変換 PictureBox2.Image = BitmapConverter.ToBitmap(flame) End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Timer1.Stop() capture.Dispose() PictureBox1.Image = Nothing End Sub
サンプル通りだと以下になるがあまりwhile文はおすすめできない
かつ
cv2.wait()がきかない
Imports OpenCvSharp Imports OpenCvSharp.Extensions Public Class Form1 Private flame As Mat Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'PictureBoxのサイズに合わせて表示 PictureBox1.SizeMode = PictureBoxSizeMode.Zoom PictureBox2.SizeMode = PictureBoxSizeMode.Zoom End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim capture As VideoCapture = New VideoCapture() capture.Open(0) If capture.IsOpened() = False Then MsgBox("capture initialization failed", vbInformation) End If '画像取得用のMatを作成 flame = New Mat() Do While True Try capture.Read(flame) If flame.Empty() Then Exit Do End If If flame.Size().Width > 0 Then 'PictureBoxに表示 MatをBitMapに変換 PictureBox1.Image = BitmapConverter.ToBitmap(flame) End If Dim key As Integer = Cv2.WaitKey() If IsDisposed Then Exit Do End If Catch ex As Exception Exit Do End Try Loop capture.Dispose() PictureBox1.Image = Nothing End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If flame Is Nothing Then Return End If '//PictureBoxに表示 MatをBitMapに変換 PictureBox2.Image = BitmapConverter.ToBitmap(flame) End Sub End Class