使用C#的emgu CV不能隐含地&参数错误

时间:2014-10-01 06:21:38

标签: c# image-processing emgucv

我想用emguCV来检测网络摄像头上的圆圈。我没有任何经验,这是第一次。我正在尝试按照本教程https://www.youtube.com/watch?v=vdjoutNR2DQ,但似乎他使用的是不同的版本

- 在第168行Erorr:无法将类型Emgu.CV.Mat'隐式转换为'Emgu.CV.Image'

- 第171行错误无法将类型'Emgu.CV.Image'隐式转换为'Emgu.CV.Image'

- 第173行错误:'Emgu.CV.Image.HoughCircles(Emgu.CV.Structure.Bgr,Emgu.CV.Structure.Bgr,double,double,int,int)'的最佳重载方法匹配一些无效的论点

- 在同一行173错误:参数1& 2:无法从'Emgu.CV.Structure.Gray'转换为'Emgu.CV.Structure.Bgr

这些我在emguCV中使用的引用

enter image description here

这是代码

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.
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;

namespace videosearch
{
public partial class detect : Form
{

    Capture cp = null;
    bool blac = false;
    Image<Bgr, byte> imageorgnal;
    Image<Bgr, byte> imgproc;

    public detect()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (blac == true)
        {
            Application.Idle -= procframdatGUI;
            blac = false;
            button1.Text = "Resume";
        }
        else
        {
            Application.Idle += procframdatGUI;
            button1.Text = "pause";
            blac = true;
        }
    }

    private void detect_Load(object sender, EventArgs e)
    {
        try
        {
            cp = new Capture(Emgu.CV.CvEnum.CaptureType.DShow);
        }
        catch (NullReferenceException ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
        Application.Idle += procframdatGUI;
        blac = true;
    }
    private void detect_Close(object sender, FormClosedEventArgs e)
    {
        if (cp!=null)
        {
            cp.Dispose();
        }
    }
    void procframdatGUI(object sender, EventArgs e)
    {

        imageorgnal = cp.QueryFrame();//line 168 Error: Cannot implicitly convert type Emgu.CV.Mat'to 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>'   

        if (imageorgnal == null)
            return;
        imgproc = imageorgnal.InRange(new Bgr(0, 0, 175), new Bgr(100, 100, 256));// line 171    Error  Cannot implicitly convert type 'Emgu.CV.Image<Emgu.CV.Structure.Gray,byte>' to 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>'
        imgproc = imgproc.SmoothGaussian(9);
        CircleF[] cir = imgproc.HoughCircles(new Gray(100), new Gray(50), 2, imgproc.Height / 4, 10, 400); //In line 173 Error: The best overloaded method match for 'Emgu.CV.Image<Emgu.CV.Structure.Bgr,byte>.HoughCircles(Emgu.CV.Structure.Bgr, Emgu.CV.Structure.Bgr,double, double, int, int)' has some invalid arguments
// in same line Error   :Argument 1 &2: cannot convert from 'Emgu.CV.Structure.Gray' to 'Emgu.CV.Structure.Bgr'

        foreach (CircleF ci in cir)
        {
            if (textBox1.Text!="")
            {
                textBox1.AppendText(Environment.NewLine);

            }

            textBox1.AppendText("ball position x=" + ci.Center.X.ToString().PadLeft(4) + "\n Y= " + ci.Center.Y.ToString().PadLeft(4)+ "\n ridius"+ci.Radius.ToString("###.000").PadLeft(7));
            textBox1.ScrollToCaret();
            CvInvoke.Circle(imgproc, new Point((int)ci.Center.X, (int)ci.Center.Y), 3, new MCvScalar(0, 255, 0), -1, 0, 0);
            imageorgnal.Draw(ci, new Bgr(Color.Red), 3);


        }
        imageBox1.Image = imageorgnal;
        imageBox2.Image = imgproc;
    }

}
}

1 个答案:

答案 0 :(得分:0)

我想我解决了问题

对于第168行中的问题错误:无法将类型Emgu.CV.Mat'隐式转换为'Emgu.CV.Image' 我所做的是从文件中获取图像而不是使用Capture

   public string [] imagepath; 
   OpenFileDialog op = new OpenFileDialog();
        op.Multiselect = true;
        if (op.ShowDialog() != DialogResult.Cancel)
        {
            imagepath = new string[op.SafeFileNames.Length];
            imagepath = op.
        }

第171行中的问题错误无法将类型'Emgu.CV.Image'隐式转换为'Emgu.CV.Image' 我改变了图像的初始化

Image<Bgr, byte> imageorgnal;
Image<Gray, byte> imgproc;;

通过这样做它解决了第173行中的问题错误:参数1&amp; 2:无法从'Emgu.CV.Structure.Gray'转换为'Emgu.CV.Structure.Bgr' 然后我添加[0]来解决第173行中的问题

CircleF[] cir = imgproc.HoughCircles(new Gray(100), new Gray(50),5, imgproc.Height / 4, 10, 500)[0]; 

所以我在更改后做了这样的改动和这段代码

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.Util;
namespace videosearch
{
public partial class detect : Form
{
    int i = 0;
    bool blac = false;
    Image<Bgr, byte> imageorgnal;
    Image<Gray, byte> imgproc;
    string[] imagepath;

    public detect()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        // to run all the image 
        if (blac == true)
        {
            Application.Idle -= procframdatGUI;
            blac = false;
            button1.Text = "Resume";
        }
        else
        {
            Application.Idle += procframdatGUI;
            button1.Text = "pause";
            blac = true;
        }
    }
    void procframdatGUI(object sender, EventArgs e)
    {
        if (imagepath != null)
        {
            Bitmap p = new Bitmap(imagepath[i]);
            p = comparePic.ResizeBitmap(p, 680, 480);
            imageorgnal = new Image<Bgr, byte>(p);
            i++; //incrce i for next iamge 
            if (i >= imagepath.Length)
                i = 0;
            if (imageorgnal == null)
                return;
            imgproc = imageorgnal.InRange(new Bgr(0, 0, 175), new Bgr(100, 100, 256));
            imgproc = imgproc.SmoothGaussian(9);
            CircleF[] cir = imgproc.HoughCircles(new Gray(100), new Gray(50), 5, imgproc.Height / 4, 10, 500)[0];
            foreach (CircleF ci in cir)
            {
                if (textBox1.Text != "")
                textBox1.AppendText(Environment.NewLine);

                textBox1.AppendText("ball position x=" + ci.Center.X.ToString().PadLeft(4) + "\n Y= " + ci.Center.Y.ToString().PadLeft(4) + "\n ridius" + ci.Radius.ToString("###.000").PadLeft(7));
                textBox1.ScrollToCaret();
                CvInvoke.Circle(imgproc, new Point((int)ci.Center.X, (int)ci.Center.Y), 3, new MCvScalar(0, 255, 0), -1, LineType.AntiAlias, 0);
                imageorgnal.Draw(ci, new Bgr(Color.Red), 4);
            }
            imageBox1.Image = imageorgnal;
            imageBox2.Image = imgproc;
        }
        else
            MessageBox.Show("iamges haven't been selected");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //next image 
        procframdatGUI(null, null);
        blac = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //select images 
        OpenFileDialog op = new OpenFileDialog();
        op.Multiselect = true;
        if (op.ShowDialog() != DialogResult.Cancel)
        {
            imagepath = new string[op.SafeFileNames.Length];
            imagepath = op.FileNames;

        }
    }
}
}