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); } }
public ScanAreaModel(ScanArea scanArea) { this.Key = scanArea.Key; this.X = scanArea.X; this.Y = scanArea.Y; this.Width = scanArea.Width; this.Height = scanArea.Height; this.Hash = scanArea.Hash; this.ImageLocation = scanArea.Image; this.BaseResolution = scanArea.BaseResolution; this.Mostly = scanArea.Mostly; // this.Image = GetAreaImage(scanArea.Key); }
public ScanAreaModel(ScanArea scanArea) { Key = scanArea.Key; X = scanArea.X; Y = scanArea.Y; Width = scanArea.Width; Height = scanArea.Height; Hash = scanArea.Hash; ImageLocation = scanArea.Image; BaseResolution = scanArea.BaseResolution; Mostly = scanArea.Mostly; // this.Image = GetAreaImage(scanArea.Key); }
/// <summary>The detect.</summary> /// <param name="lookup">The lookup.</param> /// <param name="key">The key.</param> /// <param name="useArea"></param> /// <param name="i"></param> /// <returns>The <see cref="bool"/>.</returns> private DetectionResult Detect(ScanAreaDictionary lookup, string key, IDictionary <int, ScanArea> useArea = null, int threshold = -1) { threshold = threshold >= 0 ? threshold : ThreshHold; if (!lookup.ContainsKey(key)) { Log.Error("No scan data found for requested template: {0}", key); return(new DetectionResult(false, -1)); } var template = lookup[key]; // var rect = new Rectangle(); int theResolution; // ulong matchhash; ScanArea area = null; if (template.ContainsKey(this.lastResolution)) { // rect = template[this.lastResolution].Rect; // matchhash = template[this.lastResolution].Hash; area = template[this.lastResolution]; theResolution = this.lastResolution; } else if (template.ContainsKey(this.BaseResolution)) { // rect = template[this.BaseResolution].Rect; // matchhash = template[this.BaseResolution].Hash; area = template[this.BaseResolution]; theResolution = this.BaseResolution; } else { Log.Error("No scan data found for requested template: " + key); return(new DetectionResult(false, -1)); } if (useArea != null) { if (useArea.ContainsKey(this.lastResolution)) { // rect = useArea[this.lastResolution].Rect; area = useArea[this.lastResolution]; theResolution = this.lastResolution; } else if (useArea.ContainsKey(this.BaseResolution)) { // rect = useArea[this.BaseResolution].Rect; area = useArea[this.BaseResolution]; theResolution = this.BaseResolution; } else { Log.Error("No scan data found for requested template: " + key); return(new DetectionResult(false, -1)); } } var source = this.image; int distance = -1; DetectionResult result = new DetectionResult(); using (var roi = source.Lock(ResolutionHelper.CorrectRectangle(source.Size, area.Rect, theResolution), source.PixelFormat)) { // var roi = source.Clone(ResolutionHelper.CorrectRectangle(source.Size, area.Rect, theResolution), source.PixelFormat); //Tuple<byte[], DetectionResult> cached = null; //byte[] roiBytes = roi.GetBytes(); //if (hashCache.TryGetValue(key, out cached)) //{ // if (ImageUtils.AreEqual(cached.Item1, roiBytes)) // { // TraceLog.Log("hash cache hit: {0}, hit: {1} distance: {2}", key, cached.Item2.Found, cached.Item2.Distance); // return cached.Item2; // } //} var hash = this.imageHasher.Create(roi.Data); distance = PerceptualHash.HammingDistance(hash, area.Hash); TraceLog.Log("Detecting '{0}'. Distance: {1}", key, distance); if (distance <= threshold) { TraceLog.Log("Detected '{0}'. Distance: {1}", key, distance); Mostly mostly; if (!String.IsNullOrEmpty(area.Mostly) && Enum.TryParse(area.Mostly, out mostly)) { result = new DetectionResult(roi.Data.IsMostly(mostly), distance); } else { result = new DetectionResult(true, distance); } } else { result = new DetectionResult(false, distance); } // hashCache.Set(key, new Tuple<byte[], DetectionResult>(roiBytes, result)); } return(result); }