1

I am using EmguCV with C#, I am facing a problem when I want to grab frames from my web cam, red underline appears on statement:

imgOrg = capturecam.QueryFrame();

error: Cannot implicitly convert type 'Emgu.CV.Mat' to 'Emgu.CV.Image

how can I solve this problem?

my code:

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;
        }
    }
}
Nouman Ahmed
  • 21
  • 1
  • 1
  • 3
  • What is the type of `imgOrg`? What is the return type of `capturecam.QueryFrame()`? Make sure both types are the same. – ekad Jul 05 '15 at 05:59
  • yes same type, I just copy paste my previous code that was working correctly, I download latest EmguCV cuda-3.0.0.2158 Image imgOrg; private Capture capturecam; – Nouman Ahmed Jul 06 '15 at 00:57
  • Please [edit your question](http://stackoverflow.com/posts/31226020/edit) and add the code and any other additional information to the question so other people can understand your question better and hopefully you'll get better answers. – ekad Jul 06 '15 at 01:11
  • same code I run correctly from a year ago, One new thing in this project that I am using latest version of Emgu CV library – Nouman Ahmed Jul 06 '15 at 17:28

6 Answers6

6

This statement works:

Image<Bgr, Byte> img = mat.ToImage<Bgr, Byte>();
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Byung K Choi
  • 61
  • 1
  • 2
  • This may answer the question, but I don't see how. There's no mat variable and I don't know where that line would go. Maybe some more details and an explanation of why solves the problem. – blm Nov 28 '15 at 07:20
2

Try this:

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

Look here: how to convert mat to image in (Emgu CV version 3) in c#?

Or change your Image variable to Mat:

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

This Code Worked For me. I hope working for you too.

shilovk
  • 11,718
  • 17
  • 75
  • 74
  • It would be good to expand upon this answer by explaining what the code does that is different and how it fixes the problem. You can also put the code into code formatting to make the answer easier to read. – Klors Sep 30 '15 at 11:39
  • Well, Type of `imgOrg` is Image and the output of `QueryFrame()`'s function is a matrix. so i defined an variable named `imgOrg` and like when you load an image , used `new Image` and the input was `capturecam.QueryFrame().Bitmap` , so in one single line i converted the output matrix to bitmap. so worked fine. – Amin Shouraki Sep 30 '15 at 11:51
1

This is pretty straight forward. This works in 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;  
BoiseBaked
  • 792
  • 7
  • 15
0

Image FRAME2= frame.ToImage();

-1

Reference the example, the QueryFrame() not be used anymore at EmguCV 3.0. it be replaced by Retrieve() function.

Sample as shown below:

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

        imageBox1.Image = frame;
        imageBox2.Image = grayFrame;