0

Edit: Motion Detection I believe this may answer my question...

I want to not only detect motion, but read where the motion occurred in the frame. Nothing in MotionDetector seems to have anything about its location. I thought maybe I could run it through MotionDetector then extract the biggest blob, but that doesn't seem to work either. It only picks up one blob.

Video clip is a vehicle coming down a ramp. mainPBX is the original frame, main2PBX is the altered frame (with the biggest blob overlayed). My thought is to read when the blob goes from smaller to bigger (and vice versa), to determine entering or leaving.

Private Sub Processor()
        Dim bc As New BlobCounter With {
            .MinWidth = 5,
            .MinHeight = 5,
            .ObjectsOrder = ObjectsOrder.Size
        }

        Dim detector As New MotionDetector(New SimpleBackgroundModelingDetector, New MotionAreaHighlighting)

        Using reader As New AForge.Video.FFMPEG.VideoFileReader()
            reader.Open("C:\Videos\MyVideo.mp4")
            For i As Integer = 0 To reader.FrameCount - 1
                Dim frame = reader.ReadVideoFrame()
                Dim frame2 As Bitmap = frame.Clone()
                Dim frame3 As Bitmap = frame.Clone()
                detector.ProcessFrame(frame2)
                bc.ProcessImage(frame2)
                Dim blobs = bc.GetObjectsInformation()

                If blobs.Length > 0 Then
                    bc.ExtractBlobsImage(frame3, blobs(0), True)
                    PBX_Image(main2PBX, frame3.Clone())
                End If

                PBX_Image(mainPBX, frame.Clone())

                Threading.Thread.Sleep(25)

                frame.Dispose()
                frame2.Dispose()
                frame3.Dispose()
            Next
        End Using
    End Sub
Tyler Montney
  • 1,402
  • 1
  • 17
  • 25

1 Answers1

0

This is in no way finished, but I can actually interact with the blobs.

Private Sub Processor()
    Dim Rectangles As New List(Of Rectangle)

    Dim detector As New SimpleBackgroundModelingDetector With {
        .SuppressNoise = True,
        .DifferenceThreshold = 10,
        .FramesPerBackgroundUpdate = 10,
        .KeepObjectsEdges = True
    }

    Dim processor As New AForge.Vision.Motion.BlobCountingObjectsProcessing With {
        .MinObjectsWidth = 40,
        .MinObjectsHeight = 40,
        .HighlightColor = Color.Red
    }

    Dim motionDetector As New MotionDetector(detector, processor)

    Using reader As New AForge.Video.FFMPEG.VideoFileReader()
        reader.Open("C:\Videos\MyVideo.mp4")
        For i As Integer = 0 To reader.FrameCount - 1
            Dim frame = reader.ReadVideoFrame()
            motionDetector.ProcessFrame(frame)
            Dim t As BlobCountingObjectsProcessing = motionDetector.MotionProcessingAlgorithm
            Dim r As Rectangle = Rectangle.Empty
            If t.ObjectRectangles.Length > 0 Then
                If t.ObjectRectangles.Length > 2 Then
                    r = t.ObjectRectangles.Aggregate(Function(r1, r2) If((r1.Width * r1.Height) > (r2.Width * r2.Height), r1, r2))
                Else
                    r = t.ObjectRectangles.First()
                End If
            End If

            If r.IsEmpty = False Then Rectangles.Add(r)

            PBX_Image(mainPBX, frame.Clone())

            Threading.Thread.Sleep(25)

            frame.Dispose()
        Next
    End Using
End Sub
Tyler Montney
  • 1,402
  • 1
  • 17
  • 25