Ejemplo n.º 1
0
 public override int GetHashCode()
 {
     unchecked
     {
         return(((TemplateMatch != null ? TemplateMatch.GetHashCode() : 0) * 397) ^ (OutputData != null ? OutputData.GetHashCode() : 0));
     }
 }
Ejemplo n.º 2
0
        private void DebugMatches(TemplateMatch m, Box box)
        {
            if (debugMode)
            {
                try
                {
                    Rectangle cropRect = m.Rectangle;
                    Bitmap    target   = new Bitmap(cropRect.Width, cropRect.Height);

                    using (Graphics g = Graphics.FromImage(target))
                    {
                        g.DrawImage(box.Image, new Rectangle(0, 0, target.Width, target.Height),
                                    cropRect,
                                    GraphicsUnit.Pixel);
                    }
                    if (m.Similarity >= 0.88f)
                    {
                        target.Save(@"Matches\testscreen" + DateTime.Now.Ticks + ".jpg");
                    }
                    else
                    {
                        target.Save(@"Samples\testscreen" + DateTime.Now.Ticks + ".jpg");
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Ejemplo n.º 3
0
        public void DrawRedRectangles(MainForm obj, TemplateMatch matches, Bitmap currentImage, string markerId)
        {
            int matchX     = matches.Rectangle.X;
            int matchY     = matches.Rectangle.Y;
            int rectWidth  = matches.Rectangle.Width;
            int rectHeight = matches.Rectangle.Height;


            obj.Invoke((MethodInvoker) delegate
            {
                MainForm.dataTable.Rows.Add(markerId);
            });

            Pen   redPen = new Pen(Color.Red);
            Brush brush  = new SolidBrush(Color.Yellow);
            Font  font   = new Font("Times New Roman", 16, FontStyle.Bold);

            redPen.Width = 5;

            using (Graphics graphic = Graphics.FromImage(currentImage))
            {
                graphic.DrawRectangle(redPen, new Rectangle(matchX, matchY, rectWidth, rectHeight));
                graphic.DrawString($"{markerId}", font, brush, matchX - 10, matchY - 10);
            }
            MemoryStream memoryStream = new MemoryStream();

            currentImage.Save(memoryStream, ImageFormat.Png);
            obj.pictureBox1.BeginInvoke((MethodInvoker) delegate { obj.pictureBox1.Image = currentImage; });
        }
Ejemplo n.º 4
0
        private void CompareBtn_Click(object sender, EventArgs e)
        {
            ShowStatus("Processing...");

            Bitmap largeImage = (Bitmap)Bitmap.FromFile(largeFileName);
            Bitmap smallImage = (Bitmap)Bitmap.FromFile(smallFileName);

            // create template matching algorithm's instance
            // (set similarity threshold to 90%)
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.9f);

            // find all matchings with specified above similarity
            TemplateMatch[] matches = tm.ProcessImage(largeImage, smallImage);

            if (matches.Length > 0)
            {
                TemplateMatch match = matches[0];
                ShowStatus("Match found at X: " +
                           match.Rectangle.Location.X + "; Y:" +
                           match.Rectangle.Location.Y);
            }
            else
            {
                ShowStatus("Match not found");
            }
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public async Task RouteAsync(RouteContext context)
        {
            foreach (var tree in _trees)
            {
                var tokenizer  = new PathTokenizer(context.HttpContext.Request.Path);
                var enumerator = tokenizer.GetEnumerator();
                var root       = tree.Root;

                var treeEnumerator = new TreeEnumerator(root, tokenizer);

                while (treeEnumerator.MoveNext())
                {
                    var node = treeEnumerator.Current;
                    foreach (var item in node.Matches)
                    {
                        var values = item.TemplateMatcher.Match(context.HttpContext.Request.Path);
                        if (values == null)
                        {
                            continue;
                        }

                        var match    = new TemplateMatch(item, values);
                        var snapshot = context.RouteData.PushState(match.Entry.Target, match.Values, dataTokens: null);

                        try
                        {
                            if (!RouteConstraintMatcher.Match(
                                    match.Entry.Constraints,
                                    context.RouteData.Values,
                                    context.HttpContext,
                                    this,
                                    RouteDirection.IncomingRequest,
                                    _constraintLogger))
                            {
                                continue;
                            }

                            _logger.MatchedRoute(match.Entry.RouteName, match.Entry.RouteTemplate.TemplateText);

                            await match.Entry.Target.RouteAsync(context);

                            if (context.Handler != null)
                            {
                                return;
                            }
                        }
                        finally
                        {
                            if (context.Handler == null)
                            {
                                // Restore the original values to prevent polluting the route data.
                                snapshot.Restore();
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        private void track(UnmanagedImage frame)
        {
            if (searchWindow.Width < template.Width)
            {
                searchWindow.Width = template.Width;
            }
            if (searchWindow.Height < template.Height)
            {
                searchWindow.Height = template.Height;
            }

            searchWindow.Intersect(new Rectangle(0, 0, frame.Width, frame.Height));

            if (searchWindow.Width < template.Width ||
                searchWindow.Height < template.Height)
            {
                searchWindow.Inflate((int)(0.2f * searchWindow.Width),
                                     (int)(0.2f * searchWindow.Height));
                return;
            }

            // compare two images
            TemplateMatch obj = match(frame);

            // get object information
            int width  = obj.Rectangle.Width;
            int height = obj.Rectangle.Height;

            // update object position
            updateObject(obj.Rectangle);


            // check similarity level
            if (!checkSteadiness(obj))
            {
                Reset(); return;
            }

            if (obj.Similarity >= 0.99)
            {
                registerTemplate(frame, obj.Rectangle);
            }


            // Compute a new window size
            searchWindow = obj.Rectangle;

            if (obj.Similarity < 0.98)
            {
                searchWindow.Inflate((int)(0.2f * width), (int)(0.2f * height));
            }

            else
            {
                searchWindow.Inflate((int)(0.1f * width), (int)(0.1f * height));
            }
        }
Ejemplo n.º 7
0
        private double[] GetCoords(TemplateMatchOptions options, TemplateMatch match, int templateWidth, int templateHeight, int screenx, int screeny)
        {
            var reduction = options.Reduction / 100f;
            var matchx    = match.Rectangle.Location.X / reduction;
            var matchy    = match.Rectangle.Location.Y / reduction;

            if (options.SplitScreenPanel != Panel.All)
            {
                switch (options.SplitScreenPanel)
                {
                case (Panel.Top):
                    break;

                case (Panel.Bottom):
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Left):
                    break;

                case (Panel.Right):
                    matchx = matchx + (screenx / 2);
                    break;

                case (Panel.Q1):
                    break;

                case (Panel.Q2):
                    matchx = matchx + (screenx / 2);
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Q3):
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Q4):
                    matchx = matchx + (screenx / 2);
                    matchy = matchy + (screeny / 2);
                    break;

                case (Panel.Custom):
                    matchx = matchx + (options.CustomX);
                    matchy = matchy + (options.CustomY);
                    break;
                }
            }
            var centerx = matchx + ((templateWidth / reduction) / 2);
            var centery = matchy + ((templateHeight / reduction) / 2);

            return(new double[] { centerx, centery });
        }
Ejemplo n.º 8
0
        public static void SaveFoundArea(UnmanagedImage image, TemplateMatch match)
        {
            var img = image.ToManagedImage();

            if (img.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                return;
            }

            using (var graphics = Graphics.FromImage(img))
            {
                using (var myPen = new System.Drawing.Pen(System.Drawing.Color.Red, 2))
                {
                    graphics.DrawRectangle(myPen, match.Rectangle);
                }
                img.Save($"pos_{match.Similarity}.bmp");
            }
        }
Ejemplo n.º 9
0
        public static RECT GetMatchingImageLocation(Bitmap sourceImage, AttributeType type)
        {
            Bitmap template = null;
            Bitmap filtered = null;

            try
            {
                switch (type)
                {
                case AttributeType.HP:
                    filtered = FilterForRedColor(sourceImage);
                    template = (Bitmap)System.Drawing.Image.FromFile(TemplateNames.lifePot);
                    break;

                case AttributeType.MP:
                    filtered = FilterForBlueColor(sourceImage);
                    template = (Bitmap)System.Drawing.Image.FromFile(TemplateNames.manaPot);
                    break;

                default:
                    filtered = FilterForYellowGreenColor(sourceImage);
                    template = (Bitmap)System.Drawing.Image.FromFile(TemplateNames.stmPot);
                    break;
                }

                TemplateMatch matching = TryExhaustiveTemplateMatchingWithExactOneResult(template, filtered);
                if (matching == null)
                {
                    return(new RECT());
                }

                return(matching.Rectangle);
            }
            catch
            {
                return(new RECT());
            }
            finally
            {
                template.Dispose();
                filtered.Dispose();
            }
        }
Ejemplo n.º 10
0
        private bool checkSteadiness(TemplateMatch match)
        {
            if (match.Similarity < threshold)
            {
                if (--steady < -10)
                {
                    steady = -10;
                }
            }
            else
            {
                if (++steady > 0)
                {
                    steady = 0;
                }
            }

            return(steady != -10);
        }
Ejemplo n.º 11
0
        private TemplateMatch match(UnmanagedImage frame)
        {
            TemplateMatch[] matchings = matcher.ProcessImage(frame, template, searchWindow);

            // Select highest match
            TemplateMatch match = matchings[0];
            double        max   = match.Similarity;

            for (int i = 1; i < matchings.Length; i++)
            {
                if (matchings[i].Similarity > max)
                {
                    match = matchings[i];
                    max   = match.Similarity;
                }
            }

            return(match);
        }
Ejemplo n.º 12
0
        private void btnCompare_Click(object sender, EventArgs e)
        {
            ShowStatus("Processing...");
            Bitmap largeImage = (Bitmap)System.Drawing.Image.FromFile(largeFileName);
            Bitmap smallImage = (Bitmap)System.Drawing.Image.FromFile(smallFileName);

            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.9f);

            TemplateMatch[] match = tm.ProcessImage(largeImage, smallImage);

            if (match.Length > 0)
            {
                TemplateMatch matches = match[0];
                ShowStatus("Match found at X:" + match[0].Rectangle.Location.X + "and Y:" + match[0].Rectangle.Location.Y);
            }
            else
            {
                ShowStatus("Match not found");
            }
        }
Ejemplo n.º 13
0
        public void Procesar()
        {
            System.Drawing.Bitmap sourceImage = this.ConvertirParaProcesar((Bitmap)Bitmap.FromFile(@"C:\Users\clatour\Documents\Visual Studio 2015\Projects\img\img\PantallaCompleta.bmp"));
            System.Drawing.Bitmap template    = this.ConvertirParaProcesar((Bitmap)Bitmap.FromFile(@"C:\Users\clatour\Documents\Visual Studio 2015\Projects\img\img\Fragmento.bmp"));

            // create template matching algorithm's instance
            // (set similarity threshold to 92.5%)

            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.99f);

            // find all matchings with specified above similarity
            TemplateMatch[] matchings = null;

            while (matchings == null || matchings.Count() == 0)
            {
                tm.SimilarityThreshold -= 0.01f;
                matchings = tm.ProcessImage(sourceImage, template);
                if (matchings.Count() > 0 || tm.SimilarityThreshold < 0.8f)
                {
                    break;
                }
            }

            TemplateMatch mejorCoincidencia = matchings.ToList().First(x => matchings.ToList().Max(m => m.Similarity) == x.Similarity);

            Bitmap   res = (Bitmap)Bitmap.FromFile(@"C:\Users\clatour\Documents\Visual Studio 2015\Projects\img\img\PantallaCompleta.bmp");
            Graphics g   = Graphics.FromImage(res);
            Pen      pen = new Pen(Color.White, 2);

            foreach (var item in matchings)
            {
                g.DrawRectangle(pen, item.Rectangle);
            }

            g.Flush();
            res.Save("resultado.bmp");
            res.Dispose();
            sourceImage.Dispose();
            template.Dispose();
            g.Dispose();
        }
Ejemplo n.º 14
0
        private void CompareBtn_Click(object sender, EventArgs e)
        {
            ShowStatus("Working...");

            Bitmap largeImage = (Bitmap)Bitmap.FromFile(original);
            Bitmap smallImage = (Bitmap)Bitmap.FromFile(scaled);

            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.9f);

            TemplateMatch[] matches = tm.ProcessImage(largeImage, smallImage);

            if (matches.Length > 0)
            {
                TemplateMatch match = matches[0];
                ShowStatus("Match found at X: " +
                           match.Rectangle.Location.X + "; Y:" +
                           match.Rectangle.Location.Y);
            }
            else
            {
                ShowStatus("Match not found");
            }
        }
Ejemplo n.º 15
0
        private void btnCompare_Click(object sender, EventArgs e)
        {
            Bitmap largeImage = new Bitmap(largeFileName);
            Bitmap smallImage = new Bitmap(smallFileName);

            string msg = "";
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0.9f);

            TemplateMatch[] matches = tm.ProcessImage(largeImage, smallImage);

            if (matches.Length > 0)
            {
                TemplateMatch match = matches[0];
                msg = "Match found at X:" + match.Rectangle.Location.X + " ; Match found at Y :" + match.Rectangle.Location.Y + ";";
            }
            else
            {
                msg = "small Image is not part of Large Image";
            }



            outputMessage.Text = msg;
        }
        /// <summary>
        /// Retrieves the permission scopes for a given request url.
        /// </summary>
        /// <param name="requestUrl">The target request url whose scopes are to be retrieved.</param>
        /// <param name="method">The target http verb of the request url whose scopes are to be retrieved.</param>
        /// <param name="scopeType">The type of scope to be retrieved for the target request url.</param>
        /// <returns>A list of scopes for the target request url given a http verb and type of scope.</returns>
        public List <ScopeInformation> GetScopes(string requestUrl, string method = "GET", string scopeType = "DelegatedWork")
        {
            if (!_scopesListTable.Any())
            {
                throw new InvalidOperationException($"The permissions and scopes data sources are empty; " +
                                                    $"check the source file or check whether the file path is properly set. File path: {_permissionsFilePaths}");
            }
            if (string.IsNullOrEmpty(requestUrl))
            {
                throw new ArgumentNullException(nameof(requestUrl), "The request url cannot be null or empty.");
            }

            try
            {
                requestUrl = Regex.Replace(requestUrl, @"\?.*", string.Empty);    // remove any query params
                requestUrl = Regex.Replace(requestUrl, @"\(.*?\)", string.Empty); // remove any '(...)' resource modifiers

                // Check if requestUrl is contained in our Url Template table
                TemplateMatch resultMatch = _urlTemplateTable.Match(new Uri(requestUrl.ToLower(), UriKind.RelativeOrAbsolute));

                if (resultMatch == null)
                {
                    return(null);
                }

                JArray resultValue = (JArray)_scopesListTable[int.Parse(resultMatch.Key)];

                string[] scopes = resultValue.FirstOrDefault(x => x.Value <string>("HttpVerb") == method)?
                                  .SelectToken(scopeType)?
                                  .Select(s => (string)s)
                                  .ToArray();

                List <ScopeInformation> scopesList = null;

                if (scopes != null)
                {
                    scopesList = new List <ScopeInformation>();

                    foreach (string scopeName in scopes)
                    {
                        ScopeInformation scopeInfo = null;
                        if (scopeType.Contains("Delegated"))
                        {
                            if (_delegatedScopesInfoTable.ContainsKey(scopeName))
                            {
                                scopeInfo = _delegatedScopesInfoTable[scopeName];
                            }
                        }
                        else // Application scopes
                        {
                            if (_applicationScopesInfoTable.ContainsKey(scopeName))
                            {
                                scopeInfo = _applicationScopesInfoTable[scopeName];
                            }
                        }

                        if (scopeInfo == null)
                        {
                            scopesList.Add(new ScopeInformation
                            {
                                ScopeName = scopeName
                            });
                        }
                        else
                        {
                            scopesList.Add(scopeInfo);
                        }
                    }
                }

                return(scopesList ?? null);
            }
            catch (ArgumentException)
            {
                return(null); // equivalent to no match for the given requestUrl
            }
        }
Ejemplo n.º 17
0
        public static bool AnalyzeImage(System.Drawing.Bitmap inputBitmap)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            if (inputBitmap.Height < blocksize || inputBitmap.Width < blocksize)
            {
                return(false);
            }
            movementBitmap = BuildMovementBitmap();

            //Clone movement image to 24-bit small Image - Workaround to fix PixelFormat
            System.Drawing.Bitmap movementPattern = new System.Drawing.Bitmap(movementBitmap.Width, movementBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            using (Graphics gr = Graphics.FromImage(movementPattern))
            {
                gr.DrawImage(movementBitmap, new Rectangle(0, 0, movementPattern.Width, movementPattern.Height));
            }

            bool IsThereMovement = false;

            Aurigma.GraphicsMill.Bitmap analyzeBitmap = new Aurigma.GraphicsMill.Bitmap(inputBitmap);

            //Get an area of interest
            analyzeBitmap = FindAreaOfInterest(analyzeBitmap);

            //Process area of interest bitmap into B&W we can analyze
            analyzeBitmap = GreyscaleAndThreshold(analyzeBitmap);

            //Have to turn it back into 24-bit or the pattern matching won't work
            analyzeBitmap.ColorManagement.Convert(Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

            //...Aaaaaaand back to a system Bitmap
            System.Drawing.Bitmap matchBitmap = analyzeBitmap.ToGdiPlusBitmap();

            // create template matching algorithm's instance
            // (We set similarity threshold at top of code to 90%)
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(THRESHOLD);

            // find all matchings with specified above similarity
            TemplateMatch[] matches = tm.ProcessImage(matchBitmap, movementPattern);

            //Find any matches?
            if (matches.Length > 0)
            {
                TemplateMatch match = matches[0];
                IsThereMovement = true;
            }

            movementBitmap.Dispose();
            analyzeBitmap.Dispose();
            matchBitmap.Dispose();
            movementPattern.Dispose();

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            MessageBox.Show("Processed in " + ts.TotalSeconds + " seconds.");

            return(IsThereMovement);
        }
Ejemplo n.º 18
0
 protected IList <DeclaredType> types_of(TemplateMatch match)
 {
     return(match.Data.Types);
 }
Ejemplo n.º 19
0
 protected IList <Namespace> namespaces_of(TemplateMatch match)
 {
     return(match.Data.Namespaces);
 }
Ejemplo n.º 20
0
 protected DeclaredType type_of(TemplateMatch match)
 {
     return(match.Data.Type);
 }
Ejemplo n.º 21
0
 public bool Match(string url, QueryString queryString)
 {
     return(TemplateMatch.Execut(url, queryString));
 }
Ejemplo n.º 22
0
 protected Namespace namespace_of(TemplateMatch match)
 {
     return(match.Data.Namespace);
 }
Ejemplo n.º 23
0
        public override bool Track(List <AbstractTrackPoint> _previousPoints, Bitmap _CurrentImage, long _t, out AbstractTrackPoint _currentPoint)
        {
            //---------------------------------------------------------------------
            // The input informations we have at hand are:
            // - The current bitmap we have to find the point into.
            // - The coordinates of all the previous points tracked.
            // - Previous tracking infos, stored in the TrackPoints tracked so far.
            //---------------------------------------------------------------------

            TrackPointBlock lastTrackPoint = (TrackPointBlock)_previousPoints[_previousPoints.Count - 1];
            Point           lastPoint      = lastTrackPoint.ToPoint();

            // Compute the projected point.
            // (coordinate of the point that would keep following the same motion as in last step).
            Point forecast;

            if (_previousPoints.Count > 1)
            {
                Point penultimate = _previousPoints[_previousPoints.Count - 2].ToPoint();

                int dx = lastPoint.X - penultimate.X;
                int dy = lastPoint.Y - penultimate.Y;

                forecast = new Point(lastPoint.X + dx, lastPoint.Y + dy);
            }
            else
            {
                forecast = _previousPoints[0].ToPoint();
            }

            bool bMatched = false;

            _currentPoint = null;

            if (lastTrackPoint.Template != null && _CurrentImage != null)
            {
                // Center search zone around last point.
                Point     searchCenter = lastPoint;
                Rectangle searchZone   = new Rectangle(searchCenter.X - (m_SearchWindowSize.Width / 2),
                                                       searchCenter.Y - (m_SearchWindowSize.Height / 2),
                                                       m_SearchWindowSize.Width,
                                                       m_SearchWindowSize.Height);

                // Convert to grayscale prior to match, if necessary.
                Bitmap workingImage = m_bWorkOnGrayscale ? Grayscale.CommonAlgorithms.BT709.Apply(_CurrentImage) : _CurrentImage;

                double fBestScore    = 0;
                Point  bestCandidate = new Point(-1, -1);

                if (m_UpdateStrategy == UpdateStrategy.Both)
                {
                    // Try to match the initial reference block in the image first.
                    // If it gets a score over a given threshold, we give it the priority over the I-1 block.
                    // This is an attempt at correcting the drift issue.

                    // Find the last reference block. (last block manually choosen by user.)
                    int iLastReferenceBlock = 0;
                    for (int b = _previousPoints.Count - 1; b >= 0; b--)
                    {
                        if (((TrackPointBlock)_previousPoints[b]).IsReferenceBlock)
                        {
                            iLastReferenceBlock = b;
                            break;
                        }
                    }

                    Bitmap originalTemplate        = ((TrackPointBlock)_previousPoints[iLastReferenceBlock]).Template;
                    Bitmap workingOriginalTemplate = m_bWorkOnGrayscale ? Grayscale.CommonAlgorithms.BT709.Apply(originalTemplate) : originalTemplate;

                    ITemplateMatching originalMatcher;
                    if (m_bCorrelationMatching)
                    {
                        originalMatcher = new CorrelationTemplateMatching(m_fOriginalSimilarityThreshold);
                    }
                    else
                    {
                        originalMatcher = new ExhaustiveTemplateMatching(m_fOriginalSimilarityThreshold);
                    }

                    TemplateMatch[] matchingsOriginal = originalMatcher.ProcessImage(workingImage, workingOriginalTemplate, searchZone);

                    if (matchingsOriginal.Length > 0)
                    {
                        // We found a block with a very good similarity to the original block selected by the user.
                        // It will take precedence over the I-1 block.
                        TemplateMatch tm = matchingsOriginal[0];
                        bestCandidate = new Point(tm.Rectangle.Left + (tm.Rectangle.Width / 2), tm.Rectangle.Top + (tm.Rectangle.Height / 2));
                        fBestScore    = tm.Similarity;

                        if (m_bMonitoring)
                        {
                            log.Debug(String.Format("Original template found with good similarity ({0:0.000}), {1} candidates.", tm.Similarity, matchingsOriginal.Length));
                        }
                    }
                    else
                    {
                        log.Debug(String.Format("Original template not found"));
                    }
                }

                if (bestCandidate.X == -1 || bestCandidate.Y == 1)
                {
                    Bitmap workingTemplate = m_bWorkOnGrayscale ? Grayscale.CommonAlgorithms.BT709.Apply(lastTrackPoint.Template) : lastTrackPoint.Template;

                    ITemplateMatching templateMatcher;
                    if (m_bCorrelationMatching)
                    {
                        templateMatcher = new CorrelationTemplateMatching(m_fSimilarityTreshold);
                    }
                    else
                    {
                        templateMatcher = new ExhaustiveTemplateMatching(m_fSimilarityTreshold);
                    }

                    TemplateMatch[] matchings = templateMatcher.ProcessImage(workingImage, workingTemplate, searchZone);

                    if (matchings.Length > 0)
                    {
                        // Find the best candidate.
                        // Score is weighted average of : similarity and closeness to forecast.
                        int    iBestCandidate  = -1;
                        double fWinnerDistance = 0;
                        for (int i = 0; i < matchings.Length; i++)
                        {
                            TemplateMatch tm = matchings[i];
                            //if(_previousPoints.Count > 1)
                            {
                                Point  candidatePoint      = new Point(tm.Rectangle.Left + (tm.Rectangle.Width / 2), tm.Rectangle.Top + (tm.Rectangle.Height / 2));
                                double fDistanceToForecast = CalibrationHelper.PixelDistance(candidatePoint, forecast);
                                double fScore = GetScore(tm.Similarity, fDistanceToForecast, m_fMaxDistance);

                                if (fScore > fBestScore)
                                {
                                    fBestScore      = fScore;
                                    fWinnerDistance = fDistanceToForecast;
                                    iBestCandidate  = i;
                                    bestCandidate   = candidatePoint;
                                }
                            }
                        }
                        if (m_bMonitoring)
                        {
                            log.Debug(String.Format("Last template found with : Score:{0:0.000}, Similarity:{1:0.000} (index:{2:00}/{3:00}), Distance to forecast (px):{4:0.00}",
                                                    fBestScore,
                                                    matchings[iBestCandidate].Similarity,
                                                    iBestCandidate,
                                                    matchings.Length,
                                                    fWinnerDistance));
                        }
                    }
                    else
                    {
                        log.Debug(String.Format("Last template not found, or score too low."));
                    }
                }

                // Result of the matching.
                if (bestCandidate.X != -1 && bestCandidate.Y != -1)
                {
                    // Save template in the point.
                    _currentPoint = CreateTrackPoint(false, bestCandidate.X, bestCandidate.Y, fBestScore, _t, _CurrentImage, _previousPoints);
                    ((TrackPointBlock)_currentPoint).Similarity = fBestScore;

                    // Finally, it is only considered a match if the score is over the threshold.
                    if (fBestScore >= m_fScoreTreshold || _previousPoints.Count == 1)
                    {
                        bMatched = true;
                    }
                }
                else
                {
                    // No match. Create the point at the center of the search window (whatever that might be).
                    _currentPoint = CreateTrackPoint(false, searchCenter.X, searchCenter.Y, fBestScore, _t, _CurrentImage, _previousPoints);
                    log.Debug("Track failed. No block over the similarity treshold in the search window.");
                }

                #region Monitoring
                if (m_bMonitoring)
                {
                    // Save current template to file, to visually monitor the drift.
                    string tplDirectory = @"C:\Documents and Settings\Administrateur\Mes documents\Dev  Prog\Videa\Video Testing\Tracking\Template Update";
                    if (_previousPoints.Count == 1)
                    {
                        // Clean up folder.
                        string[] tplFiles = Directory.GetFiles(tplDirectory, "*.bmp");
                        foreach (string f in tplFiles)
                        {
                            File.Delete(f);
                        }
                    }
                    String iFileName = String.Format("{0}\\tpl-{1:000}.bmp", tplDirectory, _previousPoints.Count);
                    ((TrackPointBlock)_currentPoint).Template.Save(iFileName);
                }
                #endregion
            }
            else
            {
                // No image. (error case ?)
                // Create the point at the last point location.
                _currentPoint = CreateTrackPoint(false, lastTrackPoint.X, lastTrackPoint.Y, 0.0f, _t, _CurrentImage, _previousPoints);
                log.Debug("Track failed. No input image, or last point doesn't have any cached block image.");
            }

            return(bMatched);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Process image looking for matchings with specified template.
        /// </summary>
        /// 
        /// <param name="image">Unmanaged source image to process.</param>
        /// <param name="template">Unmanaged template image to search for.</param>
        /// <param name="searchZone">Rectangle in source image to search template for.</param>
        /// 
        /// <returns>Returns array of found template matches. The array is sorted by similarity
        /// of found matches in descending order.</returns>
        /// 
        /// <exception cref="UnsupportedImageFormatException">The source image has incorrect pixel format.</exception>
        /// <exception cref="InvalidImagePropertiesException">Template image is bigger than search zone.</exception>
        ///
        public TemplateMatch[] ProcessImage( UnmanagedImage image, UnmanagedImage template, Rectangle searchZone )
        {
            // check image format
            if (
                ( ( image.PixelFormat != PixelFormat.Format8bppIndexed ) &&
                  ( image.PixelFormat != PixelFormat.Format24bppRgb ) ) ||
                ( image.PixelFormat != template.PixelFormat ) )
            {
                throw new UnsupportedImageFormatException( "Unsupported pixel format of the source or template image." );
            }

            // clip search zone
            Rectangle zone = searchZone;
            zone.Intersect( new Rectangle( 0, 0, image.Width, image.Height ) );

            // search zone's starting point
            int startX = zone.X;
            int startY = zone.Y;

            // get source and template image size
            int sourceWidth    = zone.Width;
            int sourceHeight   = zone.Height;
            int templateWidth  = template.Width;
            int templateHeight = template.Height;

            // check template's size
            if ( ( templateWidth > sourceWidth ) || ( templateHeight > sourceHeight ) )
            {
                throw new InvalidImagePropertiesException( "Template's size should be smaller or equal to search zone." );
            }

            int pixelSize = ( image.PixelFormat == PixelFormat.Format8bppIndexed ) ? 1 : 3;
            int sourceStride = image.Stride;

            // similarity map. its size is increased by 4 from each side to increase
            // performance of non-maximum suppresion
            int mapWidth  = sourceWidth - templateWidth + 1;
            int mapHeight = sourceHeight - templateHeight + 1;
            int[,] map = new int[mapHeight + 4, mapWidth + 4];

            // maximum possible difference with template
            int maxDiff = templateWidth * templateHeight * pixelSize * 255;

            // integer similarity threshold
            int threshold = (int) ( similarityThreshold * maxDiff );

            // width of template in bytes
            int templateWidthInBytes = templateWidth * pixelSize;

            // do the job
            unsafe
            {
                byte* baseSrc = (byte*) image.ImageData.ToPointer( );
                byte* baseTpl = (byte*) template.ImageData.ToPointer( );

                int sourceOffset = image.Stride - templateWidth * pixelSize;
                int templateOffset = template.Stride - templateWidth * pixelSize;

                // for each row of the source image
                for ( int y = 0; y < mapHeight; y++ )
                {
                    // for each pixel of the source image
                    for ( int x = 0; x < mapWidth; x++ )
                    {
                        byte* src = baseSrc + sourceStride * ( y + startY ) + pixelSize * ( x + startX );
                        byte* tpl = baseTpl;

                        // compare template with source image starting from current X,Y
                        int dif = 0;

                        // for each row of the template
                        for ( int i = 0; i < templateHeight; i++ )
                        {
                            // for each pixel of the template
                            for ( int j = 0; j < templateWidthInBytes; j++, src++, tpl++ )
                            {
                                int d = *src - *tpl;
                                if ( d > 0 )
                                {
                                    dif += d;
                                }
                                else
                                {
                                    dif -= d;
                                }
                            }
                            src += sourceOffset;
                            tpl += templateOffset;
                        }

                        // templates similarity
                        int sim = maxDiff - dif;

                        if ( sim >= threshold )
                            map[y + 2, x + 2] = sim;
                    }
                }
            }

            // collect interesting points - only those points, which are local maximums
            List<TemplateMatch> matchingsList = new List<TemplateMatch>( );

            // for each row
            for ( int y = 2, maxY = mapHeight + 2; y < maxY; y++ )
            {
                // for each pixel
                for ( int x = 2, maxX = mapWidth + 2; x < maxX; x++ )
                {
                    int currentValue = map[y, x];

                    // for each windows' row
                    for ( int i = -2; ( currentValue != 0 ) && ( i <= 2 ); i++ )
                    {
                        // for each windows' pixel
                        for ( int j = -2; j <= 2; j++ )
                        {
                            if ( map[y + i, x + j] > currentValue )
                            {
                                currentValue = 0;
                                break;
                            }
                        }
                    }

                    // check if this point is really interesting
                    if ( currentValue != 0 )
                    {
                        matchingsList.Add( new TemplateMatch(
                            new Rectangle( x - 2 + startX, y - 2 + startY, templateWidth, templateHeight ),
                            (float) currentValue / maxDiff ) );
                    }
                }
            }

            // convert list to array
            TemplateMatch[] matchings = new TemplateMatch[matchingsList.Count];
            matchingsList.CopyTo( matchings );
            // sort in descending order
            Array.Sort( matchings, new MatchingsSorter( ) );

            return matchings;
        }
        /// <summary>
        /// Retrieves permissions scopes.
        /// </summary>
        /// <param name="scopeType">The type of scope to be retrieved for the target request url.</param>
        /// <param name="locale">The language code for the preferred localized file.</param>
        /// <param name="requestUrl">Optional: The target request url whose scopes are to be retrieved.</param>
        /// <param name="method">Optional: The target http verb of the request url whose scopes are to be retrieved.</param>
        /// <param name="org">Optional: The name of the org/owner of the repo.</param>
        /// <param name="branchName">Optional: The name of the branch containing the files.</param>
        /// <returns>A list of scopes for the target request url given a http verb and type of scope.</returns>
        public async Task <List <ScopeInformation> > GetScopesAsync(string scopeType  = "DelegatedWork",
                                                                    string locale     = DefaultLocale,
                                                                    string requestUrl = null,
                                                                    string method     = null,
                                                                    string org        = null,
                                                                    string branchName = null)
        {
            try
            {
                InitializePermissions();

                IDictionary <string, IDictionary <string, ScopeInformation> > scopesInformationDictionary;

                if (!string.IsNullOrEmpty(org) && !string.IsNullOrEmpty(branchName))
                {
                    // Creates a dict of scopes information from GitHub files
                    scopesInformationDictionary = await GetPermissionsDescriptionsFromGithub(org, branchName, locale);
                }
                else
                {
                    // Creates a dict of scopes information from cached files
                    scopesInformationDictionary = await GetOrCreatePermissionsDescriptionsAsync(locale);
                }

                if (string.IsNullOrEmpty(requestUrl))  // fetch all permissions
                {
                    List <ScopeInformation> scopesListInfo = new List <ScopeInformation>();

                    if (scopeType.Contains(Delegated))
                    {
                        if (scopesInformationDictionary.ContainsKey(Delegated))
                        {
                            foreach (var scopesInfo in scopesInformationDictionary[Delegated])
                            {
                                scopesListInfo.Add(scopesInfo.Value);
                            }
                        }
                    }
                    else // Application scopes
                    {
                        if (scopesInformationDictionary.ContainsKey(Application))
                        {
                            foreach (var scopesInfo in scopesInformationDictionary[Application])
                            {
                                scopesListInfo.Add(scopesInfo.Value);
                            }
                        }
                    }

                    return(scopesListInfo);
                }
                else // fetch permissions for a given request url and method
                {
                    if (string.IsNullOrEmpty(method))
                    {
                        throw new ArgumentNullException(nameof(method), "The HTTP method value cannot be null or empty.");
                    }

                    requestUrl = Regex.Replace(requestUrl, @"\?.*", string.Empty);    // remove any query params
                    requestUrl = Regex.Replace(requestUrl, @"\(.*?\)", string.Empty); // remove any '(...)' resource modifiers

                    // Check if requestUrl is contained in our Url Template table
                    TemplateMatch resultMatch = _urlTemplateMatcher.Match(new Uri(requestUrl.ToLowerInvariant(), UriKind.RelativeOrAbsolute));

                    if (resultMatch == null)
                    {
                        return(null);
                    }

                    JArray resultValue = new JArray();
                    resultValue = (JArray)_scopesListTable[int.Parse(resultMatch.Key)];

                    var scopes = resultValue.FirstOrDefault(x => x.Value <string>("HttpVerb") == method)?
                                 .SelectToken(scopeType)?
                                 .Select(s => (string)s)
                                 .ToArray();

                    if (scopes == null)
                    {
                        return(null);
                    }

                    List <ScopeInformation> scopesList = new List <ScopeInformation>();

                    foreach (string scopeName in scopes)
                    {
                        ScopeInformation scopeInfo = null;

                        if (scopeType.Contains(Delegated))
                        {
                            if (scopesInformationDictionary[Delegated].ContainsKey(scopeName))
                            {
                                scopeInfo = scopesInformationDictionary[Delegated][scopeName];
                            }
                        }
                        else // Application scopes
                        {
                            if (scopesInformationDictionary[Application].ContainsKey(scopeName))
                            {
                                scopeInfo = scopesInformationDictionary[Application][scopeName];
                            }
                        }
                        if (scopeInfo == null)
                        {
                            scopesList.Add(new ScopeInformation
                            {
                                ScopeName = scopeName
                            });
                        }
                        else
                        {
                            scopesList.Add(scopeInfo);
                        }
                    }

                    return(scopesList);
                }
            }
            catch (ArgumentNullException exception)
            {
                throw exception;
            }
            catch (ArgumentException)
            {
                return(null); // equivalent to no match for the given requestUrl
            }
        }
        /// <summary>
        /// Create predicate function based on passed query parameters
        /// </summary>
        /// <param name="operationIds">Comma delimited list of operationIds or * for all operations.</param>
        /// <param name="tags">Comma delimited list of tags or a single regex.</param>
        /// <param name="url">Url path to match with Operation Ids.</param>
        /// <param name="graphVersion">Version of Microsoft Graph.</param>
        /// <param name="forceRefresh">Don't read from in-memory cache.</param>
        /// <returns>A predicate</returns>
        public static async Task <Func <OpenApiOperation, bool> > CreatePredicate(string operationIds, string tags, string url,
                                                                                  string graphVersion, bool forceRefresh)
        {
            if (operationIds != null && tags != null)
            {
                return(null); // Cannot filter by operationIds and tags at the same time
            }
            if (url != null && (operationIds != null || tags != null))
            {
                return(null); // Cannot filter by url and either 0perationIds and tags at the same time
            }

            Func <OpenApiOperation, bool> predicate;

            if (operationIds != null)
            {
                if (operationIds == "*")
                {
                    predicate = (o) => true;  // All operations
                }
                else
                {
                    var operationIdsArray = operationIds.Split(',');
                    predicate = (o) => operationIdsArray.Contains(o.OperationId);
                }
            }
            else if (tags != null)
            {
                var tagsArray = tags.Split(',');
                if (tagsArray.Length == 1)
                {
                    var regex = new Regex(tagsArray[0]);

                    predicate = (o) => o.Tags.Any(t => regex.IsMatch(t.Name));
                }
                else
                {
                    predicate = (o) => o.Tags.Any(t => tagsArray.Contains(t.Name));
                }
            }
            else if (url != null)
            {
                /* Extract the respective Operation Id(s) that match the provided url path */

                if (!_openApiOperationsTable.Any() || forceRefresh)
                {
                    _uriTemplateTable       = new UriTemplateTable();
                    _openApiOperationsTable = new Dictionary <int, OpenApiOperation[]>();

                    await PopulateReferenceTablesAync(graphVersion, forceRefresh);
                }

                url = url.Replace('-', '_');

                TemplateMatch resultMatch = _uriTemplateTable.Match(new Uri(url.ToLower(), UriKind.RelativeOrAbsolute));

                if (resultMatch == null)
                {
                    return(null);
                }

                /* Fetch the corresponding Operations Id(s) for the matched url */

                OpenApiOperation[] openApiOps        = _openApiOperationsTable[int.Parse(resultMatch.Key)];
                string[]           operationIdsArray = openApiOps.Select(x => x.OperationId).ToArray();

                predicate = (o) => operationIdsArray.Contains(o.OperationId);
            }
            else
            {
                predicate = null;
            }

            return(predicate);
        }
        /// <summary>
        /// Retrieves permissions scopes.
        /// </summary>
        /// <param name="scopeType">The type of scope to be retrieved for the target request url.</param>
        /// <param name="locale">The language code for the preferred localized file.</param>
        /// <param name="requestUrl">The target request url whose scopes are to be retrieved.</param>
        /// <param name="method">The target http verb of the request url whose scopes are to be retrieved.</param>
        /// <returns>A list of scopes for the target request url given a http verb and type of scope.</returns>
        public async Task <List <ScopeInformation> > GetScopesAsync(string scopeType  = "DelegatedWork",
                                                                    string locale     = DefaultLocale,
                                                                    string requestUrl = null,
                                                                    string method     = null)
        {
            try
            {
                /* Add multiple checks to ensure thread that
                 * populated scopes information successfully
                 * completed seeding.
                 */
                if (RefreshPermissionsTables() ||
                    _scopesListTable == null ||
                    !_scopesListTable.Any())
                {
                    /* Permissions tables are not localized, so no need to keep different localized cached copies.
                    *  Refresh tables only after the specified time duration has elapsed or no cached copy exists. */
                    lock (_permissionsLock)
                    {
                        /* Ensure permissions tables are seeded by only one executing thread,
                         * once per refresh cycle. */
                        if (!_permissionsRefreshed)
                        {
                            SeedPermissionsTables();
                        }
                    }
                }

                var scopesInformationDictionary = await GetOrCreatePermissionsDescriptionsAsync(locale);

                if (string.IsNullOrEmpty(requestUrl))  // fetch all permissions
                {
                    List <ScopeInformation> scopesListInfo = new List <ScopeInformation>();

                    if (scopeType.Contains(Delegated))
                    {
                        if (scopesInformationDictionary.ContainsKey(Delegated))
                        {
                            foreach (var scopesInfo in scopesInformationDictionary[Delegated])
                            {
                                scopesListInfo.Add(scopesInfo.Value);
                            }
                        }
                    }
                    else // Application scopes
                    {
                        if (scopesInformationDictionary.ContainsKey(Application))
                        {
                            foreach (var scopesInfo in scopesInformationDictionary[Application])
                            {
                                scopesListInfo.Add(scopesInfo.Value);
                            }
                        }
                    }

                    return(scopesListInfo);
                }
                else // fetch permissions for a given request url and method
                {
                    if (string.IsNullOrEmpty(method))
                    {
                        throw new ArgumentNullException(nameof(method), "The HTTP method value cannot be null or empty.");
                    }

                    requestUrl = Regex.Replace(requestUrl, @"\?.*", string.Empty);    // remove any query params
                    requestUrl = Regex.Replace(requestUrl, @"\(.*?\)", string.Empty); // remove any '(...)' resource modifiers

                    // Check if requestUrl is contained in our Url Template table
                    TemplateMatch resultMatch = _urlTemplateTable.Match(new Uri(requestUrl.ToLower(), UriKind.RelativeOrAbsolute));

                    if (resultMatch == null)
                    {
                        return(null);
                    }

                    JArray resultValue = new JArray();
                    resultValue = (JArray)_scopesListTable[int.Parse(resultMatch.Key)];

                    var scopes = resultValue.FirstOrDefault(x => x.Value <string>("HttpVerb") == method)?
                                 .SelectToken(scopeType)?
                                 .Select(s => (string)s)
                                 .ToArray();

                    if (scopes == null)
                    {
                        return(null);
                    }

                    List <ScopeInformation> scopesList = new List <ScopeInformation>();

                    foreach (string scopeName in scopes)
                    {
                        ScopeInformation scopeInfo = null;
                        if (scopeType.Contains(Delegated))
                        {
                            if (scopesInformationDictionary[Delegated].ContainsKey(scopeName))
                            {
                                scopeInfo = scopesInformationDictionary[Delegated][scopeName];
                            }
                        }
                        else // Application scopes
                        {
                            if (scopesInformationDictionary[Application].ContainsKey(scopeName))
                            {
                                scopeInfo = scopesInformationDictionary[Application][scopeName];
                            }
                        }

                        if (scopeInfo == null)
                        {
                            scopesList.Add(new ScopeInformation
                            {
                                ScopeName = scopeName
                            });
                        }
                        else
                        {
                            scopesList.Add(scopeInfo);
                        }
                    }

                    return(scopesList);
                }
            }
            catch (ArgumentNullException exception)
            {
                throw exception;
            }
            catch (ArgumentException)
            {
                return(null); // equivalent to no match for the given requestUrl
            }
        }
Ejemplo n.º 28
0
 public bool Match(string url, Dictionary <string, string> parameters)
 {
     return(TemplateMatch.Execute(url, parameters));
 }