Source Code Surf
Source Code Surf
Source Code Surf
Imports System.Drawing
Imports System.Diagnostics
Imports
Imports
Imports
Imports
Imports
Imports
Emgu.CV
Emgu.CV.CvEnum
Emgu.CV.Features2D
Emgu.CV.Structure
Emgu.CV.UI
Emgu.CV.Util
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Public Class frmSURF
' member variables '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Dim blnFirstTimeInResizeEvent As Boolean = True
'used to throw out first time in form resize event, see resize event comments fo
r details
Dim intOrigFormWidth As Integer
'vars used to save original
Dim intOrigFormHeight As Integer
'form and image box sizes,
Dim intOrigImageBoxWidth As Integer
'used to resize image box
Dim intOrigImageBoxHeight As Integer
'when form is resized
Dim capWebcam As Capture
'Capture object for webcam
Dim blnWebcamCapturingInProcess As Boolean = False
'variable to keep track of if SURF function has been added to application's list
of tasks
Dim imgSceneColor As Image(Of Bgr, Byte) = Nothing
'original image scene, in color
Dim imgToFindColor As Image(Of Bgr, Byte) = Nothing
'original image to find, in color
Dim imgCopyOfImageToFindWithBorder As Image(Of Bgr, Byte) = Nothing
'use as a copy of image to find, so we can draw a border on this image without a
ltering original image to find
Dim blnImageSceneLoaded As Boolean = False
'flag to track if a scene image has been loaded successfully
Dim blnImageToFindLoaded As Boolean = False
'flag to track if an image to find has been loaded successfully
'resulting image of image scene and image to find concatenated together,
Dim imgResult As Image(Of Bgr, Byte) = Nothing
'with border drawn around found image, and key points and matching lines also dr
awn if chosen
Dim bgrKeyPointsColor As Bgr = New Bgr(Color.Blue)
'color to draw key points on result image
Dim bgrMatchingLinesColor As Bgr = New Bgr(Color.Green)
dle
blnWebcamCapturingInProcess = False
'update flag variable
End If
imgSceneColor = Nothing
'reset class level variables
imgToFindColor = Nothing
'
imgCopyOfImageToFindWithBorder = Nothing
'
imgResult = Nothing
'
blnImageSceneLoaded = False
'
blnImageToFindLoaded = False
'
txtImageScene.Text = ""
'reset form
txtImageToFind.Text = ""
'
ibResult.Image = Nothing
'
Me.Text = "Instructions: use ""..."" buttons to choose both imag
e files, then press Perform SURF button"
'update
title bar text
btnPerformSURFOrGetImageToTrack.Text = "Perform SURF Detection"
'update button text
ibResult.Image = Nothing
'make image box blank
lblImageScene.Visible = True
'show controls that are used for still images
lblImageToFind.Visible = True
'
txtImageScene.Visible = True
'
txtImageToFind.Visible = True
'
btnImageScene.Visible = True
'
btnImageToFind.Visible = True
'
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Private Sub rdoWebcam_CheckedChanged( sender As System.Object, e As System.Even
tArgs) Handles rdoWebcam.CheckedChanged
If(rdoWebcam.Checked = True) Then
'if webcam was just chosen
imgSceneColor = Nothing
'reset class level variables
imgToFindColor = Nothing
'
imgCopyOfImageToFindWithBorder = Nothing
'
imgResult = Nothing
'
blnImageSceneLoaded = False
'
blnImageToFindLoaded = False
'
txtImageScene.Text = ""
'reset form
txtImageToFind.Text = ""
'
ibResult.Image = Nothing
'
Try
'try
capWebcam = New Capture()
'to instantiate Capture object for webcam
Catch ex As Exception
'if not successful
Me.Text = ex.Message
'show error message on title bar
Return
'and bail
End Try
Me.Text = "Instructions: hold image to track up to camera, then
press ""get image to track"" "
'update title ba
r text
btnPerformSURFOrGetImageToTrack.Text = "get image to track"
'update button text
imgToFindColor = Nothing
AddHandler Application.Idle, New EventHandler(AddressOf Me.Perfo
rmSURFDetectionAndUpdateGUI)
'add SURF function to applicatio
n's list of tasks
blnWebcamCapturingInProcess = True
'update flag variable
lblImageScene.Visible = False
'hide controls that are not used with webcam
lblImageToFind.Visible = False
'
txtImageScene.Visible = False
'
txtImageToFind.Visible = False
'
btnImageScene.Visible = False
'
btnImageToFind.Visible = False
'
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Private Sub btnImageScene_Click( sender As System.Object, e As System.EventArgs
) Handles btnImageScene.Click
Dim dialogResult As DialogResult = ofdImageScene.ShowDialog()
'bring up image scene file dialog box
Else
'else
Me.Text = "choose image files first, then choose Perform
SURF Detection button"
'remind user to choose b
oth image files before choosing Perform SURF
End If
ElseIf(rdoWebcam.Checked = True) Then
'if using the webcam
imgToFindColor = imgSceneColor.Resize(320, 240, INTER.CV_INTER_C
UBIC, True)
'use most recent image from webcam, which will be in img
SceneColor, then shrink and save as new image to track
Me.Text = "Instructions: to update image to track, hold image up
to camera, then press ""update image to track"""
'update
title bar text
btnPerformSURFOrGetImageToTrack.Text = "update image to track"
'update button text
Else
'should never get here
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Private Sub ckDrawKeyPoints_CheckedChanged( sender As System.Object, e As Syste
m.EventArgs) Handles ckDrawKeyPoints.CheckedChanged
If(ckDrawKeyPoints.Checked = False) Then
'if draw key points was just unchecked
ckDrawMatchingLines.Checked = False
'uncheck draw matching lines check box
ckDrawMatchingLines.Enabled = False
'and disable draw matching lines check box
ElseIf(ckDrawKeyPoints.Checked = True) then
'else if draw key points was just checked
ckDrawMatchingLines.Enabled = True
're-enable draw matching lines check box
End If
If(rdoImageFile.Checked = True) Then
'if using image from file
btnPerformSURFOrGetImageToTrack_Click(New Object(), New EventArg
s())
'call SURF button click event to update image for draw k
ey points check box change
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Private Sub ckDrawMatchingLines_CheckedChanged( sender As System.Object, e As S
ystem.EventArgs) Handles ckDrawMatchingLines.CheckedChanged
If(rdoImageFile.Checked = True) Then
'if using image from file
btnPerformSURFOrGetImageToTrack_Click(New Object(), New EventArg
s())
'call SURF button click event to update
image for draw matching lines check box change
End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''
Sub PerformSURFDetectionAndUpdateGUI(sender As Object, arg As EventArgs)
End If
imgCopyOfImageToFindWithBorder = imgToFindColor.Copy()
'make a copy of the image to find, so we can draw on the copy, therefore no chan
ging the original image to find
'draw a 2 pixel wide border around the copy of the image to find, use same color
as box for found image
imgCopyOfImageToFindWithBorder.Draw(New Rectangle(1, 1, imgCopyOfImageTo
FindWithBorder.Width - 3, imgCopyOfImageToFindWithBorder.Height - 3), bgrFoundIm
ageColor, 2)
'next we will draw the scene image and image to find together in the result imag
e
'3 conditionals are necessary, depenting on which check boxes are checked (draw
key points and / or draw matching lines)
If(ckDrawKeyPoints.Checked = True And ckDrawMatchingLines.Checked = True
) Then
'if drawing both key points & ma
tching lines on result image,
'use DrawMatches function, combines scene image and copy of image to find into r
esult, then draws key points and matching lines all in one step
imgResult = Features2DToolbox.DrawMatches(imgCopyOfImageToFindWi
thBorder, _
vkpToFindKeyPoints, _
imgSceneColor, _
vkpSceneKeyPoints, _
mtxMatchIndices, _
bgrMatchingLinesColor, _
bgrKeyPointsColor, _
mtxMask, _
Features2DToolbox.KeypointDrawType.DEFAULT)
ElseIf(ckDrawKeyPoints.Checked = True And ckDrawMatchingLines.Checked =
False) Then
'if drawing key points but not matching
lines on result image,
'draw scene with key points on result image,
imgResult = Features2DToolbox.DrawKeypoints(imgSceneColor, vkpSc
eneKeyPoints, bgrKeyPointsColor, Features2DToolbox.KeypointDrawType.DEFAULT)
'then draw key points on copy of image to find,
imgCopyOfImageToFindWithBorder = Features2DToolbox.DrawKeypoints
(imgCopyOfImageToFindWithBorder, vkpToFindKeyPoints, bgrKeyPointsColor, Features
2DToolbox.KeypointDrawType.DEFAULT)
imgResult = imgResult.ConcateHorizontal(imgCopyOfImageToFindWith
Border)
'then concatenate copy of image to find
onto result image
ElseIf(ckDrawKeyPoints.Checked = False And ckDrawMatchingLines.Checked =
False) Then
'if not drawing key points or matching lines,
imgResult = imgSceneColor