public List <ImageRecord> QueryImage(string queryImagePath, out string messageToLog, SurfSettings surfSetting = null) { List <ImageRecord> rtnImageList = new List <ImageRecord>(); #region Diagnostic Region Stopwatch sw = new Stopwatch(); Stopwatch sw1 = new Stopwatch(); long _loadingTime = 0, _modelImageDectionlong = 0, _queryingTime = 0, _matchingTime = 0; #endregion Diagnostic Region #region Surf Dectator Region double hessianThresh = 500; double uniquenessThreshold = 0.8; int minGoodMatchPercent = 0; if (surfSetting != null) { hessianThresh = surfSetting.HessianThresh.Value; uniquenessThreshold = surfSetting.UniquenessThreshold.Value; minGoodMatchPercent = surfSetting.GoodMatchThreshold.Value; } float hessianThreshold2 = (float)hessianThresh / 1000000; SpeededUpRobustFeaturesDetector surf = new SpeededUpRobustFeaturesDetector(hessianThreshold2); #endregion Surf Dectator Region #region Get Model Dectection and Validation sw.Reset(); sw.Start(); List <SpeededUpRobustFeaturePoint> modelImageSurfPoints; using (Bitmap modelImage = (Bitmap)Image.FromFile(queryImagePath)) { modelImageSurfPoints = surf.ProcessImage(modelImage); } if (modelImageSurfPoints == null || modelImageSurfPoints.Count < 4) { throw new InvalidOperationException("Insuffucient interesting point in query image, try another query image"); } sw.Stop(); _modelImageDectionlong = sw.ElapsedMilliseconds; #endregion #region Search Images sw.Reset(); sw.Start(); string fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "SurfAccordLinear.bin"); if (!File.Exists(fullFileName)) { string exMsg = string.Format("Can't get the Surf Index at {0}, please index first", fullFileName); throw new FileNotFoundException(fullFileName); } using (FileStream fs = new FileStream(fullFileName, FileMode.Open, FileAccess.Read, FileShare.None)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); long fileLength = fs.Length; while (fs.Position < fileLength) { SURFAccordRecord3 record = (SURFAccordRecord3)bf.Deserialize(fs); KNearestNeighborMatching matcher = new KNearestNeighborMatching(2); matcher.Threshold = uniquenessThreshold; sw1.Start(); AForge.IntPoint[][] matches = matcher.Match(modelImageSurfPoints, record.SurfDescriptors); sw1.Stop(); var countOfMatchPoint = matches[0].Length; if (countOfMatchPoint > 0) { double totalnumberOfModelFeature = modelImageSurfPoints.Count; double matchPercentage = ((totalnumberOfModelFeature - (double)countOfMatchPoint) / totalnumberOfModelFeature); matchPercentage = (1 - matchPercentage) * 100; matchPercentage = Math.Round(matchPercentage); if (matchPercentage >= minGoodMatchPercent) { record.Distance = matchPercentage; rtnImageList.Add(record.Clone()); } } record = null; } fs.Close(); } sw.Stop(); _matchingTime = sw1.ElapsedMilliseconds; _queryingTime = sw.ElapsedMilliseconds; #endregion string msg = String.Format("Loading: {0}, Model detection: {1}, Querying: {2}, Matching: {3}", _loadingTime, _modelImageDectionlong, _queryingTime, _matchingTime); messageToLog = msg; if (rtnImageList.Count > 0) { rtnImageList = rtnImageList.OrderByDescending(rec => rec.Distance) .ToList <ImageRecord>(); } return(rtnImageList); }
public void IndexFiles(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action <string> logWriter, SurfSettings surfSetting = null) { //For Time Profilling long readingTime, indexingTime = 0, saveingTime = 0; #region Surf Dectator Region double hessianThresh = 500; double uniquenessThreshold = 0.8; if (surfSetting != null) { hessianThresh = surfSetting.HessianThresh.Value; uniquenessThreshold = surfSetting.UniquenessThreshold.Value; } float hessianThreshold2 = (float)hessianThresh / 1000000; SpeededUpRobustFeaturesDetector surf = new SpeededUpRobustFeaturesDetector(hessianThreshold2); #endregion int rows = 0; Stopwatch sw1; sw1 = Stopwatch.StartNew(); logWriter("Index started..."); string fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "SurfAccordLinear.bin"); if (File.Exists(fullFileName)) { File.Delete(fullFileName); } using (FileStream fs = new FileStream(fullFileName, FileMode.Create, FileAccess.Write, FileShare.None)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); int totalFileCount = imageFiles.Length; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Bitmap observerImage = (Bitmap)Image.FromFile(fi.FullName)) { List <SpeededUpRobustFeaturePoint> observerImageSurfPoints = surf.ProcessImage(observerImage); if (observerImageSurfPoints.Count > 4) { SURFAccordRecord3 record = new SURFAccordRecord3 { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, SurfDescriptors = observerImageSurfPoints }; bf.Serialize(fs, record); } else { Debug.WriteLine(fi.Name + " skip from index, because it didn't have significant feature"); } } IndexBgWorker.ReportProgress(i); } fs.Close(); } sw1.Stop(); readingTime = sw1.ElapsedMilliseconds; logWriter(string.Format("Reading Surb Complete, it tooked {0} ms. Saving Repository...", readingTime)); logWriter(string.Format("Reading: {0} ms, Indexing: {1} ms, Saving Indexed data {2}", readingTime, indexingTime, saveingTime)); }