-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageProcessing.cs
More file actions
111 lines (65 loc) · 3.02 KB
/
ImageProcessing.cs
File metadata and controls
111 lines (65 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using Emgu;
using Emgu.CV;
using Emgu.CV.Structure;
namespace LordsBot
{
public class ImageProcessing
{
public static List<Point> LocateImageMultiple(int handl , Bitmap bmpImage, double threshold) {
Image<Bgr, byte> source = new Image<Bgr, byte>(ScreenshotWindow(handl));
Image<Bgr, byte> template = new Image<Bgr, byte>(bmpImage);
Image<Gray, float> result = source.MatchTemplate(template, Emgu.CV.CvEnum.TemplateMatchingType.CcoeffNormed);
List<Point> pointList = new List<Point>();
for (int y = 0; y < result.Data.GetLength(0); y++)
{
for (int x = 0; x < result.Data.GetLength(1); x++)
{
if (result.Data[y, x, 0] >= threshold) //Check if its a valid match
{
//Point loc = new Point(x, y);
//Image2 found within Image1
pointList.Add(new Point(x,y));
}
}
}
return pointList;
}
public static Point LocateImageSingle(int handl, Bitmap bmpImage, double threshold)
{
Image<Bgr, byte> source = new Image<Bgr, byte>(ScreenshotWindow(handl));
Image<Bgr, byte> template = new Image<Bgr, byte>(bmpImage);
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);
// value of threshold determinds how accurate the image match should be.
if (maxValues[0] > threshold)
{
// If there is a match. Returns the location of the match.
return maxLocations[0];
}
//if there is no match, returns empty point
return Point.Empty;
}
public static Bitmap ScreenshotWindow(int handl) {
RECT rc; //creates a rectangle
Win32.GetWindowRect(handl, out rc); //gets dimensions of the window
// Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); //creates a empty bitmap with the dimensions of the window
Bitmap bmp = new Bitmap(rc.Width, rc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics memoryGraphics = Graphics.FromImage(bmp);
IntPtr dc = memoryGraphics.GetHdc();
Win32.PrintWindow(handl, dc, 0);
memoryGraphics.ReleaseHdc(dc);
memoryGraphics.Dispose();
return bmp;
}
}
}