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



这是我目前在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:yugiCardskuriboh.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 在图像 B 中查找图像 A

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

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

任何输入将不胜感激,感谢和抱歉,如果这是一个明显的错误,我仍然对这种事情很陌生。

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

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

解决了,经过几个小时的尝试弄乱代码,事实证明所使用的模板不是一个好的模板。

在进一步研究了代码作者所说的话之后,他提到你可能想要在你的模板周围使用灰色,认为他的意思是 Bgr 到 Gray,这就是我对代码感到沮丧的原因。事实证明,这意味着您的模板周围确实需要灰色。

相关内容

最新更新