-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageMatcher.cs
57 lines (51 loc) · 1.99 KB
/
ImageMatcher.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenCvSharp;
using System.Diagnostics;
namespace BiuBiuClick
{
class ImageMatcher
{
public static System.Drawing.Point FindImageCenter(string targetImagePath, string templateImagePath)
{
Point p = RectCenter(Match(targetImagePath, templateImagePath));
return new System.Drawing.Point(p.X, p.Y);
}
public static Rect Match(string targetImagePath, string templateImagePath)
{
Mat template = Cv2.ImRead(templateImagePath);
Mat target = Cv2.ImRead(targetImagePath);
return Match(target, template);
}
public static Rect Match(Mat target, Mat template)
{
Mat result = new Mat();
Cv2.MatchTemplate(target, template, result, TemplateMatchModes.SqDiffNormed);
Point minLoc, maxLoc;
Cv2.MinMaxLoc(result, out minLoc, out maxLoc);
return new Rect(minLoc, template.Size());
}
public static Point RectCenter(Rect rect)
{
return new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
}
public static void Main(string[] args)
{
string targetImagePath = "C:/Users/zen/Pictures/screen.png", templateImagePath = System.Environment.CurrentDirectory + "/app/config/potplayer/potplayer-play.png";
Stopwatch sw = new Stopwatch();
sw.Start();
Rect rect = Match(targetImagePath, templateImagePath);
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
Mat target = Cv2.ImRead(targetImagePath);
Cv2.Rectangle(target, rect, Scalar.Blue, 1);
Cv2.NamedWindow("target", WindowFlags.KeepRatio);
Cv2.Circle(target, RectCenter(rect), 2, Scalar.Red);
Cv2.ImShow("target", target);
Cv2.WaitKey(0);
}
}
}