예제 #1
0
 private void GlyphComparator_MatchFoundInternal(object sender, GlyphComparatorEventArgs e)
 {
     if (_glyphToFind != null)
     {
         _glyphToFind.CompareInfo.Matches.Add(e.MatchingInfo);
     }
     if (_glyphSearched != null)
     {
         _glyphSearched.CompareInfo.Matches.Add(e.MatchingInfo);
     }
 }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="imageToParse"></param>
        /// <param name="imageToFind"></param>
        /// <param name="maxErr"></param>
        /// <returns></returns>
        private bool FindImageAdapter(IImageAdapter imageToParse, IImageAdapter imageToFind, double maxErr)
        {
            if (imageToParse == null)
            {
                throw new ArgumentNullException("imageToParse", "variable must be set to a valid instance of an object implementing IImageAdapter (null passed in)");
            }
            if (imageToFind == null)
            {
                throw new ArgumentNullException("imageToFind", "variable must be set to a valid instance of an object implementing IImageAdapter (null passed in)");
            }

            float[] hist       = new float[MatchingInfo.HistogramLength];
            int     pixelCount = imageToFind.Height * imageToFind.Width;

            int  maxX          = (int)imageToParse.Width;
            int  maxY          = (int)imageToParse.Height;
            int  minX          = 0;
            int  minY          = 0;
            long feedbackCount = 0;
            bool found         = false;

            // Parse the whole image
            for (int y = minY; y < maxY; y++)
            {
                for (int x = minX; x < maxX; x++)
                {
                    if (y + imageToFind.Height >= maxY || x + imageToFind.Width >= maxX)
                    {
                        continue;
                    }

                    // Init histogram
                    for (int u = 0; u < MatchingInfo.HistogramLength; u++)
                    {
                        hist[u] = 0f;
                    }

                    double err   = 0;
                    int    count = 0;
                    // compare target (sub-area) and glyph (full area)
                    for (int glyphY = 0; glyphY < imageToFind.Height; glyphY++)
                    {
                        for (int glyphX = 0; glyphX < imageToFind.Width; glyphX++)
                        {
                            IColor targetColor = imageToParse[x + glyphX, y + glyphY];
                            IColor glyphColor  = imageToFind[glyphX, glyphY];

                            //

                            if (glyphColor.Alpha != 0 && targetColor.Alpha != 0)
                            {
                                float deltaColor = (float)(Math.Abs(targetColor.Red - glyphColor.Red) + Math.Abs(targetColor.Green - glyphColor.Green) + Math.Abs(targetColor.Blue - glyphColor.Blue)) / 3f;
                                err += deltaColor / pixelCount;
                                if (err > maxErr)
                                {
                                    goto ExitInternalLoop;
                                }

                                AddInc((float)deltaColor, hist);
                                count++;
                            }
                        }
                    }

                    if (err <= maxErr)
                    {
                        if (count > 0)
                        {
                            GlyphComparatorEventArgs glyphComp = new GlyphComparatorEventArgs(new MatchingInfo());
                            glyphComp.MatchingInfo.Error       = (float)err;
                            glyphComp.MatchingInfo.BoundingBox = new Rectangle(x, y, imageToFind.Width, imageToFind.Height);
                            // Normalize histogram
                            for (int i = 0; i < MatchingInfo.HistogramLength; i++)
                            {
                                glyphComp.MatchingInfo.Histogram[i] = hist[i] / count;
                            }
//                                    ProcessMatch(matchingInfo);
                            if (MatchFoundEvent != null)
                            {
                                MatchFoundEvent(this, glyphComp);
                            }
                            found = true;
                        }
                    }
                    else
                    {
                        // Log.WriteLine();
                        goto ExitExternalLoop;
                    }
ExitInternalLoop:
                    feedbackCount++;
                    if (feedbackCount % 100 == 0)
                    {
                        // Give Feedback
//                                Log.Write(".");
                    }
                }       // for x
            }           // for y
ExitExternalLoop:
            if (found == false)
            {
//                        Log.WriteLine("   < TEMPLATE NOT FOUND during the Scan>");
            }
            return(found);
        }