EmguCV捕获错误:无法将类型'Emgu.CV.Mat'隐式转换为'Emgu.CV.Image <emgu.cv.structure.bgr,byte>

时间:2015-07-04 23:45:59

标签: c# emgucv webcam-capture

我正在使用带有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;
        }
    }
}

6 个答案:

答案 0 :(得分:6)

此声明有效:

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

答案 1 :(得分:1)

imgOrg = new Image(capturecam.QueryFrame().Bitmap);

此代码适用于我。我也希望为你工作。

答案 2 :(得分:1)

试试这个:

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 :(得分:0)

图片FRAME2 = frame.ToImage();

答案 4 :(得分:0)

这很简单。这适用于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;  

答案 5 :(得分:-1)

参考示例,在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;