1

这是我目前在 C# 和 EmguCV 中使用的代码:

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.Structure;
using Emgu.Util;

namespace CameraCapture
{
 public partial class CameraCapture : Form
{
    //declaring global variables
    private Capture capture;        //takes images from camera as image frames
    private bool captureInProgress; // checks if capture is executing

    public CameraCapture()
    {
        InitializeComponent();
    }

    private void ProcessFrame(object sender, EventArgs arg)
    {
        Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
        Image<Bgr, Byte> template = new Image<Bgr, byte>(@"D:\yugiCards\kuriboh.jpg");
        Image<Bgr, Byte> imageToShow = ImageFrame.Copy();


        using (Image<Gray, float> result = imageToShow.MatchTemplate(template, Emgu.CV.CvEnum.TM_TYPE.CV_TM_CCOEFF_NORMED))
        {
            double[] minValues, maxValues;
            Point[] minLocations, maxLocations;
            result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);

            // You can try different values of the threshold. I guess somewhere between 0.75 and 0.95 would be good.
            if (maxValues[0] > 0.9)
            {
                // This is a match. Do something with it, for example draw a rectangle around it.
                Rectangle match = new Rectangle(maxLocations[0], template.Size);
                imageToShow.Draw(match, new Bgr(Color.Red), 3);
            }
        }

        CamImageBox.Image = imageToShow; 
        //ImageFrame.Save(@"E:\MyPic.jpg");  //saves to location
    }

    private void CameraOutput_Load(object sender, EventArgs e)
    {

    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        #region if capture is not created, create it now
        if (capture == null)
        {
            try
            {
                capture = new Capture();
            }
            catch (NullReferenceException excpt)
            {
                MessageBox.Show(excpt.Message);
            }
        }
        #endregion

        if (capture != null)
        {
            if (captureInProgress)
            {  //if camera is getting frames then stop the capture and set button Text
                // "Start" for resuming capture
                btnStart.Text = "Start!"; //
                Application.Idle -= ProcessFrame;
            }
            else
            {
                //if camera is NOT getting frames then start the capture and set button
                // Text to "Stop" for pausing capture
                btnStart.Text = "Stop";
                Application.Idle += ProcessFrame;
            }

            captureInProgress = !captureInProgress;
        }
    }

    private void ReleaseData()
    {
        if (capture != null)
            capture.Dispose();
    }

}
}

我试图在相机捕捉中找到一个模板,但是,当我运行程序并开始相机捕捉时,我的相机 LED 亮起,然后程序在尝试捕捉时变得没有响应。也许它与匹配模板函数的放置或使用的变量类型有关,但我不太确定,因为我没有那么有经验,所以我想对我的代码问题进行一些输入。

我确实意识到不需要 imageToShow 变量,但我决定保留它,直到我可以让事情正常工作,然后我可以自己更多地处理它。

在这里找到了用于检测模板的代码 emgu find image a in image b

它主要用于检测 2 个静态图像之间的模板,但我将源编辑为来自网络摄像头。

当我从代码中删除 using match 模板段时,网络摄像头工作正常。

任何输入将不胜感激,感谢和抱歉,如果这是一个明显的错误,我对这种事情还是新手。

编辑:忘了提到它在调试程序时给出了这个错误

 '[6164] CameraCapture.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
4

1 回答 1

1

解决了,经过数小时试图弄乱代码后,结果发现正在使用的模板并不是一个好的模板。

在进一步研究了代码作者所说的内容后,他提到您可能希望模板周围有灰色,认为他的意思是 Bgr 到灰色,这就是我对代码感到沮丧的原因。原来这意味着你需要在你的模板周围使用灰色。

于 2016-03-11T22:57:40.637 回答