1. 程式人生 > >EmguCV中的實時視訊顯示

EmguCV中的實時視訊顯示

EmguCV中的Capture類可以完成視訊檔案的讀取,並且能捕獲每一幀。

捕獲的幀可繫結事件,實時對捕獲的圖片進行處理。

Imports Emgu
Imports Emgu.CV
Imports Emgu.CV.Capture
Imports Emgu.CV.CvEnum
Imports Emgu.CV.Structure
Imports Emgu.Util
Imports Emgu.CV.Util


Public Class Form1

   Dim _capture As Capture = Nothing
   Dim _captureInProgress As Boolean
   Private Sub initCapture()
        _capture = New Capture()
        _capture.SetCaptureProperty(CapProp.FrameWidth, 1600)
        _capture.SetCaptureProperty(CapProp.FrameHeight, 1200)
        _capture.SetCaptureProperty(CapProp.FrameCount, 1)
        AddHandler _capture.ImageGrabbed, AddressOf ProcessFrame
       '啟動攝像頭
        switchCapture()
    End Sub

    Sub switchCapture()
        '啟停攝像頭
        If Not (_capture Is Nothing) Then
            If (_captureInProgress) Then
                _capture.Pause()
            Else
                _capture.Start()
            End If
            _captureInProgress = Not _captureInProgress
        End If
    End Sub

   Sub ProcessFrame(sender As System.Object, e As System.EventArgs)
 	Dim bkWhite As New Bgr(Color.White)
        '獲得當前的影象QueryFrame()
        Dim frame As Image(Of Bgr, Byte) = New Image(Of Bgr, Byte)(_capture.Width, _capture.Height)
        _capture.Retrieve(frame)
        frame = frame.Rotate(270, bkWhite, False)

        Dim img As Image(Of Gray, Byte) = New Image(Of Gray, Byte)(frame.Width, frame.Height)
        frame.Convert(Of Gray, Byte).CopyTo(img)
        '控制元件實時顯示攝像頭捕獲的視訊
        ImageBoxEdit.Image = frame
   End Sub

End Class