コード例 #1
0
        private void trainModel(String plantCategoryName, List <String> eachLeafSpecies)
        {
            BFMatcher match;

            if (!categoryBfmatcherMapping.ContainsKey(plantCategoryName))
            {
                match = new BFMatcher(DistanceType.L2);
                categoryBfmatcherMapping.Add(plantCategoryName, match);
            }
            else
            {
                //Find values associated with key
                match = categoryBfmatcherMapping[plantCategoryName];
            }
            foreach (String leaf in eachLeafSpecies)
            {
                using (Image <Bgr, Byte> image = loadImage(leaf))
                {
                    using (PreProcess preProcessAlgorithm = new PreProcess(image))
                    {
                        using (Image <Hsv, Byte> HSVImage = preProcessAlgorithm._ImageToHSV())
                        {
                            using (Image <Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert())
                            {
                                using (FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage))
                                {
                                    KeyPoints leafDescriptor = featureSet.SIFTDescriptor();
                                    match.Add(leafDescriptor.Descriptor);
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        private void trainModel(String plantCategoryName, List <String> eachLeafSpecies)
        {
            BFMatcher match;

            if (!categoryBfmatcherMapping.ContainsKey(plantCategoryName))
            {
                match = new  BFMatcher(DistanceType.L1);
                categoryBfmatcherMapping.Add(plantCategoryName, match);
            }
            else
            {
                //Find values associated with key
                match = categoryBfmatcherMapping[plantCategoryName];
            }
            foreach (String leaf in eachLeafSpecies)
            {
                PreProcess preProcessAlgorithm     = null;
                FeatureExtractAlgorithm featureSet = null;
                try
                {
                    //Console.WriteLine(leaf + "\n");
                    Image <Bgr, Byte> image = loadImage(leaf);
                    preProcessAlgorithm = new PreProcess(image);
                    //Image<Hsv, Byte> HSVImage = preProcessAlgorithm._ImageToHSV();
                    Image <Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert();

                    featureSet = new FeatureExtractAlgorithm(grayScaleImage);
                    KeyPoints leafDescriptor = featureSet.SIFTDescriptor();
                    match.Add(leafDescriptor.Descriptor);
                }
                finally
                {
                    if (preProcessAlgorithm != null)
                    {
                        preProcessAlgorithm.Dispose();
                    }

                    if (featureSet != null)
                    {
                        featureSet.Dispose();
                    }
                }
            }
        }
コード例 #3
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);
        }
コード例 #4
0
        /*public LeafAnalysis()
         * {
         *  TrainedDataSet trainingModel = new TrainedDataSet();
         *  trainingModel.ReadFileSystem();
         *  trainingModel.startTrainingModel();
         *  categoryBfmatcherMapping = trainingModel.ImagesBfmatcherMapping;
         * }
         * public LeafAnalysis(byte[] image)
         * {
         *  //1. Convert the image to bitmap
         *  QueryModel queryImage = new QueryModel();
         *  queryImage.startComparingImages();
         *  resultSet = queryImage.matchedResultSet;
         * }
         */
        /*public void trainModel(String plantCategoryName, List<String> eachLeafSpecies)
         * {
         *  BFMatcher match;
         *  if (!categoryBfmatcherMapping.ContainsKey(plantCategoryName))
         *  {
         *      match = new BFMatcher(DistanceType.L2);
         *      categoryBfmatcherMapping.Add(plantCategoryName, match);
         *  }
         *  else
         *  {
         *      //Find values associated with key
         *      match = categoryBfmatcherMapping[plantCategoryName];
         *  }
         *  foreach(String leaf in eachLeafSpecies)
         *  {
         *      Image<Bgr, Byte> image = loadImage(leaf);
         *      PreProcess preProcessAlgorithm = new PreProcess(image);
         *      Image<Hsv, Byte> HSVImage = preProcessAlgorithm._ImageToHSV();
         *      Image<Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert();
         *
         *      FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage);
         *      UMat leafDescriptor = featureSet.SIFTDescriptor();
         *      match.Add(leafDescriptor);
         *  }
         *
         * }
         *
         * public void queryModel(String imageCategory,String imagePath)
         * {
         *  PerfromanceEvaluator evaluateResult = new PerfromanceEvaluator();
         *  evaluateResult.expectedLeafName = imageCategory;
         *  List<MatcherResult> results = new List<MatcherResult>();
         *  Image<Bgr, Byte> image = loadImage(imagePath);
         *  PreProcess preProcessAlgorithm = new PreProcess(image);
         *  Image<Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert();
         *
         *  FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage);
         *  UMat leafDescriptor = featureSet.SIFTDescriptor();
         *  foreach (var pair in categoryBfmatcherMapping)
         *  {
         *      DescriptorMatcher learningAlgo = new DescriptorMatcher();
         *      BFMatcher bfmatch = pair.Value;
         *      string leafCategory = pair.Key;
         *      MatcherResult result = learningAlgo.knnMatch(leafDescriptor, bfmatch, leafCategory);
         *      results.Add(result);
         *  }
         *  results = results.OrderByDescending(item => item.MatchingPoints).Take(3).ToList();
         *  evaluateResult.result = results;
         *  resultSet.Add(evaluateResult);
         * }
         * public Image<Bgr, Byte> loadImage(String imagePath)
         * {
         *  //1)Load the image from file and resize it to display
         *  Image<Bgr, Byte> img =
         *     new Image<Bgr, byte>(imagePath)
         *     .Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
         *  return img;
         *
         * }
         *
         * public Image<Bgr, Byte> loadImage(byte[] image)
         * {
         *  using (MemoryStream stream = new MemoryStream(image))
         *  {
         *      Bitmap bitmap = new Bitmap(stream);
         *      Image<Bgr, Byte> img =
         *         new Image<Bgr, byte>(bitmap)
         *         .Resize(400, 400, Emgu.CV.CvEnum.Inter.Linear, true);
         *      return img;
         *  }
         * }
         *
         * public float findAccuracyOfSystem()
         * {
         *  PerfromanceEvaluator evaluate = new PerfromanceEvaluator();
         *  float accuracyValue = evaluate.computeAccuracy(resultSet);
         *  return accuracyValue;
         * }
         *
         * public void findPrecision()
         * {
         *  PerfromanceEvaluator evaluate = new PerfromanceEvaluator();
         *  evaluate.computePrecisionAndRecall(resultSet);
         *
         * }
         * //UMat umatGratScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingCvtColor();
         * //Image<Gray, Byte> _processedImageUsingGrabCut = preProcessAlgorithm.grabCutAlgo(img);
         * //FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage);
         * //Image<Gray, byte> _cornerOfTheImage = featureSet.harrisCornerDetection();
         *
         * //FeatureExtractAlgorithm featureSetUsingUmatType = new FeatureExtractAlgorithm(umatGratScaleImage);
         * //UMat _EdgeOfTheImage = featureSetUsingUmatType.cannyEdgeDetection();
         * //UMat largestContour = new UMat();
         *
         * //VectorOfPoint result = FindLargestContour(_EdgeOfTheImage, largestContour);
         *
         * /*UMat contourImage = new UMat();
         * MCvScalar color = new MCvScalar(0,0,255);
         *
         * CvInvoke.DrawContours(contourImage,result, 0, color);
         *
         * UMat trainingSiftdescriptors = featureSet.SIFTDescriptor(grayScaleImage);
         * UMat SurfDescriptors = featureSet.SURFDescriptor(_processedImageUsingGrabCut);
         * UMat OrbDescriptors = featureSet.ORBDescriptor(grayScaleImage);
         * DescriptorMatcher matcher = new DescriptorMatcher();
         * matcher.BfMatcher(trainingSiftdescriptors);
         */

        public void DetectAndCompute(Image <Bgr, byte> img)
        {
            using (PreProcess p = new PreProcess(img))
            {
                using (Image <Gray, Byte> grayScaleImage = p._ImageToGrayScaleUsingConvert())
                {
                    using (Image <Bgr, Byte> smoothImg = img.SmoothGaussian(5, 5, 1.5, 1.5))
                    {
                        using (Mat canny = new Mat())
                            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint())
                            {
                                CvInvoke.Canny(grayScaleImage, canny, 100, 50);
                                int[,] hierachy = CvInvoke.FindContourTree(canny, contours, ChainApproxMethod.ChainApproxSimple);
                                if (hierachy.GetLength(0) > 0)
                                {
                                }
                            }
                    }
                }
            }
        }
コード例 #5
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);
                            }
                }
        }
コード例 #6
0
        public void startComparingImages(Dictionary <String, BFMatcher> categoryBfmatcherMapping)
        {
            using (PreProcess preProcessAlgorithm = new PreProcess(queryImage))
            {
                using (Image <Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert())
                {
                    using (FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage))
                    {
                        using (KeyPoints leafDescriptor = featureSet.SIFTDescriptor())
                        {
                            foreach (var pair in categoryBfmatcherMapping)
                            {
                                if (pair.Key == "abies_concolor")
                                {
                                    continue;
                                }

                                try
                                {
                                    DescriptorMatcher learningAlgo = new DescriptorMatcher();
                                    BFMatcher         bfmatch      = pair.Value;
                                    string            leafCategory = pair.Key;
                                    MatcherResult     result       = learningAlgo.knnMatch(leafDescriptor, bfmatch, leafCategory);
                                    finalResultSet.Add(result);
                                }
                                catch (Exception exception)
                                {
                                    //TODO
                                    //Console.WriteLine(exception.ToString());
                                }
                            }
                            finalResultSet = finalResultSet.OrderByDescending(item => item, new MatcherResultComparer()).Take(3).ToList();
                        }
                    }
                }
            }
        }
コード例 #7
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);
            }
        }