private int SaveMatchString(DescriptorsMatchModel descriptorsMatch, bool UseMask) { var leftImageName = descriptorsMatch.LeftDescriptor.KeyPoint.InputFile.fileInfo.Name; var rightImageName = descriptorsMatch.RightDescriptor.KeyPoint.InputFile.fileInfo.Name; var matchesList = descriptorsMatch.FilteredMatch ? descriptorsMatch.FilteredMatchesList : descriptorsMatch.MatchesList; if (descriptorsMatch.Mask == null || descriptorsMatch.FilteredMatchesList.Count == 0) { descriptorsMatch.FileFormatMatch = null; return(0); } int countMaskMatches = 0; if (UseMask) { for (int m = 0; m < matchesList.Count; m++) { if (descriptorsMatch.Mask.GetValue(0, m) > 0) { countMaskMatches++; } } } StringBuilder sb = new StringBuilder(); sb.AppendLine(Path.Combine(Path.GetFullPath(tempDirectory), leftImageName)); sb.AppendLine(Path.Combine(Path.GetFullPath(tempDirectory), rightImageName)); sb.AppendLine($"{(UseMask ? countMaskMatches : matchesList.Count)}"); for (int m = 0; m < matchesList.Count; m++) { if (!UseMask || descriptorsMatch.Mask.GetValue(0, m) > 0) { sb.Append($"{matchesList[m][0].TrainIdx} "); } } sb.AppendLine(); for (int m = 0; m < matchesList.Count; m++) { if (!UseMask || descriptorsMatch.Mask.GetValue(0, m) > 0) { sb.Append($"{matchesList[m][0].QueryIdx} "); } } descriptorsMatch.FileFormatMatch = sb.ToString(); return(0); }
private void FindMatches(IFeatureMatcher matcher, DescriptorModel leftDescriptor, DescriptorModel rightDescriptor, bool AddToList = true, bool FilterMatches = true, bool ComputeHomography = true, bool SaveInMatchNode = true) { WindowsFormHelper.AddLogToConsole($"Start computing matches for: \n" + $"\t{leftDescriptor.KeyPoint.InputFile.fileInfo.Name.ToString()}\n" + $"\t{rightDescriptor.KeyPoint.InputFile.fileInfo.Name.ToString()}\n"); var foundedMatch = new DescriptorsMatchModel() { FilteredMatch = FilterMatches, LeftDescriptor = leftDescriptor, RightDescriptor = rightDescriptor }; var matches = new VectorOfVectorOfDMatch(); matcher.Match(leftDescriptor.Descriptors, rightDescriptor.Descriptors, matches); WindowsFormHelper.AddLogToConsole( $"FINISH computing matches for: \n" + $"\t{leftDescriptor.KeyPoint.InputFile.fileInfo.Name.ToString()}\n" + $"\t{rightDescriptor.KeyPoint.InputFile.fileInfo.Name.ToString()}\n" ); MDMatch[][] matchesArray = matches.ToArrayOfArray(); foundedMatch.MatchesList = matchesArray.ToList(); if (FilterMatches) { FindMinMaxDistInMatches(matchesArray, ref ms_MAX_DIST, ref ms_MIN_DIST); List <MDMatch[]> filteredMatchesList = FilterMatchesByMaxDist(matchesArray); foundedMatch.FilteredMatchesList = filteredMatchesList; } if (ComputeHomography) { var PerspectiveMatrix = new Mat(); Mat Mask = new Mat(); lock (locker) { var matchesForHomography = FilterMatches ? foundedMatch.FilteredMatchesList : foundedMatch.MatchesList; if (matchesForHomography.Count > 0) { PerspectiveMatrix = FindHomography(leftDescriptor.KeyPoint.DetectedKeyPoints, rightDescriptor.KeyPoint.DetectedKeyPoints, FilterMatches ? foundedMatch.FilteredMatchesList : foundedMatch.MatchesList, Mask); foundedMatch.Mask = Mask; foundedMatch.PerspectiveMatrix = PerspectiveMatrix; } } } // Save drawing image Mat output = new Mat(); Directory.CreateDirectory($@"{tempDirectory}\DrawMatches"); Features2DToolbox.DrawMatches(new Mat(foundedMatch.LeftDescriptor.KeyPoint.InputFile.fileInfo.FullName), foundedMatch.LeftDescriptor.KeyPoint.DetectedKeyPoints, new Mat(foundedMatch.RightDescriptor.KeyPoint.InputFile.fileInfo.FullName), foundedMatch.RightDescriptor.KeyPoint.DetectedKeyPoints, new VectorOfVectorOfDMatch(foundedMatch.FilteredMatchesList.ToArray()), output, new MCvScalar(0, 0, 255), new MCvScalar(0, 255, 0), foundedMatch.Mask); output.Save(Path.Combine($@"{tempDirectory}\DrawMatches", $"{Path.GetFileNameWithoutExtension(foundedMatch.RightDescriptor.KeyPoint.InputFile.fileInfo.Name)}-{Path.GetFileNameWithoutExtension(foundedMatch.LeftDescriptor.KeyPoint.InputFile.fileInfo.Name)}.JPG")); fileManager.listViewerModel._lastDrawnMatches = new Image <Bgr, byte>(output.Bitmap); var inputFile = new InputFileModel(Path.Combine($@"{tempDirectory}\DrawMatches", $"{Path.GetFileNameWithoutExtension(foundedMatch.RightDescriptor.KeyPoint.InputFile.fileInfo.Name)}-{Path.GetFileNameWithoutExtension(foundedMatch.LeftDescriptor.KeyPoint.InputFile.fileInfo.Name)}.JPG")); var imageList = _winForm.ImageList[(int)EListViewGroup.DrawnMatches]; var listViewer = _winForm.ListViews[(int)EListViewGroup.DrawnMatches]; fileManager.AddInputFileToList(inputFile, fileManager.listViewerModel.ListOfListInputFolder[(int)EListViewGroup.DrawnMatches], imageList, listViewer); if (SaveInMatchNode) { SaveMatchString(foundedMatch, true); } if (AddToList) { FoundedMatches.Add(foundedMatch); } }