从相机捕获匹配模板时访问冲突/程序无响应。 (C#EmguCV)

时间:2016-03-11 10:12:15

标签: c# camera emgucv capture matchtemplate

以下是我目前在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 finding image a in image b

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

当我从代码中删除使用匹配模板段时,网络摄像头正常工作。

任何输入都会受到赞赏,谢谢和抱歉,如果这是一个明显的错误,我仍然是这种事情的新手。

编辑:忘记提及它在调试程序时出现此错误

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

1 个答案:

答案 0 :(得分:1)

解决了,经过几个小时试图弄乱代码后,原来使用的模板并不是一个好的。

在详细了解代码的作者所说的内容之后,他提到你可能想要模板周围的灰色,认为他的意思是Bgr to Gray,这就是我对代码感到沮丧的原因。原来它意味着你的模板周围需要灰色。