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); } } } } } } }
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 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(); } } } } }