I take a screenshot of the screen area, then search for a match using the template.
An error appears on the line.
Image<Bgr, Byte> source = Emgu.CV.BitmapExtension.ToImage<Bgr, Byte>(bitmap);
System.ArgumentException: “Parameter is not valid.”
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
private void btnStart_Click(object sender, EventArgs e)
{
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 1200;
rect.bottom = 800;
Rectangle bounds = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
var bitmap = new Bitmap(bounds.Width, bounds.Height);
using (bitmap)
{
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
}
//----** ERROR**
Image<Bgr, Byte> source = Emgu.CV.BitmapExtension.ToImage<Bgr, Byte>(bitmap);
// and this option doesn't work
// Image<Bgr, Byte> source = bitmap.ToImage<Bgr, Byte>();
Image<Bgr, byte> template = new Image<Bgr, byte>("scr-over/temple.bmp");
Image<Bgr, byte> imageToShow = source.Copy();
using (Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.9)
{
Rectangle match = new Rectangle(maxLocations[0], template.Size);
imageToShow.Draw(match, new Bgr(Color.Red), 1);
pictureBox1.Image = imageToShow.ToBitmap();
}
}
imageToShow.Save("scr-over/result.bmp");
}
New contributor
Mari Rveze is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.