public MatchResult FindMatch(Mat queryMat) { try { var queryDescriptors = DescriptorManager.ExtractDescriptor(queryMat); const int Knn = 11; using (var indices = new Mat(new Size(queryDescriptors.Rows, 2), DepthType.Cv32F, 2)) using (var dists = new Mat(new Size(queryDescriptors.Rows, 2), DepthType.Cv32F, 2)) { IndexContext.CurrentFlannIndex.KnnSearch(queryDescriptors, indices, dists, Knn, 32); var result = new Dictionary <Models.Image, QueryHit>(); for (int i = 0; i < indices.Rows; i++) { // filter out all inadequate pairs based on distance between pairs if (dists.GetFloatValue(i, 0) < (0.6 * dists.GetFloatValue(i, 1))) { var pos = indices.GetIntValue(i, 0); //CvInvoke.FindHomography(); if (IndexContext.CurrentMappingIndex.ContainsKey(pos)) { var img = IndexContext.CurrentMappingIndex[pos]; if (!result.ContainsKey(img)) { result.Add(img, new QueryHit { Image = img }); } result[img].Hits++; } } } return(new MatchResult { Matches = result.Where(x => x.Value.Hits > Threshold).OrderByDescending(x => x.Value.Hits).ToDictionary(x => x.Key, y => y.Value), }); } } catch (Exception e) { return(null); } }
public void BuildIndex() { var imageRepo = new LocalImagesRepository(); var images = imageRepo.GetAll(); var descriptors = new List <Mat>(); var indexMappning = new Dictionary <int, Image>(); var startIndex = 0; foreach (var image in images) { var descriptor = DescriptorManager.ExtractDescriptor(image); if (descriptor == null) { continue; } //descriptor.IsEnabledDispose = false; descriptors.Add(descriptor); image.IndexStart = startIndex; image.IndexEnd = startIndex + descriptor.Size.Height - 1; for (var a = image.IndexStart; a < image.IndexEnd; a++) { indexMappning.Add(a, image); } //set new startIndex startIndex += descriptor.Rows; } IndexContext.CurrentMappingIndex = indexMappning; var indexParams = new LshIndexParamses(10, 10, 0); IndexContext.ConcatDescriptors = DescriptorManager.ConcatDescriptors(descriptors); IndexContext.CurrentFlannIndex = new Index(IndexContext.ConcatDescriptors, indexParams); }