1

Here is the code i'm currently using in C# and EmguCV included:

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();
    }

}
}

I am trying to find a template in the camera capture, however, when I run the program and start camera capture my camera LED lights up and then the program becomes not responding when it's trying to capture. Maybe it has to do with the placement of the match template function or the types of variables used but I'm not really sure since I'm not that experienced so I wanted some input on the problem with my code.

I do realize the imageToShow variable isn't needed but I decided to leave it be until I can get the thing working then I can mess around with it more myself.

Code for detecting the template was found here emgu finding image a in image b

It was mainly for detecting template between 2 static images though, but I edited the source to be from the webcam.

The webcam is working normally when I remove the using match template segment from the code.

Any input would be appreciated, thanks and sorry if it is an obvious error I'm still new to this kind of thing.

EDIT: Forgot to mention it gives this error when debugging The program

 '[6164] CameraCapture.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
Community
  • 1
  • 1
hyhae
  • 43
  • 7

1 Answers1

1

Solved, after hours of trying to mess around with the code it turned out the template being used just wasn't a good one.

After looking more into what the author of the code said, he mentioned you might want grey around your template, thought he meant the Bgr to Gray that's why I was frustrated with the code. Turned out it meant you literally needed grey around your template.

hyhae
  • 43
  • 7