Exemplo n.º 1
0
        IList <PreProcessedImage> ITrainModel.startTrainingModel()
        {
            IList <PreProcessedImage> result = new List <PreProcessedImage>(this.trainingModel.Sum(item => item.listOfImages.Count));

            using (PreProcess preProcesAlgorithm = new PreProcess(null))
            {
                int count = 0;
                foreach (ImageCategory imageCategory in this.trainingModel)
                {
                    Console.WriteLine(count++ + " Training category: " + imageCategory.leafCategory + "total files: " + imageCategory.listOfImages.Count);
                    foreach (string imagePath in imageCategory.listOfImages)
                    {
                        PreProcessedImage preProcessedImage = preProcesAlgorithm.Execute(imagePath, imageCategory.leafCategory);
                        result.Add(preProcessedImage);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private List <MatcherResult> QueryImage(string filePath, string expectedCategory)
        {
            using (PreProcess preProcessAlgorithm = new PreProcess(null))
            {
                PreProcessedImage    preProcessedQueryImage = preProcessAlgorithm.Execute(filePath, expectedCategory);
                List <MatcherResult> finalResultSet         = new List <MatcherResult>(this.trainingDataset.Count);
                DescriptorMatcher    learningAlgo           = new DescriptorMatcher();

                foreach (PreProcessedImage trainingData in this.trainingDataset)
                {
                    try
                    {
                        using (BFMatcher trainingMatcher = new BFMatcher(DistanceType.L1))
                        {
                            double areaRatio    = trainingData.ContourArea / preProcessedQueryImage.ContourArea;
                            int    contourDelta = trainingData.NumberOfContours - preProcessedQueryImage.NumberOfContours;
                            //if (areaRatio < 0.1 || areaRatio > 5)
                            //{
                            //    continue;
                            //}

                            //if(contourDelta > 100 || contourDelta < -100)
                            //{
                            //    continue;
                            //}

                            trainingMatcher.Add(trainingData.KeyPoints.Descriptor);
                            MatcherResult result = learningAlgo.knnMatch(preProcessedQueryImage.KeyPoints, trainingMatcher, trainingData.Category, 300, trainingData);
                            result.ContourRatio  = CvInvoke.MatchShapes(trainingData.Contour, preProcessedQueryImage.Contour, Emgu.CV.CvEnum.ContoursMatchType.I3);
                            result.AreaRatio     = areaRatio;
                            result.ContoursDelta = contourDelta;
                            //Console.WriteLine("QueryCategory: {0}, {1}", preProcessedQueryImage.Category, result.ToString());
                            finalResultSet.Add(result);
                        }
                    }
                    catch (Exception exception)
                    {
                        //Console.WriteLine(exception);
                    }
                }

                //IEnumerable<MatcherResult> highConfidenceResults = finalResultSet.Where(item => item.MatchingPoints >= 3);
                //finalResultSet = highConfidenceResults.ToList();
                //// If there are clusters with size great than or equal to 3 , then they have a very high probability of being correct.
                //if (highConfidenceResults.Any())
                //{
                //    //Console.WriteLine("High confidence:" + highConfidenceResults.Count() + " : " + highConfidenceResults.Max(item => item.MatchingPoints));
                //    finalResultSet = highConfidenceResults.ToList();
                //}
                //else
                //{
                //    finalResultSet = finalResultSet
                //                            .Where(item => item.MatchingPoints > 0)
                //                            .GroupBy(item => item.Category)
                //                            .Select(item =>
                //                                new MatcherResult()
                //                                {
                //                                    Category = item.Key,
                //                                    MatchingPoints = item.Sum(result => result.MatchingPoints),
                //                                    MatchDistance = item.Sum(result => result.MatchDistance)
                //                                })
                //                            .ToList();
                //}

                double totalWeightDistance = finalResultSet.Sum(item => item.MatchDistanceWeight);
                double totalWeightPoints   = finalResultSet.Sum(item => item.MatchingPointsWeight);

                //finalResultSet = finalResultSet
                //                    .Where(item => item.MatchingPoints > 0)
                //                    .GroupBy(item => item.Category)
                //                    .Select(item =>
                //                        new MatcherResult()
                //                        {
                //                            Category = item.Key,
                //                            MatchingPoints = item.Sum(result => result.MatchingPoints * result.MatchingPointsWeight) / totalWeightPoints,
                //                            MatchDistance = item.Sum(result => result.MatchDistance * result.MatchDistanceWeight) / totalWeightDistance
                //                        })
                //                    .ToList();

                finalResultSet = finalResultSet
                                 .Where(item => item.MatchingPoints > 0)
                                 .GroupBy(item => item.Category)
                                 .Select(item =>
                                         new MatcherResult()
                {
                    Category       = item.Key,
                    MatchingPoints = item.Sum(result => result.MatchingPointsWeight) / totalWeightPoints,
                    MatchDistance  = item.Sum(result => result.MatchDistanceWeight) / totalWeightDistance
                })
                                 .ToList();

                finalResultSet = finalResultSet.Where(item => item.MatchingPoints > 0).OrderByDescending(item => item, new MatcherResultWeightedComparer()).Take(3).ToList();
                return(finalResultSet);
            }
        }