enter image description here
I'm making a bot for a game, and I need it to find buttons and click on them. I decided to search for buttons on the screen using the example, but matchTemplate does not always accurately find buttons, and I understand that if the image size decreases or increases, then this function will not work. I would like to know if there are any other solutions to my problem.
ScreenShot();
Image<Bgr, byte> source = new Image<Bgr, byte>(ScreenShot());
Image<Gray, byte> hsvSource = source.Convert<Gray, byte>();
Image<Bgr, byte> template = new Image<Bgr, byte>(pathToTemplate); // Image A
Image<Gray, byte> hsvTemplate = template.Convert<Gray, byte>();
Image<Bgr, byte> imageToShow = source.Copy();
using (Image<Gray, float> result = hsvSource.MatchTemplate(hsvTemplate, TemplateMatchingType.CcoeffNormed))
{
double[] minValues, maxValues;
Point[] minLocations, maxLocations;
result.MinMax(out minValues, out maxValues, out minLocations, out maxLocations);
if (maxValues[0] > 0.8)
{
Rectangle match = new Rectangle(maxLocations[0], template.Size);
//MessageBox.Show($"{match.X + offsetX} X {match.Y + offsetY}");
//Clicker(match.X + offsetX, match.Y + offsetY);
TestFunctions.Click(match.X + offsetX, match.Y + offsetY);
//TestFunctions.ClikerAlt(match.X + offsetX, match.Y + offsetY);
imageToShow.Draw(match, new Bgr(Color.Red), 3);
return;
}
}
public string ScreenShot()
{
Rectangle bounds = Screen.AllScreens[0].Bounds;
Bitmap bitmap = new Bitmap(1500, 800, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}
string screenshotPath =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.png");
bitmap.Save(screenshotPath, ImageFormat.Png);
return screenshotPath;
}