EMGU的。CV System.Runtime.InteropServices.SEHException,同时在 VB.NET 中在某个区域上方绘制轮廓 (VectorOfVectorOfPoint)

EMGU.CV System.Runtime.InteropServices.SEHException while drawing contours (VectorOfVectorOfPoint) above a certain area in VB.NET

提问人:user2959923 提问时间:5/19/2023 更新时间:5/19/2023 访问量:25

问:

我试图简单地在图像上某个区域上方绘制所有轮廓。 在我得到框架和轮廓后,我在迭代 contorus 时出现以下异常:

System.Runtime.InteropServices.SEHException: '外部组件具有 抛出了一个例外。

代码如下(它位于计时器内):

' cyclic routine
Dim frame As New Emgu.CV.Image(Of Bgr, Byte)(1920, 1080)
Dim currFr As Emgu.CV.Image(Of Bgr, Byte) = videoCap.QueryFrame().ToImage(Of Bgr, Byte)

' copy frame
frame = currFr.Resize(1, CvEnum.Inter.Lanczos4)
Dim imgContours As New Image(Of Gray, Byte)(frame.Size)
If backgroundFrame Is Nothing Then

    ' save this frame as background for subtraction
    backgroundFrame = frame
Else

    ' background subtraction
    Dim elFrame As New Image(Of Bgr, Byte)(frame.Size)
    elFrame = frame - backgroundFrame

    ' get contours from image (as VectorOfVectorOfPoint)
    imgContours = elFrame.Convert(Of Gray, Byte).ThresholdBinary(New Gray(80), New Gray(255))
    Dim contours As New Emgu.CV.Util.VectorOfVectorOfPoint()
    Dim hier As New Mat()
    CvInvoke.FindContours(imgContours, contours, hier, CvEnum.RetrType.External, CvEnum.ChainApproxMethod.ChainApproxSimple)

    ' draw all contours on the original frame of blue
    CvInvoke.DrawContours(frame, contours, -1, New MCvScalar(255, 0, 0), 5)

    ' iterate through contours
    For j As Integer = 0 To contours.Length - 1

        ' if the current contour area is above a threshold
        If CvInvoke.ContourArea(contours(j)) > threshold Then   <-------- HERE I GET THE EXCEPTION

            ' draw the current contour of red
            CvInvoke.DrawContours(frame, contours, j, New MCvScalar(0, 0, 255), 5)
        End If
    Next
End If

' display the frame in the PictureBox
picFrame.Image = frame.ToBitmap
vb.net 异常 emgucv

评论


答:

0赞 user2959923 5/19/2023 #1

我设法让它工作。 我不知道为什么,但代码需要从:

' iterate through contours
For j As Integer = 0 To contours.Length - 1

    ' if the current contour area is above a threshold
    If CvInvoke.ContourArea(contours(j)) > threshold Then

自:

' iterate through contours
Dim cont As Integer = contours.Size
For j As Integer = 0 To cont - 1

    Using cnt As Util.VectorOfPoint = contours(j)

        ' if the current contour area is above a threshold
        Dim area As Double = CvInvoke.ContourArea(cnt)
        If area > threshold Then
        
            ' draw the bounding box of the contour in red
            frame.Draw(CvInvoke.BoundingRectangle(cnt), New Bgr(0, 0, 255), 5)