private float DetectTemplate(ScanAreaImageDictionary lookup, string key) { if (!lookup.ContainsKey(key)) { Log.Error("No scan data found for requested template: {0}", key); return(-1); } var template = lookup[key]; var rect = new Rectangle(); int theResolution; Bitmap matchhash; ScanArea area = null; if (template.ContainsKey(this.lastResolution)) { area = template[this.lastResolution].Item2; // rect = new Rectangle(area.X, area.Y, area.Width, area.Height); matchhash = template[this.lastResolution].Item1; theResolution = this.lastResolution; } else if (template.ContainsKey(this.BaseResolution)) { area = template[this.BaseResolution].Item2; // rect = new Rectangle(area.X, area.Y, area.Width, area.Height); matchhash = template[this.BaseResolution].Item1; theResolution = this.BaseResolution; } else { Log.Error("No scan data found for requested template: " + key); return(-1); } var source = this.image; rect = new Rectangle(area.X, area.Y, area.Width, area.Height); if (area.BaseResolution > 0) { // theResolution = area.BaseResolution; } var templatesize = new Size(matchhash.Width, matchhash.Height); var roiRect = ResolutionHelper.CorrectRectangle(source.Size, rect, theResolution); //var roiRect = ResolutionHelper.CorrectPoints(source.Size, rect, theResolution); templatesize = ResolutionHelper.CorrectSize(source.Size, templatesize, area.BaseResolution > 0 ? area.BaseResolution : theResolution); roiRect.Inflate(5, 5); // roiRect.X -= 5; // roiRect.Y -= 5; using (var roi = source.Clone(roiRect, PixelFormat.Format24bppRgb)) { var newhash = new Bitmap(templatesize.Width, templatesize.Height, PixelFormat.Format24bppRgb); var graph = Graphics.FromImage(newhash); graph.InterpolationMode = InterpolationMode.HighQualityBicubic; graph.DrawImage(matchhash, 0, 0, templatesize.Width, templatesize.Height); var ismatch = templateMatcher.IsMatch(roi, newhash); graph.Dispose(); newhash.Dispose(); return(ismatch); } }
private void LoadScanAreas() { this.areas = new ScanAreaDictionary(); this.heroes = new ScanAreaDictionary(); this.opponentHeroes = new ScanAreaDictionary(); this.decks = new ScanAreaDictionary(); this.arenaHeroes = new ScanAreaDictionary(); this.arenaWinsLookup = new ScanAreaDictionary(); this.arenaWinsLookup2 = new ScanAreaImageDictionary(); this.arenaLossesLookup = new ScanAreaDictionary(); this.scanAreaProvider.Load(); this.scanareas = this.scanAreaProvider.GetScanAreas(); foreach (var scanArea in this.scanareas) { var allareas = scanArea.Areas.ToDictionary(x => x.Key, x => x); foreach (var a in allareas) { if (a.Key.StartsWith("hero_")) { this.InitAreas(a.Key, "hero_", scanArea, this.heroes, a); } else if (a.Key.StartsWith("opphero_")) { this.InitAreas(a.Key, "opphero_", scanArea, this.opponentHeroes, a); } else if (a.Key.StartsWith("deck_")) { this.InitAreas(a.Key, "deck_", scanArea, this.decks, a); } //else if (a.Key.StartsWith("arenahero_")) //{ // this.InitAreas(a.Key, "arenahero_", scanArea, this.arenaHeroes, a); //} else if (a.Key.StartsWith("arenawins_")) { this.InitAreas(a.Key, "arenawins_", scanArea, this.arenaWinsLookup, a); this.InitImageAreas(a.Key, "arenawins_", scanArea, this.arenaWinsLookup2, a); } else if (a.Key.StartsWith("arenaloss_")) { this.InitAreas(a.Key, "arenaloss_", scanArea, this.arenaLossesLookup, a); } else if (a.Key.StartsWith("arena_hero_")) { this.InitAreas(a.Key, "arena_hero_", scanArea, this.arenaHeroes, a); } else { this.InitAreas(a.Key, string.Empty, scanArea, this.areas, a); } } } }
private string DetectBest(ScanAreaImageDictionary lookup) { string bestKey = null; float best = 0; foreach (var key in lookup.Keys) { var detect = DetectTemplate(lookup, key); if (detect > best) { best = detect; bestKey = key; } } return(bestKey); }
private void InitImageAreas(string arrkey, string prefix, ScanAreas scanArea, ScanAreaImageDictionary lookup, KeyValuePair <string, ScanArea> a) { var key = arrkey.Substring(prefix.Length); if (!lookup.ContainsKey(key)) { // lookup[key] = new Tuple<ulong, IDictionary<int, Rectangle>>(a.Value.Hash, new Dictionary<int, Rectangle>()); lookup[key] = new Dictionary <int, Tuple <Bitmap, ScanArea> >(); } var tmp = lookup[key]; if (!tmp.ContainsKey(scanArea.BaseResolution)) { // var image = (Bitmap)Image.FromFile(Path.Combine(BasePath, a.Value.Image)); var image = (Bitmap)scanAreaProvider.GetImage(a.Value.Image); tmp[scanArea.BaseResolution] = new Tuple <Bitmap, ScanArea>(image, a.Value); } }