Esempio n. 1
0
        //private string DetectBest(ScanAreaDictionary lookup, IDictionary<int, Tuple<ulong, Rectangle>> useArea, string debugkey)
        //{
        //    var hashes = new List<ulong>();
        //    var keys = new List<string>();

        //    var rect = new Rectangle();
        //    int theResolution;
        //    foreach (var key in lookup.Keys)
        //    {
        //        var template = lookup[key];
        //        ulong matchhash;
        //        if (template.ContainsKey(this.lastResolution))
        //        {
        //            matchhash = template[this.lastResolution].Hash;
        //        }
        //        else if (template.ContainsKey(this.BaseResolution))
        //        {
        //            matchhash = template[this.BaseResolution].Hash;
        //        }
        //        else
        //        {
        //            Log.Error("No scan data found for requested template: " + debugkey);
        //            return null;
        //        }
        //        hashes.Add(matchhash);
        //        keys.Add(key);
        //    }

        //    if (useArea.ContainsKey(this.lastResolution))
        //    {
        //        rect = useArea[this.lastResolution].Rect;
        //        theResolution = this.lastResolution;
        //    }
        //    else if (useArea.ContainsKey(this.BaseResolution))
        //    {
        //        rect = useArea[this.BaseResolution].Rect;
        //        theResolution = this.BaseResolution;
        //    }
        //    else
        //    {
        //        Log.Error("No scan data found for requested template: " + debugkey);
        //        return null;
        //    }

        //    var source = this.image;
        //    using (var roi = source.Clone(ResolutionHelper.CorrectRectangle(source.Size, rect, theResolution), PixelFormat.Format32bppRgb))
        //    {
        //        var hash = this.imageHasher.Create(roi);
        //        var best = PerceptualHash.FindBest(hash, hashes);
        //        if (best.Distance <= ThreshHold)
        //        {
        //            Log.Diag("Detected best hash: '{0}' Distance: {1}", debugkey, best.Distance);
        //            return keys[best.Index];
        //        }
        //    }

        //    return null;
        //}

        private string DetectBest(ScanAreaDictionary lookup, string debugkey, int?threshold = null)
        {
            threshold = threshold ?? ThreshHold;
            var hashes = new List <ulong>();
            var keys   = new List <string>();

            var rect          = new Rectangle();
            var theResolution = BaseResolution;

            foreach (var key in lookup.Keys)
            {
                var   template = lookup[key];
                ulong matchhash;
                if (template.ContainsKey(lastResolution))
                {
                    matchhash     = template[lastResolution].Hash;
                    rect          = template[lastResolution].Rectangle;
                    theResolution = lastResolution;
                }
                else if (template.ContainsKey(BaseResolution))
                {
                    matchhash     = template[BaseResolution].Hash;
                    rect          = template[BaseResolution].Rectangle;
                    theResolution = BaseResolution;
                }
                else
                {
                    Log.Error("No scan data found for requested template: " + debugkey);
                    return(null);
                }
                hashes.Add(matchhash);
                keys.Add(key);
            }

            var source = image;

            using (var roi = source.Lock(ResolutionHelper.CorrectRectangle(source.Size, rect, theResolution), PixelFormat.Format32bppRgb))
            {
                var hash = imageHasher.Create(roi.Data);
                var best = PerceptualHash.FindBest(hash, hashes);
                TraceLog.Log("Detected best hash: '{0}' Distance: {1}", debugkey, best.Distance);
                if (best.Distance <= threshold)
                {
                    return(keys[best.Index]);
                }
            }

            return(null);
        }
Esempio n. 2
0
        public void UpdateHash()
        {
            if (SelectedScanArea == null || Screenshot == null)
            {
                return;
            }
            var model = SelectedScanArea;
            var image = ImageHelper.BitmapImage2Bitmap(Screenshot);
            var roi   = image.Clone(new Rectangle((int)(this.Region.XPos + this.BoardX), (int)this.Region.YPos, (int)this.Region.Width, (int)this.Region.Height), image.PixelFormat);
            var hash  = hasher.Create(roi);

            var filename = AppDomain.CurrentDomain.BaseDirectory + "\\data\\images\\" + model.Key + ".png";

            roi.Save(filename, ImageFormat.Png);

            image.Dispose();
            model.Hash  = hash;
            model.Image = roi;

            if (!String.IsNullOrWhiteSpace(CompareHash))
            {
                CompareHashResult = PerceptualHash.HammingDistance(model.Hash, Convert.ToUInt64(CompareHash));
            }

            var color = roi.GetDominantColor();

            Mostly = String.Format("R: {0} G: {1} B: {2}", color.R, color.G, color.B);

            var corners = cornerDetecter.GetCorners(roi);
            var right   = 0;

            foreach (var c in corners)
            {
                if (c.X > right)
                {
                    right = c.X;
                }
            }
            RightMostCorner = right;
        }