コード例 #1
0
        /// <summary>
        /// Recognizes the given strokes. Used by ComboRecognizer.
        /// </summary>
        /// <param name="strokes">The list of strokes to recognize</param>
        /// <returns>A ranked list of possible ShapeType matches</returns>
        public List <ShapeType> Recognize(List <Substroke> strokes)
        {
            BitmapSymbol      unknown = new BitmapSymbol(strokes);
            List <SymbolRank> results = unknown.Recognize(_templates);
            List <ShapeType>  output  = new List <ShapeType>();

            foreach (SymbolRank sr in results)
            {
                output.Add(sr.SymbolType);
            }
            return(output);
        }
コード例 #2
0
        /// <summary>
        /// Uses the RecognitionInterfaces.Recognizer recognize method
        /// which recognizes and assigns the type of a shape.  This
        /// implementation allows the use of the learnFromExample
        /// method in Interface Functions.
        /// </summary>
        /// <param name="shape">The shape to recognize</param>
        /// <param name="featureSketch">The featureSketch to use</param>
        public override void recognize(Sketch.Shape shape, Featurefy.FeatureSketch featureSketch)
        {
            BitmapSymbol      unknown = new BitmapSymbol(shape.SubstrokesL);
            List <SymbolRank> results = unknown.Recognize(_templates);

            if (results.Count > 0)
            {
                // Populate the dictionary of alterateTypes with all of the ShapeTypes in results
                Dictionary <ShapeType, double> alternateTypes = new Dictionary <ShapeType, double>();

                if (debug)
                {
                    Console.WriteLine("\nRecognition results: ");
                }

                foreach (SymbolRank result in results)
                {
                    if (!alternateTypes.ContainsKey(result.SymbolType))
                    {
                        alternateTypes.Add(result.SymbolType, getProbability(result.SymbolType, results));
                    }

                    if (debug)
                    {
                        Console.WriteLine(result.SymbolType + " with template " + result.SymbolName);
                    }
                }

                ShapeType type = results[0].SymbolType;          // the most likely type

                float probability = (float)alternateTypes[type]; // grab the probability of our most likely type

                alternateTypes.Remove(type);                     // the most likely type is NOT an alternate

                shape.setRecognitionResults(
                    type,
                    probability,
                    alternateTypes,
                    results[0].BestOrientation,
                    results[0].SymbolName);
            }
        }
コード例 #3
0
        /// <summary>
        /// Recognize a shape as if it had the given type
        /// </summary>
        /// <param name="symbol"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public Dictionary <ShapeType, RecognitionInterfaces.RecognitionResult> RecognitionResults(BitmapSymbol symbol, IEnumerable <ShapeType> types)
        {
            if (_templates.Count == 0)
            {
                throw new Exception("Cannot recognize a symbol when there are no available templates");
            }

            var recResults = new Dictionary <ShapeType, RecognitionInterfaces.RecognitionResult>();

            RecoResult        allResults = symbol.Recognize(_templates);
            List <SymbolRank> results    = allResults.SortedResults(ResultType.FUSION);

            if (results.Count == 0)
            {
                throw new Exception("Image recognition failed on bitmap symbol " + symbol);
            }

            Dictionary <ShapeType, double> alternateTypes = new Dictionary <ShapeType, double>();

            // Loop through them from most-likely to least-likely.
            foreach (SymbolRank result in results)
            {
                if (!recResults.ContainsKey(result.SymbolType))
                {
                    double partialHausdorff = allResults.getSR(ResultType.PARTIAL_HAUSDORFF, result.Symbol).Distance;
                    double modHausdorff     = allResults.getSR(ResultType.MOD_HAUSDORFF, result.Symbol).Distance;
                    double yule             = allResults.getSR(ResultType.YULE, result.Symbol).Distance;
                    double tanimoto         = allResults.getSR(ResultType.TANIMOTO, result.Symbol).Distance;

                    ImageRecognitionResult r = new ImageRecognitionResult(
                        result.SymbolType,
                        partialHausdorff,
                        modHausdorff,
                        yule,
                        tanimoto,
                        getProbabilityFromTotalDistance(result),
                        result.BestOrientation,
                        alternateTypes,
                        result.SymbolName,
                        result.Symbol.toBitmap());

                    recResults.Add(result.SymbolType, r);
                }
                if (!alternateTypes.ContainsKey(result.SymbolType))
                {
                    alternateTypes.Add(result.SymbolType, getProbabilityFromTotalDistance(result));
                }
            }

            foreach (ShapeType type in types)
            {
                if (!recResults.ContainsKey(type))
                {
                    recResults.Add(type, new ImageRecognitionResult(
                                       type,
                                       0, 0, 0, 0, 0, 0, new Dictionary <ShapeType, double>(), "", null));
                }
            }

            return(recResults);
        }
コード例 #4
0
        /// <summary>
        /// Recognize a shape.
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="featureSketch"></param>
        /// <returns>An ImageRecognitionResult. If you know you are dealing with an ImageRecognizer, you can cast the returned
        /// RecognitionResult to an ImageRecognitionResult safely.</returns>
        public override RecognitionInterfaces.RecognitionResult recognize(Sketch.Shape shape, Featurefy.FeatureSketch featureSketch)
        {
            // Gaaghaghagahga
            // C# has one flaw, and I found it:
            // http://www.simple-talk.com/community/blogs/simonc/archive/2010/07/14/93495.aspx
            // In short, this method must return a generic "RecognitionResult" and cannot return
            // the better "ImageRecognitionResult," even though doing so would be perfectly
            // type-safe. =(

            BitmapSymbol      unknown    = _shapesToSymbols[shape];
            RecoResult        allResults = unknown.Recognize(_templates);
            List <SymbolRank> results    = allResults.SortedResults(ResultType.FUSION);

            if (results.Count > 0)
            {
                // Populate the dictionary of alterateTypes with all of the ShapeTypes in results
                var alternateTypes = new Dictionary <ShapeType, double>();

#if JESSI
                Console.WriteLine();
                Console.WriteLine("\nRecognition results: ");
#endif

                foreach (SymbolRank result in results)
                {
                    if (!alternateTypes.ContainsKey(result.SymbolType))
                    {
                        alternateTypes.Add(result.SymbolType, getProbabilityFromTotalDistance(result));
                    }
#if JESSI
                    if (debug)
                    {
                        Console.WriteLine(result.SymbolType + " with template " + result.SymbolName);
                    }
#endif
                }

                ShapeType type = results[0].SymbolType;          // the most likely type

                float probability = (float)alternateTypes[type]; // grab the probability of our most likely type

                alternateTypes.Remove(type);                     // the most likely type is NOT an alternate

                double confidence   = getProbabilityFromTotalDistance(results[0]);
                double orientation  = results[0].BestOrientation;
                string templateName = results[0].SymbolName;
                System.Drawing.Bitmap templateBitmap = results[0].Symbol.toBitmap();

                double partialHausdorff = allResults.getSR(ResultType.PARTIAL_HAUSDORFF, results[0].Symbol).Distance;
                double modHausdorff     = allResults.getSR(ResultType.MOD_HAUSDORFF, results[0].Symbol).Distance;
                double yule             = allResults.getSR(ResultType.YULE, results[0].Symbol).Distance;
                double tanimoto         = allResults.getSR(ResultType.TANIMOTO, results[0].Symbol).Distance;

                return(new ImageRecognitionResult(
                           type,
                           partialHausdorff,
                           modHausdorff,
                           yule,
                           tanimoto,
                           confidence,
                           orientation,
                           alternateTypes,
                           templateName,
                           templateBitmap));
            }

            throw new Exception("Image recognition failed on shape " + shape);
        }