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);
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 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();
                    }
                }
            }
        }
        /*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)
                                {
                                }
                            }
                    }
                }
            }
        }
        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);
                            }
                }
        }
        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();
                        }
                    }
                }
            }
        }