【问题标题】:Mat to Bitmap converter - Argument Exception垫到位图转换器 - 参数异常
【发布时间】:2019-11-29 14:48:02
【问题描述】:

我在 VB Net Forms 应用程序中使用 OpenCVSharp3。我有这个使用 C# 但现在试图转换为 VB Net。我想将网络摄像头源显示到图片框。

     ''' <summary>
    ''' Feed from camera
    ''' </summary>
    ''' <remarks></remarks>
    Private _capture As VideoCapture

    ''' <summary>
    ''' Frame from camera
    ''' </summary>
    ''' <remarks></remarks>
    Private _frame As Mat

    ''' <summary>
    ''' Image
    ''' </summary>
    ''' <remarks></remarks>
    Private _image As Bitmap

     Private Sub CaptureCameraCallback()
        Try
            _isCameraRunning = True
            _frame = New Mat()
            _capture = New VideoCapture()
            _capture.Open(_cameraDevice)

            _capture.Read(_frame)

            If _frame IsNot Nothing Then
                While _isCameraRunning
                    _image = BitmapConverter.ToBitmap(_frame)
                    captureFrame.Image = _image
                    _image = Nothing
                End While
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub CaptureCamera()
        _camera = New Thread(AddressOf CaptureCameraCallback)
        _camera.Start()
    End Sub

我有上面的代码来将网络摄像头提要显示到图片框。但是当我运行它时,我得到了以下异常:

A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll

线上出现异常:

_image = BitmapConverter.ToBitmap(_frame)

我想我需要在某个地方处理,但不确定这是否是这里的问题。

正在运行的 C# 代码 -

 // Declare required methods
    private void CaptureCamera()
    {
        camera = new Thread(new ThreadStart(CaptureCameraCallback));
        camera.Start();
    }

    private void CaptureCameraCallback()
    {
        try
        {
            frame = new Mat();
            capture = new VideoCapture();
            capture.Open(CameraDevice);
            while (isCameraRunning)
            {
                capture.Read(frame);
                image = BitmapConverter.ToBitmap(frame);
                captureFrame.Image = image;
                image = null;
            }
        }
        catch (Exception)
        {
            MessageBox.Show("No camera found on device", "Start Stream",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
            if (streamBtn.InvokeRequired)
            {
                streamBtn.Invoke(new MethodInvoker(delegate { streamBtn.Text = "Start"; }));
            }
        }
    }

解决方案

''' <summary>
    ''' Get feed from camera
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub CaptureCameraCallback()
        Me._isCameraRunning = True
        Me._frame = New Mat()
        Me._capture = New VideoCapture(_CAMERA_DEVICE)
        Me._capture.Open(_CAMERA_DEVICE)
        ' Enable button if there is a camera feed
        If _isCameraRunning Then
            Me.BeginInvoke(New SetButtonCallback(AddressOf SetButtonStatus), True)
        End If
        While (_isCameraRunning)
            Me._capture.Read(Me._frame)
            Me._image = BitmapConverter.ToBitmap(Me._frame)
            Me.captureFrame.BackgroundImage = Me._image
            Me._image = Nothing
        End While
    End Sub

【问题讨论】:

  • 原来的 C# 代码是什么样子的?没有看到它,很难检测到任何 C#/VB 转换问题(如果有的话)。
  • @OlivierJacot-Descombes 现在添加了 C# 代码。
  • VB 代码似乎等同于 C# 代码,只是您省略了 Do While isCameraRunning 循环和 Catch 子句的内容。你没有显示变量的声明(如_frame_capture),会不会有问题?
  • @blackwood 我现在用 while 循环更新了代码,并检查 _frame 是否不为空。当我调试时,它进入了 while 循环和检查,但仍然在同一行失败。

标签: vb.net opencvsharp


【解决方案1】:

原始代码确保相机正在运行

while (isCameraRunning) ...

如果你不想要一个循环,你应该使用一个

If isCameraRunning Then
    _capture.Read(_frame)
    ...
End If

否则您可能会得到无效帧。也许在尝试阅读后测试是否有框架也足够了

_capture.Read(_frame)
If _frame IsNot Nothing Then
    ...
End If

如果仍然出现异常,请进行进一步测试以确保框架有效:

_capture.Read(_frame)
If _frame IsNot Nothing AndAlso _frame.Width > 0 AndAlso _frame.Height > 0 Then
    ...
End If

【讨论】:

  • 我现在用 while 循环更新了代码,并检查 _frame 是否为空。当我调试时,它进入了 while 循环和检查,但仍然在同一行失败。奇怪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 2017-03-17
  • 1970-01-01
  • 2019-11-23
  • 2017-06-25
  • 1970-01-01
相关资源
最近更新 更多