网络摄像头无法启动OPENCV,EMGUCV [英] webcam not initilising OPENCV,EMGUCV

查看:239
本文介绍了网络摄像头无法启动OPENCV,EMGUCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的vB2010代码用于Surf检测
问题是,每当我选中网络摄像头收音机复选框时,表格就会消失...
即使没有显示任何内容,我也做了try catch块..
我的wc是罗技c170
(在安装了驱动程序的情况下)在上一个项目中使用过,因此在visualC ++中可以很好地用于人脸检测
我是vb的新手,我不能仅仅弄清楚问题出在哪里...
图像映射正常运行...但是w/c没有执行它必须执行的操作..
最有可能我认为在这堂课中,

>>>" Sub PerformSURFDetectionAndUpadateGUI(ByVal sender As Object,ByVal arg EventEvents)


在程序的末尾....



试试

imgSceneColor = capWebcam.QueryFrame()
异常捕获
Me.Text = ex.Message
返回
结束尝试
如果(imgSceneColor什么都没有)那么
Me.Text =错误,img场景颜色为空"
返回
我将''''放在2个try块附近

我在没有trycatch块的情况下尝试了统治-
编译器返回没有imgsecnecolor"
imgscenecaptre是我用来将w/c或orig图像代码放入缓冲区以进行比较的代码..
所有的boolen标志均正常运行
并且对于图像来说,它工作得很好..

''在此处输入代码''

严格启用选项
导入System.Drawing
导入System.Diagnostics
导入系统
导入System.Runtime
导入System.Runtime.InteropServices
导入Emgu.CV
导入Emgu.CV.CvEnum
导入Emgu.CV.Features2D
导入Emgu.CV.Structure
导入Emgu.CV.UI
导入Emgu.CV.Util

公共课程表格1
''成员变量
Dim blnFirstTimeInResizeEvent As Boolean = True''用于在fom调整大小事件中第一次抛出
Dim intOrigFormWidth As Integer''var用于保存orig img宽度
Dim intOrigFormHeight As Integer''height
Dim intOrigImageBoxWidth As Integer''用于调整img框的大小
调整表单大小时,将intOrigInageBoxHeight设置为Intger



Dim capWebcam As Capture''为wc捕获obj
Dim blnWebcamCapturingInProcess As Boolean = False''变量以跟踪是否已将SUB函数添加到应用列表中

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

Dim imgtofindcolor As Image(Of Bgr,Byte)= Nothing
Dim imgCopyOfImageToFindimWidthBorder As Image(Of Bgr,Byte)= Nothing''用作要查找的图像副本,因此我们可以在此图像上绘制边框


昏暗的blnImageSceneLoaded为Boolean = False

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


昏暗的bgrKeyPointolor作为Bgr =新的Bgr(颜色.蓝色)
昏暗的bgrMatchingLineColor为Bgr =新的Bgr(颜色.绿色)
暗bgrFoundImageColo作为Bgr =新Bgr(颜色.红色)

昏暗的秒表作为秒表=新的秒表()

子New()

''设计人员需要此调用.
InitializeComponent()

''在InitializeComponent()调用之后添加任何初始化. intOrigFormHeight = Me.Height
intOrigFormWidth = Me.Width

intOrigImageBoxWidth = ibResult.Width
intOrigInageBoxHeight = ibResult.Height


结束子



Private Sub Button3_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btnPerformSurfOnGteImageToTrack.Click
如果(rdoImageFile.Checked = True),则为",如果使用静止图像

如果(txtImageToFind.Text<> String.Empty和txtImageScene.Text<> String.Empty)然后
PerformSURFDetectionAndUpadateGUI(New Object(),New EventArgs())
其他
Me.Text =首先选择文件并进行peroform冲浪检测"

如果结束
ElseIf(rdoWebCam.Checked = True)然后
imgtofindcolor = imgSceneColor.Resize(320,240,INTER.CV_INTER_CUBIC,True)
Me.Text =更新图像以进行跟踪,将图像保持在相机上,然后按upadte进行跟踪"
btnPerformSurfOnGteImageToTrack.Text =更新图像以进行跟踪"

如果结束
结束子



私有子Form1_Resize(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理MyBase.Resize
如果(blnFirstTimeInResizeEvent = True),则
blnFirstTimeInResizeEvent = False
其他
ibResult.Width = Me.Width-(intOrigFormWidth-intOrigImageBoxWidth)''调整图像框的大小
ibResult.Height = Me.Height-(intOrigInageBoxHeight-intOrigInageBoxHeight)''调整图像框大小以形成

如果结束
结束子

私有Sub rdoWebCam_CheckedChanged(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理rdoWebCam.CheckedChanged
如果(rdoWebCam.Checked = True)然后

imgSceneColor =什么也没有'重置类级别变量
imgtofindcolor =没什么
imgCopyOfImageToFindimWidthBorder =没什么
imgResult =什么都没有
blnImageSceneLoaded = False
blnIageToFIndLoaded = False

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


试试

capWebcam = New Capture()
异常捕获
Me.Text = ex.Message
返回

结束尝试


Me.Text =说明:按住图像以跟踪到摄像机,然后按"获取图像以进行跟踪"
btnPerformSurfOnGteImageToTrack.Text =获取图像进行跟踪"
imgtofindcolor =没什么
AddHandler Application.Idle,新建EventHandler(AddressOf Me.PerformSURFDetectionAndUpadateGUI)
blnWebcamCapturingInProcess = True

lblImageScene.Visible = False''显示控件
lblImageToFind.Visible = False''正确
txtImageScene.Visible = False''真
txtImageToFind.Visible = False''真
txtImageScene.Visible = False''真
btnImageToFind.Visible = False''正确

如果结束

结束子

私有Sub btnImageScene_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btnImageScene.Click
Dim dialogResult作为DialogResult = ofdIageScene.ShowDialog()
如果(dialogResult = Windows.Forms.DialogResult.OK或dialogResult = Windows.Forms.DialogResult.Yes),则
txtImageScene.Text = ofdIageScene.FileName
其他
返回
如果结束
试试
imgSceneColor = New Image(Of Bgr,Byte)(txtImageScene.Text)''尝试加载场景图像

异常捕获
Me.Text = ex.Message
结束尝试
blnImageSceneLoaded = True''场景图像已成功加载
如果(blnIageToFIndLoaded = False)那么
ibResult.Image = imgSceneColor''将图像插入屏幕
其他
ibResult.Image = imgSceneColor.ConcateHorizo​​ntal(imgCopyOfImageToFindimWidthBorder)''将图像连接到ind宽度和边框

如果结束
结束子

私有Sub btnImageToFind_Click(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理btnImageToFind.Click
暗调dialogResult作为DialogResult = ofdImageToFind.ShowDialog()''带图片查找
如果(dialogResult = Windows.Forms.DialogResult.OK或dialogResult = Windows.Forms.DialogResult.Yes),则
txtImageToFind.Text = ofdIageScene.FileName
其他
返回

如果结束
试试
imgtofindcolor =新图片(Bgr,字节)(txtImageToFind.Text)
异常捕获
Me.Text = ex.Message
返回
结束尝试
blnIageToFIndLoaded = True
imgCopyOfImageToFindimWidthBorder = imgtofindcolor.Copy()
imgCopyOfImageToFindimWidthBorder.Draw(新矩形(1,1,imgCopyOfImageToFindimWidthBorder.Width-3,imgCopyOfImageToFindimWidthBorder.Height-3),bgrFoundImageColo,2)
如果(blnImageSceneLoaded = True)然后
ibResult.Image = imgSceneColor.ConcateHorizo​​ntal(imgCopyOfImageToFindimWidthBorder)
其他
ibResult.Image = imgCopyOfImageToFindimWidthBorder

如果结束
结束子

私有Sub txtImageScene_TextChanged(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理txtImageScene.TextChanged
txtImageScene.SelectionStart = txtImageToFind.Text.Length
结束子




私有子对象ckDrawKeyPoints_CheckedChanged(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理ckDrawKeyPoints.CheckedChanged

如果(ckDrawKeyPoints.Checked = False)然后
ckDrawMatching.Checked = False
ckDrawMatching.Enabled =假
ElseIf(ckDrawKeyPoints.Checked = True)然后
ckDrawMatching.Enabled = True
如果结束
如果(rdoImageFile.Checked = True)然后
Button3_Click(New Object(),New EventArgs())


如果结束
结束子

私有子对象ckDrawMatching_CheckedChanged(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理ckDrawMatching.CheckedChanged
如果(rdoImageFile.Checked = True)然后
Button3_Click(New Object(),New EventArgs())''''button 3是我在更改变量名称时未更改的冲浪按钮,因此它保留了button3_click
如果结束
结束子

Sub PerformSURFDetectionAndUpadateGUI(ByVal sender作为对象,ByVal arg作为EventArgs)
如果(rdoImageFile.Checked = True)然后
如果(blnImageSceneLoaded = False或blnIageToFIndLoaded = False或imgSceneColor无效或imgtofindcolor无效)那么
Me.Text =两个图像中的一个或两个均未加载,请在执行冲浪之前选择图像"
返回

如果结束
Me.Text =加工前毛重.............."
Application.DoEvents()
stopwatch.Restart()
''''''''''''''''在这里''''''''''''''''''''
否则(rdoWebCam.Checked = True)然后
试试

imgSceneColor = capWebcam.QueryFrame()
异常捕获
Me.Text = ex.Message
返回
结束尝试
如果(imgSceneColor什么都没有)那么
Me.Text =错误,img场景颜色为空"
返回
如果结束
如果(imgtofindcolor什么都没有)那么
ibResult.Image = imgSceneColor
返回
如果结束
如果结束

''prfrm在这两个img上冲浪
Dim surfDetector As SURFDetector = New SURFDetector(500,False)''初始化SURF obj,参数为阈值和扩展标志
昏暗的imgSceneGray作为图像(灰色,字节)=什么都没有
昏暗的imgToFindGray作为图像(灰色,字节)=什么都没有

将vkpSceneKeyPoints昏暗为VectorOfKeyPoint''场景图像中关键点的矢量
将vkpToFindKeyPoints调暗为VectorOfKeyPoint''kp的矢量以查找图像
Dim mtxSceneDescriptors As Matrix(Of Single)''描述符矩阵
Dim mtxToFindDescriptors As Matrix(Of Single)''描述的矩阵[tors in find imagh
Dim mtxMatchIndices As Matrix(Integer)''描述符矩阵,将由训练矩阵得出
Dim mtxDistance As Matrix(Of Single)''''距离值矩阵来自训练数据
Dim mtxMask As Matrix(Of Byte)''输入和输出fumvtion投票均具有唯一性()
Dim bruteForceMatcher As BruteForceMatcher(Of Single)''对于每个描述符,我在第一个集合中amtcher在第二个集合中找到匹配项
Dim homographyMatrix As HomographyMatrix =什么都没有'用于点投影

Dim intKNumNearestNeigh As Integer = 2
Dim dblUniquenessthreshold As Double = 0.8''匹配的距离差比率
将Dim intNumNonZeroElements As Integer用作非零元素的返回值

Dim dblScaleIncrement为Double = 1.5
昏暗的intRotationBins为整数= 20


Dim dblRansacprojthresh As Double = 2.0''以获取单应性特征
在矩形上暗淡rectImageToFind''围绕着您的4点draqing框在场景中找到了图像
将ptfPointsF昏暗为PointF()''4个点在一个圆周围定义一个框
Dim ptPoints As Point()''''''''''''''''''''''''''''''''


imgSceneGray = imgSceneColor.Convert(Of Gray,Byte)()''将场景颜色图像转换为灰度
imgToFindGray = imgtofindcolor.Convert(Of Gray,Byte)()''转换为查找灰度的彩色图像


vkpSceneKeyPoints = surfDetector.DetectKeyPointsRaw(imgSceneGray,Nothing)''检测场景中的关键点
mtxSceneDescriptors = surfDetector.ComputeDescriptorsRaw(imgSceneGray,Nothing,vkpSceneKeyPoints)''在场景中计算sedne描述符

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

bruteForceMatcher =新的BruteForceMatcher(单身)(DistanceType.L2)
bruteForceMatcher.Add(mtxToFindDescriptors)''添加矩阵以查找蛮力Maycher
mtxMatchIndices =新矩阵(整数)(mtxSceneDescriptors.Rows,intKNumNearestNeigh)
mtxDistance =新矩阵(单个)(mtxSceneDescriptors.Rows,intKNumNearestNeigh)


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


mtxMask = New Matrix(Of Byte)(mtxDistance.Rows,1)''掩码矩阵
mtxMask.SetValue(255)


Features2DToolbox.VoteForUniqueness(mtxDistance,dblUniquenessthreshold,mtxMask)''过滤匹配的恐惧

intNumNonZeroElements = CvInvoke.cvCountNonZero(mtxMask)
如果(intNumNonZeroElements> = 4)则''使用ransac阈值获取单应性
intNumNonZeroElements = Feature2DToolbox.VoteForSizeAndOrientation(vkpToFindKeyPoints,vkpSceneKeyPoints,mtxMatchIndices,mtxMask,dblScaleIncrement,intRotationBins)
如果(intNumNonZeroElements> = 4)然后
homographyMatrix = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(vkpToFindKeyPoints,vkpSceneKeyPoints,mtxMatchIndices,mtxMask,dblRansacprojthresh)

如果结束
如果结束
imgCopyOfImageToFindimWidthBorder = imgtofindcolor.Copy()
imgCopyOfImageToFindimWidthBorder.Draw(新矩形(1,1,imgCopyOfImageToFindimWidthBorder.Width-3,imgCopyOfImageToFindimWidthBorder.Height-3),bgrFoundImageColo,2)



如果(ckDrawKeyPoints.Checked = True和ckDrawMatching.Checked = True)然后

imgResult = Features2DToolbox.DrawMatches(imgCopyOfImageToFindimWidthBorder,vkpToFindKeyPoints,imgSceneColor,vkpSceneKeyPoints,mtxMatchIndices,bgrMatchingLineColor,bgrKeyPointolor,mtxMask,Features2DToolbox.KeypointDrawTypeDEFA.
''画线和点




ElseIf(ckDrawKeyPoints.Checked = True和ckDrawMatching.Checked = False)然后
''在图像上绘制具有关键点的场景

imgResult = Features2DToolbox.DrawKeypoints(imgSceneColor,vkpSceneKeyPoints,bgrKeyPointolor,Features2DToolbox.KeypointDrawType.DEFAULT)
''在图像上绘制要查找的关键点

imgCopyOfImageToFindimWidthBorder = Features2DToolbox.DrawKeypoints(imgCopyOfImageToFindimWidthBorder,vkpToFindKeyPoints,bgrKeyPointolor,Features2DToolbox.KeypointDrawType.DEFAULT)
imgResult = imgResult.ConcateHorizo​​ntal(imgCopyOfImageToFindimWidthBorder)


ElseIf(ckDrawKeyPoints.Checked = False和ckDrawMatching.Checked = True)然后
imgResult = imgSceneColor
imgResult = imgResult.ConcateHorizo​​ntal(imgCopyOfImageToFindimWidthBorder)
其他
''永远不应该到这里

如果结束

''''以场景颜色在iamge周围绘制边框
如果(homographyMatrix不为空)那么''chedck以确保单应性矩阵不为空
rectImageToFind.x = 0
rectImageToFind.Y = 0

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

对应于矩形的"pointsF的数组的整数"
ptfPointsF = New PointF(){New PointF(rectImageToFind.Left,rectImageToFind.Top),New PointF(rectImageToFind.Right,rectImageToFind.Top),New PointF(rectImageToFind.Right,rectImageToFind.Bottom),新PointF(rectImageToFind.Left, rectImageToFind.Bottom)}
homographyMatrix.ProjectPoints(ptfPointsF)''obox在场景图像中的位置

"从pointf []到point的竞争,因为项目points()需要pointsf []但drawpoly []需要point []
ptPoints =新Point(){Point.Round(ptfPointsF(0)),Point.Round(ptfPointsF(1)),Point.Round(ptfPointsF(2)),Point.Round(ptfPointsF(3))}



imgResult.DrawPolyline(ptPoints,True,bgrFoundImageColo,2)''在结果图像的找到的法师部分周围绘制边框


如果结束

ibResult.Image = imgResult
如果(rdoImageFile.Checked = True)然后
stopwatch.Stop()
Me.Text =处理时间" +秒表.Elapsed.TotalSeconds.ToString()+秒,完成处理,如果需要,请选择其他图像"
如果结束




结束子

私有Sub rdoImageFile_CheckedChanged(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理rdoImageFile.CheckedChanged
如果(rdoImageFile.Checked = True)然后
如果(blnWebcamCapturingInProcess = True)然后
RemoveHandler Application.Idle,新建EventHandler(AddressOf Me.PerformSURFDetectionAndUpadateGUI)
blnWebcamCapturingInProcess = False
如果结束
imgSceneColor =没什么
imgtofindcolor =没什么
imgCopyOfImageToFindimWidthBorder =没什么
imgResult =什么都没有
blnImageSceneLoaded = False
blnIageToFIndLoaded = False


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


Me.Text =说明:使用浏览器选择文件,然后按浏览检测"''更新标题栏文本
btnPerformSurfOnGteImageToTrack.Text =执行SURF检测"''更新按钮文本
ibResult.Image = Nothing

lblImageScene.Visible = True''显示控件
lblImageToFind.Visible = True
txtImageScene.Visible = True
txtImageToFind.Visible = True
txtImageScene.Visible = True
btnImageToFind.Visible = True


如果结束
结束子

私有子Form1_Load(ByVal发送者作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load

结束子
结束类

解决方案

尝试

capWebcam = New Capture()
异常捕获
Me.Text = ex.Message
返回

结束尝试


试试这个----------------------->

试试

capWebcam =新捕获(0)
异常捕获
Me.Text = ex.Message
返回

结束尝试

---------------------------------------------
放置设备索引0、1、2、3取决于网络摄像头连接的数量
一手放
capWebcam =新捕获(0)

对于两个网络摄像头,又初始化了一个变量....
capWebcam_1 =新捕获(0)
capWebcam_2 = New Capture(1)

hoope可以正常工作.

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

解决方案

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..


这篇关于网络摄像头无法启动OPENCV,EMGUCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆