private void SaveSiftFile(DescriptorModel Descriptor, bool SaveInTempDirectory = true, bool SaveInDescriptorNode = true) { var descriptor = Descriptor.Descriptors; var keyPoints = Descriptor.KeyPoint.DetectedKeyPoints; var fileName = $"{Path.GetFileNameWithoutExtension(Descriptor.KeyPoint.InputFile.fileInfo.Name)}.SIFT"; var countKeypoint = keyPoints.Size; var countDescriptor = descriptor.Cols; StringBuilder sb = new StringBuilder(); sb.AppendLine($"{countKeypoint} 128"); for (int i = 0; i < descriptor.Rows; i++) { // X a Y su prehodene, teraz je to dobre sb.AppendLine($"{keyPoints[i].Point.Y} {keyPoints[i].Point.X} {keyPoints[i].Size} {keyPoints[i].Angle}"); for (int j = 0; j < 128; j++) { if (j < descriptor.Cols) { sb.Append($"{descriptor.GetValue(i, j)} "); } else { sb.Append("0 "); } } sb.AppendLine(); } if (SaveInTempDirectory) { File.WriteAllText(Path.Combine(tempDirectory, fileName), sb.ToString()); } if (SaveInDescriptorNode) { Descriptor.FileFormatSIFT = sb.ToString(); } }
private void ComputeDescriptor(KeyPointModel keypoint, IFeatureDescriptor descriptor, bool AddToList = true, bool SaveOnDisk = true) { WindowsFormHelper.AddLogToConsole($"Start computing descriptor for: {keypoint.InputFile.fileInfo.Name.ToString()}\n"); var computedDescriptor = descriptor.ComputeDescriptor(keypoint); var descriptorNode = new DescriptorModel() { Descriptors = computedDescriptor, KeyPoint = keypoint }; WindowsFormHelper.AddLogToConsole($"FINISH computing descriptor for: {keypoint.InputFile.fileInfo.Name.ToString()}\n"); if (AddToList) { ComputedDescriptors.Add(keypoint.ID, descriptorNode); } if (SaveOnDisk) { SaveSiftFile(descriptorNode); } }
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); } }