Logo
Acquisti Giochi Scienza Affari Notizie Societè Casa
Regionale Sport Computer Salute Tempo libero Consultazione  


Calcultate Optical Flow PyrLK 2

' inspired by Opencv lkdemo.c sample

' by Giuseppe Di Santo - Studio IT

 

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Linq

Imports System.Text

Imports System.Windows.Forms

Imports Emgu.CV

Imports Emgu.CV.Structure

 

 

 

Partial Public Class cvCalcOpticalFlowPyrLK2

    Inherits Form

    Private _capture As Capture

    Private ActualFrameNumber As Integer = 0

    Private ActualFrame As Image(Of Bgr, Byte), NextFrame As Image(Of Bgr, Byte), OpticalFlowFrame As Image(Of Bgr, Byte)

    Private ActualGrayFrame As Image(Of Gray, Byte), NextGrayFrame As Image(Of Gray, Byte)

 

    Private ActualFeature As PointF()()

    Private NextFeature As PointF()

    Private Status As Byte()

    Private TrackError As Single()

 

    Private newPoint As PointF

 

 

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        AcquireInputVideo()

        AddHandler Application.Idle, New EventHandler(AddressOf Optical_Flow_Worker)

 

    End Sub

 

    Private Sub Optical_Flow_Worker(ByVal sender As Object, ByVal e As EventArgs)

        ' the actual frame

        _capture.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_POS_FRAMES, ActualFrameNumber)

        ActualFrame = _capture.QueryFrame().Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL)

        ActualGrayFrame = ActualFrame.Convert(Of Gray, [Byte])()

 

        ' the next frame

        NextFrame = _capture.QueryFrame().Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL)

 

 

        NextGrayFrame = NextFrame.Convert(Of Gray, [Byte])()

 

        ' We extract some features to be tracked to compute optical flow

        If ActualFeature Is Nothing Then

            ActualFeature = ActualGrayFrame.GoodFeaturesToTrack(300, 0.01, 10, 5)

            ActualGrayFrame.FindCornerSubPix(ActualFeature, New System.Drawing.Size(10, 10), New System.Drawing.Size(-1, -1), New MCvTermCriteria(20, 0.03))

        Else

            ' Compute optical flow using Lukas Kanade Method

            ComputeSparseOpticalFlow()

            OpticalFlowFrame = New Image(Of Bgr, [Byte])(ActualFrame.Width, ActualFrame.Height)

            OpticalFlowFrame = NextFrame.Copy()

 

            For i As Integer = 0 To ActualFeature(0).Length - 1

                DrawTrackedFeatures(i)

            Next

            ActualFeature(0) = NextFeature

        End If

 

        ActualFrameNumber += 1

 

        ' Frame binding to emguCV imageboxes

        Me.imageBox1.Image = OpticalFlowFrame

        'this.imageBox1.Image = ActualGrayFrame;

 

    End Sub

 

    Private Sub DrawTrackedFeatures(ByVal i As Integer)

        OpticalFlowFrame.Draw(New CircleF(New PointF(ActualFeature(0)(i).X, ActualFeature(0)(i).Y), 1.0F), New Bgr(Color.Red), 2)

    End Sub

 

    Private Sub DrawFlowVectors(ByVal i As Integer)

        Dim p As System.Drawing.Point = New Point()

        Dim q As System.Drawing.Point = New Point()

 

        p.X = CInt(Math.Truncate(ActualFeature(0)(i).X))

        p.Y = CInt(Math.Truncate(ActualFeature(0)(i).Y))

        q.X = CInt(Math.Truncate(NextFeature(i).X))

        q.Y = CInt(Math.Truncate(NextFeature(i).Y))

 

        Dim angle As Double

        angle = Math.Atan2(CDbl(p.Y) - q.Y, CDbl(p.X) - q.X)

 

        Dim line As New LineSegment2D(p, q)

        OpticalFlowFrame.Draw(line, New Bgr(255, 255, 255), 1)

 

        p.X = CInt(Math.Truncate(q.X + 20 * Math.Cos(angle + Math.PI / 4)))

        p.Y = CInt(Math.Truncate(q.Y + 20 * Math.Sin(angle + Math.PI / 4)))

        OpticalFlowFrame.Draw(New LineSegment2D(p, q), New Bgr(255, 255, 255), 1)

        p.X = CInt(Math.Truncate(q.X + 20 * Math.Cos(angle - Math.PI / 4)))

        p.Y = CInt(Math.Truncate(q.Y + 20 * Math.Sin(angle - Math.PI / 4)))

        OpticalFlowFrame.Draw(New LineSegment2D(p, q), New Bgr(255, 255, 255), 1)

    End Sub

 

    Private Sub AcquireInputVideo()

        _capture = New Capture()

        _capture.QueryFrame()

    End Sub

    Private Sub ImageBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ImageBox1.MouseDown

        newPoint = New PointF(e.X, e.Y)

 

        Dim temp As PointF() = New PointF(0) {}

        temp(0) = newPoint

 

        ActualFeature = New PointF(0)() {}

        ActualFeature(0) = temp

 

        ActualGrayFrame.FindCornerSubPix(ActualFeature, New System.Drawing.Size(10, 10), New System.Drawing.Size(-1, -1), New MCvTermCriteria(20, 0.03))

 

    End Sub

    'Private Sub imagebox1_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ImageBox1.Click

    '    newPoint = New PointF(e.X, e.Y)

 

    '    Dim temp As PointF() = New PointF(0) {}

    '    temp(0) = newPoint

 

    '    ActualFeature = New PointF(0)() {}

    '    ActualFeature(0) = temp

 

    '    ActualGrayFrame.FindCornerSubPix(ActualFeature, New System.Drawing.Size(10, 10), New System.Drawing.Size(-1, -1), New MCvTermCriteria(20, 0.03))

    'End Sub

 

    Private Sub ComputeSparseOpticalFlow()

        ' Compute optical flow using pyramidal Lukas Kanade Method               

        OpticalFlow.PyrLK(ActualGrayFrame, NextGrayFrame, ActualFeature(0), New System.Drawing.Size(10, 10), 3, New MCvTermCriteria(20, 0.03), _

         NextFeature, Status, TrackError)

    End Sub

 

 

 

End Class

 

 

 

<< back to index