Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' BEGINS THE CAPTURE OF CAMERA AND THREAD IMAGES Video_NewFrame
CAP = New VideoCaptureDevice(FILTER(ComboBox1.SelectedIndex).MonikerString)
AddHandler CAP.NewFrame, New NewFrameEventHandler(AddressOf Video_NewFrame)
CAP.Start()
ComboBox1.Visible = False
End Sub
Private Sub Video_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)
Dim ORIGINAL As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'IMAGE OF THE CAMERA
Dim FILTERED As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'FILTERED IMAGE
'COLOR FILTERED
Dim FILTRO As New ColorFiltering
FILTRO.Red = New IntRange(MinRed, MaxRed)
FILTRO.Green = New IntRange(MinGreen, MaxGreen)
FILTRO.Blue = New IntRange(MinBlue, Maxblue)
FILTRO.ApplyInPlace(FILTERED)
'PUT FILTERED ON GRAYS SCALE TO BE ABLE TO PROCESS BLOBS
Dim GRIS As Grayscale = Grayscale.CommonAlgorithms.BT709
Dim IMAGENG As Bitmap = GRIS.Apply(FILTERED)
'CLASSIFIES FILTERED (GRAY) IN ZONES (BLOB)
Dim BLOBS As New BlobCounter()
BLOBS.MinHeight = 10 'IT ONLY TAKES THOSE THAT HAVE A DETERMINED SIZE
BLOBS.MinWidth = 10 'IT ONLY TAKES THOSE THAT HAVE A DETERMINED SIZE
BLOBS.ObjectsOrder = ObjectsOrder.Size 'IT WILL TAKE THE MAJOR
BLOBS.ProcessImage(IMAGENG) 'EXECUTE THE CLASSIFIER
Dim RECTANGLES As Rectangle() = BLOBS.GetObjectsRectangles() 'COLLECTION OF BLOBS PASSED TO RECTANGLES
If RECTANGLES.Count > 0 Then ' IF THERE ARE MORE THAN ONE ......
Dim RECTANGLE As Rectangle = RECTANGLES(0) '... TAKE THE FIRST BECAUSE IT IS THE GREATEST
Dim DIBUJO As Graphics = Graphics.FromImage(ORIGINAL) '.... AND ABOUT THE IMAGE OF THE PICTUREBOX ...
Dim RECT As New Pen(Color.Lime, 5)
DIBUJO.DrawRectangle(RECT, RECTANGLE) 'DRAW THE BLOB BOX
DIBUJO.Dispose() 'FREE THE PREVIOUS DRAWING
End If
PictureBoxORIGINAL.Image = ORIGINAL 'PRESENTS THE IMAGES
PictureBoxFILTRADO.Image = FILTERED 'PRESENTS THE IMAGES
End Sub
โค้ดประมาณนี้ คือโค้ดนี้มันจะดึงกล้อง จาก ComboBox มา คือกล้องที่ติดกับเครื่อง แต่ผมจะใช้ไฟล์วีดีโอ ที่อยู่ในเครื่องแล้ว มาใช้ ไม่ทราบว่า ต้อง แก้ตรงไหนครับ
Vb ขอวีธีการดึงไฟล์วีดีโอจากเครื่องมาใช้หน่อยครับ
' BEGINS THE CAPTURE OF CAMERA AND THREAD IMAGES Video_NewFrame
CAP = New VideoCaptureDevice(FILTER(ComboBox1.SelectedIndex).MonikerString)
AddHandler CAP.NewFrame, New NewFrameEventHandler(AddressOf Video_NewFrame)
CAP.Start()
ComboBox1.Visible = False
End Sub
Private Sub Video_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)
Dim ORIGINAL As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'IMAGE OF THE CAMERA
Dim FILTERED As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap) 'FILTERED IMAGE
'COLOR FILTERED
Dim FILTRO As New ColorFiltering
FILTRO.Red = New IntRange(MinRed, MaxRed)
FILTRO.Green = New IntRange(MinGreen, MaxGreen)
FILTRO.Blue = New IntRange(MinBlue, Maxblue)
FILTRO.ApplyInPlace(FILTERED)
'PUT FILTERED ON GRAYS SCALE TO BE ABLE TO PROCESS BLOBS
Dim GRIS As Grayscale = Grayscale.CommonAlgorithms.BT709
Dim IMAGENG As Bitmap = GRIS.Apply(FILTERED)
'CLASSIFIES FILTERED (GRAY) IN ZONES (BLOB)
Dim BLOBS As New BlobCounter()
BLOBS.MinHeight = 10 'IT ONLY TAKES THOSE THAT HAVE A DETERMINED SIZE
BLOBS.MinWidth = 10 'IT ONLY TAKES THOSE THAT HAVE A DETERMINED SIZE
BLOBS.ObjectsOrder = ObjectsOrder.Size 'IT WILL TAKE THE MAJOR
BLOBS.ProcessImage(IMAGENG) 'EXECUTE THE CLASSIFIER
Dim RECTANGLES As Rectangle() = BLOBS.GetObjectsRectangles() 'COLLECTION OF BLOBS PASSED TO RECTANGLES
If RECTANGLES.Count > 0 Then ' IF THERE ARE MORE THAN ONE ......
Dim RECTANGLE As Rectangle = RECTANGLES(0) '... TAKE THE FIRST BECAUSE IT IS THE GREATEST
Dim DIBUJO As Graphics = Graphics.FromImage(ORIGINAL) '.... AND ABOUT THE IMAGE OF THE PICTUREBOX ...
Dim RECT As New Pen(Color.Lime, 5)
DIBUJO.DrawRectangle(RECT, RECTANGLE) 'DRAW THE BLOB BOX
DIBUJO.Dispose() 'FREE THE PREVIOUS DRAWING
End If
PictureBoxORIGINAL.Image = ORIGINAL 'PRESENTS THE IMAGES
PictureBoxFILTRADO.Image = FILTERED 'PRESENTS THE IMAGES
End Sub
โค้ดประมาณนี้ คือโค้ดนี้มันจะดึงกล้อง จาก ComboBox มา คือกล้องที่ติดกับเครื่อง แต่ผมจะใช้ไฟล์วีดีโอ ที่อยู่ในเครื่องแล้ว มาใช้ ไม่ทราบว่า ต้อง แก้ตรงไหนครับ