【问题标题】:EmguCV capture error: Cannot implicitly convert type 'Emgu.CV.Mat' to 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>EmguCV 捕获错误:无法将类型 'Emgu.CV.Mat' 隐式转换为 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>
【发布时间】:2015-07-04 23:45:59
【问题描述】:

我在 C# 中使用 EmguCV,当我想从网络摄像头抓取帧时遇到问题,语句上出现红色下划线:

imgOrg = capturecam.QueryFrame();

错误:无法将类型“Emgu.CV.Mat”隐式转换为“Emgu.CV.Image”

我该如何解决这个问题?

我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;

namespace test2
{
    public partial class Form1 : Form
    {
        Image<Bgr, Byte> imgOrg; //image type RGB (or Bgr as we say in Open CV)
        private Capture capturecam;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                capturecam = new Capture();
            }
            catch (NullReferenceException exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }
            Application.Idle += new EventHandler(ProcessFunction);

        }
        private void ProcessFunction(object sender, EventArgs arg)
        {
            imgOrg = capturecam.QueryFrame(); // error line
            imageBox1.Image = imgOrg;
        }
    }
}

【问题讨论】:

  • imgOrg的类型是什么? capturecam.QueryFrame() 的返回类型是什么?确保两种类型相同。
  • 是的相同类型,我只是复制粘贴我以前正常工作的代码,我下载了最新的 EmguCV cuda-3.0.0.2158 Image imgOrg;私人捕获捕获摄像头;
  • edit your question 并将代码和任何其他附加信息添加到问题中,以便其他人可以更好地理解您的问题,并希望您能得到更好的答案。
  • 我在一年前正确运行的相同代码,这个项目中的一个新事物是我正在使用最新版本的 Emgu CV 库

标签: c# emgucv webcam-capture


【解决方案1】:

此语句有效:

Image<Bgr, Byte> img = mat.ToImage<Bgr, Byte>();

【讨论】:

  • 这可能会回答这个问题,但我不明白如何。没有 mat 变量,我不知道那条线会去哪里。也许更多细节和解释为什么可以解决问题。
【解决方案2】:

试试这个:

imgOrg = capturecam.QueryFrame().ToImage<Bgr, Byte>();

看这里: how to convert mat to image in (Emgu CV version 3) in c#?

或者将您的 Image 变量更改为 Mat:

Mat imgOrg = new Mat(); // instead of: Image<Bgr, Byte> imgOrg;
imgOrg = capture.QueryFrame();
imageBox1.Image = imgOrg;

【讨论】:

    【解决方案3】:
    imgOrg = new Image(capturecam.QueryFrame().Bitmap);
    

    此代码对我有用。我也希望为你工作。

    【讨论】:

    • 最好通过解释代码的不同之处以及它如何解决问题来扩展这个答案。也可以把代码变成代码格式,让答案更容易阅读。
    • 嗯,imgOrg 的类型是图像,QueryFrame() 函数的输出是矩阵。所以我定义了一个名为imgOrg 的变量,就像加载图像时一样,使用new Image&lt;Bgr, byte&gt;,输入为capturecam.QueryFrame().Bitmap,所以在一行中我将输出矩阵转换为位图。所以工作得很好。
    【解决方案4】:

    这很简单。这适用于 Emgu CV 3.3++。

    vidCap = new VideoCapture([filename or webcam device]);  
    Mat mat = new Mat();  
    vidCap.Read(mat);  //This calls Grab() as grabbing a frame and then Retrieve();  
    imageBox1.Image = mat.Bitmap;  
    

    【讨论】:

    • 这是最好的答案。它也适用于 Core 3.1!
    【解决方案5】:

    Image FRAME2= frame.ToImage();

    【讨论】:

      【解决方案6】:

      参考示例,在 EmguCV 3.0 中不再使用 QueryFrame()。它被 Retrieve() 函数取代。

      示例如下:

              Mat frame = new Mat();
              cap.Retrieve(frame, 0);
              Mat grayFrame = new Mat();
              CvInvoke.CvtColor(frame, grayFrame, ColorConversion.Bgr2Gray);
      
              imageBox1.Image = frame;
              imageBox2.Image = grayFrame;
      

      【讨论】:

      • QueryFrame 实际上是 Grab() 后跟 Retreive()
      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2019-11-21
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多