예제 #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);
        }
예제 #2
0
        public PreProcessedImage Execute(string imagePath, string category)
        {
            PreProcessedImage result = new PreProcessedImage();

            result.FilePath = imagePath;
            result.Category = category;
            using (PreProcess preProcessAlgorithm = new PreProcess(new Image <Bgr, Byte>(imagePath)))
                using (Image <Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert())
                {
                    using (FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage))
                        using (Mat canny = new Mat())
                            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
                            {
                                KeyPoints descriptor = featureSet.SIFTDescriptor();
                                result.KeyPoints   = descriptor;
                                result.ContourArea = 0;
                                CvInvoke.Canny(grayScaleImage, canny, 100, 50);
                                int[,] hierarchy        = CvInvoke.FindContourTree(canny, contours, ChainApproxMethod.ChainApproxSimple);
                                result.NumberOfContours = contours.Size;
                                double maxArea  = double.MinValue;
                                int    maxIndex = -1;
                                for (int index = 0; index < contours.Size; index++)
                                {
                                    double area = CvInvoke.ContourArea(contours[index]);
                                    result.ContourArea += area;
                                    if (area > maxArea)
                                    {
                                        maxArea  = area;
                                        maxIndex = index;
                                    }
                                }

                                result.Contour = new VectorOfPoint();

                                CvInvoke.ApproxPolyDP(contours[maxIndex], result.Contour, CvInvoke.ArcLength(contours[maxIndex], true) * 0.02, true);
                                return(result);
                            }
                }
        }
예제 #3
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);
            }
        }
        private List <MDMatch> clusterBasedOnPoseEstimation(List <MDMatch> matches, KeyPoints queryKeyPoints, PreProcessedImage trainingData)
        {
            // If no training data is provided then we cannot compute pose (LeafAnalysisV1), hence just return back the original set
            if (trainingData == null ||
                matches == null ||
                !matches.Any())
            {
                return(matches);
            }

            Dictionary <MDMatch, List <MDMatch> > clusters = new Dictionary <MDMatch, List <MDMatch> >(matches.Count);
            List <PoseEstimate> poseEstimates = new List <PoseEstimate>(matches.Count);

            foreach (MDMatch match in matches)
            {
                MKeyPoint queryKeyPoint    = queryKeyPoints.Points[match.QueryIdx];
                MKeyPoint trainingKeyPoint = trainingData.KeyPoints.Points[match.TrainIdx];

                PoseEstimate estimate = new PoseEstimate();
                estimate.Match = match;
                estimate.Dx    = trainingKeyPoint.Point.X - queryKeyPoint.Point.X;
                estimate.Dy    = trainingKeyPoint.Point.Y - queryKeyPoint.Point.Y;
                estimate.Ds    = trainingKeyPoint.Octave / queryKeyPoint.Octave;
                estimate.Do    = trainingKeyPoint.Angle - queryKeyPoint.Angle;

                poseEstimates.Add(estimate);
                // Initialize clusters for each individual match
                // Next we will add other matches which belong to this cluster
                clusters.Add(match, new List <MDMatch>(new MDMatch[] { match }));
            }

            const double errorThreshold = 5;

            // Compute cluster membership
            foreach (PoseEstimate estimate in poseEstimates)
            {
                foreach (PoseEstimate otherEstimate in poseEstimates)
                {
                    // Ignore self
                    if (estimate == otherEstimate)
                    {
                        continue;
                    }

                    double error = estimate.RMSE(otherEstimate);
                    //Console.WriteLine("Error: " + trainingData.Category + ": " + error);
                    if (error < errorThreshold)
                    {
                        clusters[estimate.Match].Add(otherEstimate.Match);
                    }
                }
            }

            // Finally pick the largest cluster
            List <MDMatch> result        = null;
            int            sizeOfCluster = -1;;

            foreach (KeyValuePair <MDMatch, List <MDMatch> > cluster in clusters)
            {
                if (cluster.Value.Count == sizeOfCluster)
                {
                    // Tie breaker: choose the cluster with smaller overall distances
                    if (result.Sum(item => item.Distance) > cluster.Value.Sum(item => item.Distance))
                    {
                        result = cluster.Value;
                    }
                }
                else if (cluster.Value.Count > sizeOfCluster)
                {
                    sizeOfCluster = cluster.Value.Count;
                    result        = cluster.Value;
                }
            }

            return(result);
        }
        public MatcherResult knnMatch(KeyPoints queryDescriptor, BFMatcher matcher, string leafCategory, int distanceCutoff = int.MaxValue, PreProcessedImage trainingData = null)
        {
            MatcherResult result = new MatcherResult();

            result.Category = leafCategory;

            using (VectorOfVectorOfDMatch vectorMatchesForSift = new VectorOfVectorOfDMatch())
            {
                matcher.KnnMatch(queryDescriptor.Descriptor, vectorMatchesForSift, 2, null);

                int numberOfMatches = 0;

                Dictionary <int, int> counts      = new Dictionary <int, int>();
                List <MDMatch>        goodMatches = new List <MDMatch>(vectorMatchesForSift.Size);
                for (int i = 0; i < vectorMatchesForSift.Size; i++)
                {
                    // Do Ratio test: Reject matches where ratio of closest match with second closest if greater than 0.8
                    if (
                        (vectorMatchesForSift[i].Size == 1 ||
                         vectorMatchesForSift[i][0].Distance < 0.75 * vectorMatchesForSift[i][1].Distance) &&
                        vectorMatchesForSift[i][0].Distance < distanceCutoff)
                    {
                        goodMatches.Add(vectorMatchesForSift[i][0]);
                        numberOfMatches++;
                    }
                }

                //goodMatches = clusterBasedOnPoseEstimation(goodMatches, queryDescriptor, trainingData);

                int maxResults = int.MaxValue;
                result.MatchingPoints = goodMatches.Count;
                if (!goodMatches.Any())
                {
                    result.MatchDistance        = float.MaxValue;
                    result.AverageDistance      = 0;
                    result.MatchDistanceWeight  = 0;
                    result.MatchingPointsWeight = 0;
                }
                else
                {
                    result.MatchDistance        = goodMatches.OrderBy(item => item.Distance).Take(maxResults).Sum(item => item.Distance);
                    result.AverageDistance      = result.MatchDistance / result.MatchingPoints;
                    result.MatchDistanceWeight  = 1 / Math.Pow(result.AverageDistance, 2);
                    result.MatchingPointsWeight = result.MatchingPoints * result.MatchingPoints;
                }
            }
            return(result);
        }