コード例 #1
0
ファイル: KnnCore.cs プロジェクト: aweeesome/scribex
        /// <summary>
        /// Using superimposition and then compare bool arrays.
        /// </summary>
        /// <param name="inputImage"></param>
        /// <returns></returns>
        private Classification RecognizeSuperimposedImageWithBoolArrayMethod(Image inputImage)
        {
            //If training set isn't loaded yet, load it
            if (!loadedTrainingSet)
            {
                LoadTrainingSet();
            }

            //Fill up rankTable
            foreach (TrainingSetUnit tsu in trainingSet)
            {
                RankEntry re = new RankEntry();
                Image tempInputImage = ProcrustesAnalysis.SaveTranslatedImage(inputImage);
                Image tempTsuImage = ProcrustesAnalysis.SaveTranslatedImage(tsu.image);
                bool[,] array1 = ImageHelper.MonoBitmapToBoolArray((Bitmap)tempInputImage);
                bool[,] array2 = ImageHelper.MonoBitmapToBoolArray((Bitmap)tempTsuImage);
                re.squareDistance = SquareDistanceBetweenBoolArrays(array1, array2);
                re.classification = tsu.classification;
                rankTable.Add(re);
            }

            rankTable.Sort();

            Dictionary<Classification, int> resultCountTable = new Dictionary<Classification, int>();

            for (int i = 0; i < k; i++)
            {
                try
                {
                    resultCountTable.Add(rankTable[i].classification, 0);
                }
                catch (ArgumentException)
                {
                    resultCountTable[rankTable[i].classification]++;
                }
            }

            Classification MaxLikelihoodClassification = rankTable[0].classification;
            int MaxLikelihoodClassificationCount = 1;
            foreach (var entry in resultCountTable)
            {
                if (entry.Value > MaxLikelihoodClassificationCount)
                {
                    MaxLikelihoodClassification = entry.Key;
                    MaxLikelihoodClassificationCount = entry.Value;
                }
            }

            rankTable.Clear();

            return MaxLikelihoodClassification;
        }
コード例 #2
0
ファイル: KnnCore.cs プロジェクト: aweeesome/scribex
        /// <summary>
        /// Applying KNN algorithm to two images, using Hausdorff method as difference.
        /// </summary>
        /// <param name="inputImage"></param>
        /// <returns></returns>
        private Classification RecognizeSuperimposedImageWithHausdorffMethod(Image inputImage)
        {
            //If training set isn't loaded yet, load it
            if (!loadedTrainingSet)
            {
                LoadTrainingSet();
            }

            //Fill up rankTable
            foreach (TrainingSetUnit tsu in trainingSet)
            {
                RankEntry re = new RankEntry();
                re.squareDistance = ProcrustesAnalysis.ComputeHausdroffDistance(inputImage, tsu.image);
                re.classification = tsu.classification;
                rankTable.Add(re);
            }

            rankTable.Sort();

            Dictionary<Classification, int> resultCountTable = new Dictionary<Classification, int>();

            for (int i = 0; i < k; i++)
            {
                try
                {
                    resultCountTable.Add(rankTable[i].classification, 0);
                }
                catch (ArgumentException)
                {
                    resultCountTable[rankTable[i].classification]++;
                }
            }

            Classification MaxLikelihoodClassification = rankTable[0].classification;
            int MaxLikelihoodClassificationCount = 1;
            foreach (var entry in resultCountTable)
            {
                if (entry.Value > MaxLikelihoodClassificationCount)
                {
                    MaxLikelihoodClassification = entry.Key;
                    MaxLikelihoodClassificationCount = entry.Value;
                }
            }

            rankTable.Clear();

            return MaxLikelihoodClassification;
        }
コード例 #3
0
ファイル: KnnCore.cs プロジェクト: aweeesome/scribex
        /// <summary>
        /// Applying KNN algorithm to two bool arrays.
        /// </summary>
        /// <param name="boolArray"></param>
        /// <returns></returns>
        private Classification RecognizeImageWithBoolArrayMethod(Image img)
        {
            //If training set isn't loaded yet, load it
            if (!loadedTrainingSet)
            {
                LoadTrainingSet();
            }

            //Fill up rankTable
            foreach (TrainingSetUnit tsu in trainingSet)
            {
                RankEntry re = new RankEntry();
                bool[,] boolArray = ImageHelper.MonoBitmapToBoolArray((Bitmap)img);
                bool[,] pixelBoolArray = ImageHelper.MonoBitmapToBoolArray((Bitmap)tsu.image);
                re.squareDistance = SquareDistanceBetweenBoolArrays(pixelBoolArray, boolArray);
                re.classification = tsu.classification;
                rankTable.Add(re);
            }

            rankTable.Sort();

            Dictionary<Classification, int> resultCountTable = new Dictionary<Classification, int>();

            for (int i = 0; i < k; i++)
            {
                try
                {
                    resultCountTable.Add(rankTable[i].classification, 0);
                }
                catch (ArgumentException)
                {
                    resultCountTable[rankTable[i].classification]++;
                }
            }

            Classification MaxLikelihoodClassification = rankTable[0].classification;
            int MaxLikelihoodClassificationCount = 1;
            foreach (var entry in resultCountTable)
            {
                if (entry.Value > MaxLikelihoodClassificationCount)
                {
                    MaxLikelihoodClassification = entry.Key;
                    MaxLikelihoodClassificationCount = entry.Value;
                }
            }

            rankTable.Clear();

            return MaxLikelihoodClassification;
        }