Click here to Skip to main content
15,888,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my vB2010 code for Surf detection
the problem is when ever i select the webcam radio check box , the form disappears...
i have done the try catch block even though it is not showing anything..
my wc is logitech c170
(with installed drivers ) which i used in my prev projects i worked well for face detection in the visualC++
i am new to vb and i couldnot just figure out what the problem is...
the image mapping is functioning correctly...but w/c is not doing what it has to do..
most probably i think the in this class,

>>>>Sub PerformSURFDetectionAndUpadateGUI(ByVal sender As Object, ByVal arg As EventArgs)


which is at the end of the program....



Try

imgSceneColor = capWebcam.QueryFrame()
Catch ex As Exception
Me.Text = ex.Message
Return
End Try
If (imgSceneColor Is Nothing) Then
Me.Text = "error, img scene color is nothing"
Return
i have put ''over here nearthe 2 try blocks

i tried doign without the trycatch block
the complier is returning the "there in no imgsecnecolor"
imgscenecaptre is the one which i used for getting the code w/c or orig image into the buffer for perfroming the comparison..
all the boolen flags are working perfectly
and for the image it is working perfectly..

' enter code here'

Option Strict On
Imports System.Drawing
Imports System.Diagnostics
Imports System
Imports System.Runtime
Imports System.Runtime.InteropServices
Imports Emgu.CV
Imports Emgu.CV.CvEnum
Imports Emgu.CV.Features2D
Imports Emgu.CV.Structure
Imports Emgu.CV.UI
Imports Emgu.CV.Util

Public Class Form1
' member variables
Dim blnFirstTimeInResizeEvent As Boolean = True 'used to throw out the first time in fom resize event
Dim intOrigFormWidth As Integer ' var used tosave orig img width
Dim intOrigFormHeight As Integer ' height
Dim intOrigImageBoxWidth As Integer 'used to resize img box
Dim intOrigInageBoxHeight As Integer 'when the form is resized


Dim capWebcam As Capture 'capture obj for wc
Dim blnWebcamCapturingInProcess As Boolean = False 'variable to keep track if SUB functionhas been adee to app list

Dim imgSceneColor As Image(Of Bgr, Byte) = Nothing

Dim imgtofindcolor As Image(Of Bgr, Byte) = Nothing
Dim imgCopyOfImageToFindimWidthBorder As Image(Of Bgr, Byte) = Nothing ' use as a copy of image to find,so we can draw a border on this image


Dim blnImageSceneLoaded As Boolean = False

Dim blnIageToFIndLoaded As Boolean = False
Dim imgResult As Image(Of Bgr, Byte) = Nothing


Dim bgrKeyPointolor As Bgr = New Bgr(Color.Blue)
Dim bgrMatchingLineColor As Bgr = New Bgr(Color.Green)
Dim bgrFoundImageColo As Bgr = New Bgr(Color.Red)

Dim stopwatch As Stopwatch = New Stopwatch()

Sub New()

' This call is required by the designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
intOrigFormHeight = Me.Height
intOrigFormWidth = Me.Width

intOrigImageBoxWidth = ibResult.Width
intOrigInageBoxHeight = ibResult.Height


End Sub



Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPerformSurfOnGteImageToTrack.Click
If (rdoImageFile.Checked = True) Then ' if using still images

If (txtImageToFind.Text <> String.Empty And txtImageScene.Text <> String.Empty) Then
PerformSURFDetectionAndUpadateGUI(New Object(), New EventArgs())
Else
Me.Text = "choose the files first and peroform surf detection"

End If
ElseIf (rdoWebCam.Checked = True) Then
imgtofindcolor = imgSceneColor.Resize(320, 240, INTER.CV_INTER_CUBIC, True)
Me.Text = "to update image to track ,hold iamge to camera nd press upadte to track"
btnPerformSurfOnGteImageToTrack.Text = "update image to track"

End If
End Sub



Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If (blnFirstTimeInResizeEvent = True) Then
blnFirstTimeInResizeEvent = False
Else
ibResult.Width = Me.Width - (intOrigFormWidth - intOrigImageBoxWidth) ' resize image box
ibResult.Height = Me.Height - (intOrigInageBoxHeight - intOrigInageBoxHeight) 'resize imagebox to form

End If
End Sub

Private Sub rdoWebCam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoWebCam.CheckedChanged
If (rdoWebCam.Checked = True) Then

imgSceneColor = Nothing 'reset class level variables
imgtofindcolor = Nothing
imgCopyOfImageToFindimWidthBorder = Nothing
imgResult = Nothing
blnImageSceneLoaded = False
blnIageToFIndLoaded = False

txtImageScene.Text = ""
txtImageToFind.Text = ""
ibResult.Image = Nothing


Try

capWebcam = New Capture()
Catch ex As Exception
Me.Text = ex.Message
Return

End Try


Me.Text = "instruction: hold image to track upto camera,then press ""get image to track"
btnPerformSurfOnGteImageToTrack.Text = "get image to track"
imgtofindcolor = Nothing
AddHandler Application.Idle, New EventHandler(AddressOf Me.PerformSURFDetectionAndUpadateGUI)
blnWebcamCapturingInProcess = True

lblImageScene.Visible = False 'show controls
lblImageToFind.Visible = False ' True
txtImageScene.Visible = False 'True
txtImageToFind.Visible = False 'True
txtImageScene.Visible = False 'True
btnImageToFind.Visible = False ' True

End If

End Sub

Private Sub btnImageScene_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImageScene.Click
Dim dialogResult As DialogResult = ofdIageScene.ShowDialog()
If (dialogResult = Windows.Forms.DialogResult.OK Or dialogResult = Windows.Forms.DialogResult.Yes) Then
txtImageScene.Text = ofdIageScene.FileName
Else
Return
End If
Try
imgSceneColor = New Image(Of Bgr, Byte)(txtImageScene.Text) 'try to load scene image

Catch ex As Exception
Me.Text = ex.Message
End Try
blnImageSceneLoaded = True 'scene image was loaded sucessfuly
If (blnIageToFIndLoaded = False) Then
ibResult.Image = imgSceneColor 'shoe the iamge to screen
Else
ibResult.Image = imgSceneColor.ConcateHorizontal(imgCopyOfImageToFindimWidthBorder) 'concatenate image to ind width and border

End If
End Sub

Private Sub btnImageToFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImageToFind.Click
Dim dialogResult As DialogResult = ofdImageToFind.ShowDialog() 'bring image to find
If (dialogResult = Windows.Forms.DialogResult.OK Or dialogResult = Windows.Forms.DialogResult.Yes) Then
txtImageToFind.Text = ofdIageScene.FileName
Else
Return

End If
Try
imgtofindcolor = New Image(Of Bgr, Byte)(txtImageToFind.Text)
Catch ex As Exception
Me.Text = ex.Message
Return
End Try
blnIageToFIndLoaded = True
imgCopyOfImageToFindimWidthBorder = imgtofindcolor.Copy()
imgCopyOfImageToFindimWidthBorder.Draw(New Rectangle(1, 1, imgCopyOfImageToFindimWidthBorder.Width - 3, imgCopyOfImageToFindimWidthBorder.Height - 3), bgrFoundImageColo, 2)
If (blnImageSceneLoaded = True) Then
ibResult.Image = imgSceneColor.ConcateHorizontal(imgCopyOfImageToFindimWidthBorder)
Else
ibResult.Image = imgCopyOfImageToFindimWidthBorder

End If
End Sub

Private Sub txtImageScene_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtImageScene.TextChanged
txtImageScene.SelectionStart = txtImageToFind.Text.Length
End Sub




Private Sub ckDrawKeyPoints_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckDrawKeyPoints.CheckedChanged

If (ckDrawKeyPoints.Checked = False) Then
ckDrawMatching.Checked = False
ckDrawMatching.Enabled = False
ElseIf (ckDrawKeyPoints.Checked = True) Then
ckDrawMatching.Enabled = True
End If
If (rdoImageFile.Checked = True) Then
Button3_Click(New Object(), New EventArgs())


End If
End Sub

Private Sub ckDrawMatching_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ckDrawMatching.CheckedChanged
If (rdoImageFile.Checked = True) Then
Button3_Click(New Object(), New EventArgs()) ''button 3 is the surf button i didnot change while changing the variable naems so it retained the button3_click
End If
End Sub

Sub PerformSURFDetectionAndUpadateGUI(ByVal sender As Object, ByVal arg As EventArgs)
If (rdoImageFile.Checked = True) Then
If (blnImageSceneLoaded = False Or blnIageToFIndLoaded = False Or imgSceneColor Is Nothing Or imgtofindcolor Is Nothing) Then
Me.Text = "either or both the images are not loaded,plz choose the images brfore performing surf"
Return

End If
Me.Text = "preocessing plzz wt.............."
Application.DoEvents()
stopwatch.Restart()
'''''''''over here'''''''''''''
Else If (rdoWebCam.Checked = True) Then
Try

imgSceneColor = capWebcam.QueryFrame()
Catch ex As Exception
Me.Text = ex.Message
Return
End Try
If (imgSceneColor Is Nothing) Then
Me.Text = "error, img scene color is nothing"
Return
End If
If (imgtofindcolor Is Nothing) Then
ibResult.Image = imgSceneColor
Return
End If
End If

'prfrm surf on both imgs
Dim surfDetector As SURFDetector = New SURFDetector(500, False) 'initialsise SURF obj,params are threshold and extended flag
Dim imgSceneGray As Image(Of Gray, Byte) = Nothing
Dim imgToFindGray As Image(Of Gray, Byte) = Nothing

Dim vkpSceneKeyPoints As VectorOfKeyPoint 'vectro of keypoints in scene image
Dim vkpToFindKeyPoints As VectorOfKeyPoint 'vector of kp in to find image
Dim mtxSceneDescriptors As Matrix(Of Single) 'matrix of descriptors
Dim mtxToFindDescriptors As Matrix(Of Single) ' matrix of descrip[tors in to find imagh
Dim mtxMatchIndices As Matrix(Of Integer) ' matrix of descriptor,will result from training matrices
Dim mtxDistance As Matrix(Of Single) ''matrix of distance value result from training data
Dim mtxMask As Matrix(Of Byte) 'both input and output fumvtion vote for uniqueness()
Dim bruteForceMatcher As BruteForceMatcher(Of Single) 'for each descriptor i the first set the amtcher finds tha match in the second set
Dim homographyMatrix As HomographyMatrix = Nothing 'used for projection of points

Dim intKNumNearestNeigh As Integer = 2
Dim dblUniquenessthreshold As Double = 0.8 'the distance differnece ratio for match
Dim intNumNonZeroElements As Integer 'used a return value for non zero elements

Dim dblScaleIncrement As Double = 1.5
Dim intRotationBins As Integer = 20


Dim dblRansacprojthresh As Double = 2.0 'to get the homography features
Dim rectImageToFind As Rectangle '4 points draqing box around thee found image in the scene
Dim ptfPointsF As PointF() '4 points definig a box aaround a cirlce
Dim ptPoints As Point() '' '''''''''''' ' '


imgSceneGray = imgSceneColor.Convert(Of Gray, Byte)() 'convert scene color iamge to gray scale
imgToFindGray = imgtofindcolor.Convert(Of Gray, Byte)() 'convert to find colorimage in gray scale


vkpSceneKeyPoints = surfDetector.DetectKeyPointsRaw(imgSceneGray, Nothing) 'detect the keypoints in the scene
mtxSceneDescriptors = surfDetector.ComputeDescriptorsRaw(imgSceneGray, Nothing, vkpSceneKeyPoints) 'compute sedne descriptor in the scene

vkpToFindKeyPoints = surfDetector.DetectKeyPointsRaw(imgToFindGray, Nothing)
mtxToFindDescriptors = surfDetector.ComputeDescriptorsRaw(imgToFindGray, Nothing, vkpToFindKeyPoints)

bruteForceMatcher = New BruteForceMatcher(Of Single)(DistanceType.L2)
bruteForceMatcher.Add(mtxToFindDescriptors) 'add matrix to find brute force maycher
mtxMatchIndices = New Matrix(Of Integer)(mtxSceneDescriptors.Rows, intKNumNearestNeigh)
mtxDistance = New Matrix(Of Single)(mtxSceneDescriptors.Rows, intKNumNearestNeigh)


bruteForceMatcher.KnnMatch(mtxSceneDescriptors, mtxMatchIndices, mtxDistance, intKNumNearestNeigh, Nothing)


mtxMask = New Matrix(Of Byte)(mtxDistance.Rows, 1) ' mask matrix
mtxMask.SetValue(255)


Features2DToolbox.VoteForUniqueness(mtxDistance, dblUniquenessthreshold, mtxMask) 'filter the matched feartures

intNumNonZeroElements = CvInvoke.cvCountNonZero(mtxMask)
If (intNumNonZeroElements >= 4) Then ' get the homography using the ransac threshold
intNumNonZeroElements = Features2DToolbox.VoteForSizeAndOrientation(vkpToFindKeyPoints, vkpSceneKeyPoints, mtxMatchIndices, mtxMask, dblScaleIncrement, intRotationBins)
If (intNumNonZeroElements >= 4) Then
homographyMatrix = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(vkpToFindKeyPoints, vkpSceneKeyPoints, mtxMatchIndices, mtxMask, dblRansacprojthresh)

End If
End If
imgCopyOfImageToFindimWidthBorder = imgtofindcolor.Copy()
imgCopyOfImageToFindimWidthBorder.Draw(New Rectangle(1, 1, imgCopyOfImageToFindimWidthBorder.Width - 3, imgCopyOfImageToFindimWidthBorder.Height - 3), bgrFoundImageColo, 2)



If (ckDrawKeyPoints.Checked = True And ckDrawMatching.Checked = True) Then

imgResult = Features2DToolbox.DrawMatches(imgCopyOfImageToFindimWidthBorder, vkpToFindKeyPoints, imgSceneColor, vkpSceneKeyPoints, mtxMatchIndices, bgrMatchingLineColor, bgrKeyPointolor, mtxMask, Features2DToolbox.KeypointDrawType.DEFAULT)

'to draw both the line and points




ElseIf (ckDrawKeyPoints.Checked = True And ckDrawMatching.Checked = False) Then
'draw scene with key points on the image

imgResult = Features2DToolbox.DrawKeypoints(imgSceneColor, vkpSceneKeyPoints, bgrKeyPointolor, Features2DToolbox.KeypointDrawType.DEFAULT)
'thne draw keypoints on the image to find

imgCopyOfImageToFindimWidthBorder = Features2DToolbox.DrawKeypoints(imgCopyOfImageToFindimWidthBorder, vkpToFindKeyPoints, bgrKeyPointolor, Features2DToolbox.KeypointDrawType.DEFAULT)
imgResult = imgResult.ConcateHorizontal(imgCopyOfImageToFindimWidthBorder)


ElseIf (ckDrawKeyPoints.Checked = False And ckDrawMatching.Checked = True) Then
imgResult = imgSceneColor
imgResult = imgResult.ConcateHorizontal(imgCopyOfImageToFindimWidthBorder)
Else
'should never get here

End If

''draw border around the iamge in the scene color
If (homographyMatrix IsNot Nothing) Then 'chedck to makesure the homography matrix is not null
rectImageToFind.x = 0
rectImageToFind.Y = 0

rectImageToFind.Width = imgToFindGray.Width
rectImageToFind.Height = imgToFindGray.Height

'int of array of pointsF's corresponding to the rectangle
ptfPointsF = New PointF() {New PointF(rectImageToFind.Left, rectImageToFind.Top), New PointF(rectImageToFind.Right, rectImageToFind.Top), New PointF(rectImageToFind.Right, rectImageToFind.Bottom), New PointF(rectImageToFind.Left, rectImageToFind.Bottom)}
homographyMatrix.ProjectPoints(ptfPointsF) 'location of the obox in the scene image

'convet from pointf[] to point,because project points() takes pointsf[] but drawpoly[] takes point[]
ptPoints = New Point() {Point.Round(ptfPointsF(0)), Point.Round(ptfPointsF(1)), Point.Round(ptfPointsF(2)), Point.Round(ptfPointsF(3))}



imgResult.DrawPolyline(ptPoints, True, bgrFoundImageColo, 2) 'draw border around found mage portion of result image


End If

ibResult.Image = imgResult
If (rdoImageFile.Checked = True) Then
stopwatch.Stop()
Me.Text = "Processing time" + stopwatch.Elapsed.TotalSeconds.ToString() + "sec ,done processing ,choose another image if needed"
End If




End Sub

Private Sub rdoImageFile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoImageFile.CheckedChanged
If (rdoImageFile.Checked = True) Then
If (blnWebcamCapturingInProcess = True) Then
RemoveHandler Application.Idle, New EventHandler(AddressOf Me.PerformSURFDetectionAndUpadateGUI)
blnWebcamCapturingInProcess = False
End If
imgSceneColor = Nothing
imgtofindcolor = Nothing
imgCopyOfImageToFindimWidthBorder = Nothing
imgResult = Nothing
blnImageSceneLoaded = False
blnIageToFIndLoaded = False


txtImageScene.Text = """"
txtImageToFind.Text = ""
ibResult.Image = Nothing


Me.Text = "instructions : use browse to select the files and press surf detect" 'update title bar text
btnPerformSurfOnGteImageToTrack.Text = "perform SURF detection" 'update button text
ibResult.Image = Nothing

lblImageScene.Visible = True 'show controls
lblImageToFind.Visible = True
txtImageScene.Visible = True
txtImageToFind.Visible = True
txtImageScene.Visible = True
btnImageToFind.Visible = True


End If
End Sub

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

End Sub
End Class
Posted
Comments
[no name] 24-May-13 9:25am    
Do you really think that people are going to go through all of this unformatted, unreadable text to try and figure out what your problem could possibly be?

1 solution

Try

capWebcam = New Capture()
Catch ex As Exception
Me.Text = ex.Message
Return

End Try


try this----------------------->

Try

capWebcam = New Capture(0)
Catch ex As Exception
Me.Text = ex.Message
Return

End Try

---------------------------------------------
put device index 0,1,2,3 depends upon number of webcam attach
for one put
capWebcam = New Capture(0)

for two webcams, initialized one more variable....
capWebcam_1 = New Capture(0)
capWebcam_2 = New Capture(1)

hoope works..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900