private void QueryLocate(string queryImagePath) { List <ImageRecord> searchImage = new List <ImageRecord>(); WriteQueryStatus("Start querying..."); _stopWatch = Stopwatch.StartNew(); LocateSettings locateSetting = GetLocateSetting(); SaveLocateSetting(); string msg; LocateQuery locateQuery = new LocateQuery(); searchImage = locateQuery.QueryImage(queryImagePath, out msg, locateSetting); _stopWatch.Stop(); msg += string.Format(" Total {0} ms", _stopWatch.ElapsedMilliseconds); WriteQueryStatus(msg); this.Dispatcher.Invoke(() => { ImageList.ItemsSource = searchImage; lblTotalCount.Text = searchImage.Count.ToString(); }); }
private LocateSettings GetLocateSetting() { LocateSettings rtnSetting = new LocateSettings(); this.Dispatcher.Invoke(() => { rtnSetting.IsCodeBookNeedToBeCreated = chkCreateCodeBook.IsChecked.Value; if (rtnSetting.IsCodeBookNeedToBeCreated) { UpdateCodeBookName(); } rtnSetting.CodeBookFullPath = txtCodeBookPath.Text; rtnSetting.isLteSchemeNeedToAppy = chkLTEScheme.IsChecked.Value; rtnSetting.SizeOfCodeBook = nudCodeBookLength.Value.Value; rtnSetting.GoodThresholdDistance = nudLocateDistanceThreshold.Value.Value; ListBoxItem selectedItem = cmbLocateQueryAlgo.SelectedItem as ListBoxItem; if (selectedItem != null) { string method = selectedItem.Content.ToString(); if (method.ToLower() == "extended") { rtnSetting.IsExtendedSearch = true; } else { rtnSetting.IsExtendedSearch = false; } } }); return(rtnSetting); }
private void SaveLocateSetting() { LocateSettings rtnSetting = new LocateSettings(); this.Dispatcher.Invoke(() => { Properties.Settings.Default.Locate_IsCodeBookNeedToBeCreated = chkCreateCodeBook.IsChecked.Value; Properties.Settings.Default.CodeBookFolder = txtCodeBookPath.Text; Properties.Settings.Default.Locate_GoodThresholdDistance = nudLocateDistanceThreshold.Value.Value; Properties.Settings.Default.Locate_SizeOfCookbook = nudCodeBookLength.Value.Value; Properties.Settings.Default.Locate_IsLteSchemeNeedToAppy = chkLTEScheme.IsChecked.Value; Properties.Settings.Default.Locate_SelectedMethod = cmbLocateQueryAlgo.SelectedIndex; Properties.Settings.Default.Save(); }); }
private void LocateAlgoIndexing(LocateSettings locateSetting) { var imageFiles = getFiles(IndexDirectory, "*.gif|*.jpg|*.png|*.bmp|*.jpeg", SearchOption.TopDirectoryOnly); int totalFileCount = imageFiles.Length; Dispatcher.Invoke((Action) delegate { pbIndex.Minimum = 0; pbIndex.Maximum = totalFileCount; lblIndexStatus.Text = "Indexing.."; }); LocateIndexer LocateIndexer = new LocateIndexer(); if (ExecInParallel) { LocateIndexer.IndexFilesAsync(imageFiles, IndexBgWorker, WriteIndexStatus, locateSetting); } else { LocateIndexer.IndexFiles(imageFiles, IndexBgWorker, WriteIndexStatus, locateSetting); } }
private void btnIndex_Click(object sender, RoutedEventArgs e) { btnIndex.IsEnabled = false; if (SelectedAlgo == emAlgo.SURF || SelectedAlgo == emAlgo.AccordSurf) { //Save Settings SaveSurfSettingValues(); //Pass Surf Settings SurfSettings surfSetting = GetSurfSetting(); IndexBgWorker.RunWorkerAsync(surfSetting); } else if (SelectedAlgo == emAlgo.Locate) { LocateSettings locateSetting = GetLocateSetting(); SaveLocateSetting(); IndexBgWorker.RunWorkerAsync(locateSetting); } else { IndexBgWorker.RunWorkerAsync(); } }
private void LocateAlgoIndexing(LocateSettings locateSetting) { var imageFiles = getFiles(IndexDirectory, "*.gif|*.jpg|*.png|*.bmp|*.jpeg", SearchOption.TopDirectoryOnly); int totalFileCount = imageFiles.Length; Dispatcher.Invoke((Action)delegate { pbIndex.Minimum = 0; pbIndex.Maximum = totalFileCount; lblIndexStatus.Text = "Indexing.."; }); LocateIndexer LocateIndexer = new LocateIndexer(); if (ExecInParallel) LocateIndexer.IndexFilesAsync(imageFiles, IndexBgWorker, WriteIndexStatus, locateSetting); else LocateIndexer.IndexFiles(imageFiles, IndexBgWorker, WriteIndexStatus, locateSetting); }
private LocateSettings GetLocateSetting() { LocateSettings rtnSetting = new LocateSettings(); this.Dispatcher.Invoke(() => { rtnSetting.IsCodeBookNeedToBeCreated = chkCreateCodeBook.IsChecked.Value; if (rtnSetting.IsCodeBookNeedToBeCreated) UpdateCodeBookName(); rtnSetting.CodeBookFullPath = txtCodeBookPath.Text; rtnSetting.isLteSchemeNeedToAppy = chkLTEScheme.IsChecked.Value; rtnSetting.SizeOfCodeBook = nudCodeBookLength.Value.Value; rtnSetting.GoodThresholdDistance = nudLocateDistanceThreshold.Value.Value; ListBoxItem selectedItem = cmbLocateQueryAlgo.SelectedItem as ListBoxItem; if (selectedItem != null) { string method = selectedItem.Content.ToString(); if (method.ToLower() == "extended") rtnSetting.IsExtendedSearch = true; else rtnSetting.IsExtendedSearch = false; } }); return rtnSetting; }
public List <ImageRecord> QueryImage(string queryImagePath, out string messageToLog, LocateSettings locateSetting = null) { List <ImageRecord> rtnImageList = new List <ImageRecord>(); #region Diagnostic Region Stopwatch sw = new Stopwatch(); Stopwatch sw1 = new Stopwatch(); long _loadingTime = 0, _queryingMAVTime = 0, _matchingTime = 0; #endregion Diagnostic Region #region Reading Cookbook sw1.Reset(); sw1.Start(); //-----Reading Cookbook double[][] codeBook = null; string fullFileName = locateSetting.CodeBookFullPath; if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); codeBook = (double[][])bf.Deserialize(fs); fs.Close(); } #endregion #region Reading Image Data //----Read Image Data LoCaTeDataSet locateDS = null; fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin"); if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); locateDS = (LoCaTeDataSet)bf.Deserialize(fs); fs.Close(); } sw1.Stop(); _loadingTime = sw1.ElapsedMilliseconds; #endregion #region Query Image Detection and Visual Words //---Reading Query Image sw1.Reset(); sw1.Start(); LoCATe descriptor = new LoCATe(); List <double[]> queryDescriptors = descriptor.extract(new Bitmap(queryImagePath), "SURF"); double[] queryVisualWord = createVisualWord(queryDescriptors, codeBook); int totalFileCount = locateDS.AllImageRecordSet.Count; if (locateSetting.isLteSchemeNeedToAppy) { queryVisualWord = doMaths(queryVisualWord, locateDS.HistogramSumOfAllVisualWords, totalFileCount); } sw1.Stop(); _queryingMAVTime = sw1.ElapsedMilliseconds; #endregion List <LoCaTeBoWRecord> AllImageRecordSet = locateDS.AllImageRecordSet; double[][] AllDatas = AllImageRecordSet.Select(rec => rec.VisaulWord).ToArray(); #region Searching of Image sw1.Reset(); sw1.Start(); if (locateSetting.isLteSchemeNeedToAppy) { //-----------Creating ltc Data double[][] ltcData = new double[totalFileCount][]; for (int i = 0; i < AllDatas.Length; i++) { ltcData[i] = doMaths((double[])(AllDatas[i]).Clone(), locateDS.HistogramSumOfAllVisualWords, totalFileCount); } AllDatas = ltcData; } double EucDistance; for (int i = 0; i < totalFileCount; i++) { EucDistance = Accord.Math.Distance.Euclidean(queryVisualWord, AllDatas[i]); AllImageRecordSet[i].Distance = EucDistance; } List <LoCaTeRanker> listofImageWRanker = new List <LoCaTeRanker>(); if (locateSetting.IsExtendedSearch) { //Take first 25 images IEnumerable <LoCaTeBoWRecord> first25Image = AllImageRecordSet.OrderBy(rec => rec.Distance) .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance) .Take(25); //Map to Locate Ranker list Mapper.CreateMap <LoCaTeBoWRecord, LoCaTeRanker>(); Mapper.CreateMap <ImageRecord, LoCaTeRanker>(); listofImageWRanker = Mapper.Map <IEnumerable <LoCaTeBoWRecord>, List <LoCaTeRanker> >(first25Image); //Calculate locate range int totalImageCount = listofImageWRanker.Count; for (int i = 0; i < totalImageCount; i++) { double percentage = (1 - (listofImageWRanker[i].Distance / 1)) * 100; listofImageWRanker[i].LocateRank = Convert.ToInt32(percentage); } //Perform SURF query listofImageWRanker = PerformExtendedSurfSearch(queryImagePath, listofImageWRanker); int totalimageBeforeCEDD = listofImageWRanker.Count; //listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.SurfRank).ToList(); //Perform CEDD query CEDDQuery2 queryCEDD = new CEDDQuery2(); var imageListbyCEDD = queryCEDD.QueryImage(queryImagePath).Take(25).ToList(); totalImageCount = imageListbyCEDD.Count; for (int i = 0; i < totalImageCount; i++) { long id = imageListbyCEDD[i].Id; double percentage = (1 - (imageListbyCEDD[i].Distance / 35)) * 100; var img2 = listofImageWRanker.Where(img => img.Id == id).SingleOrDefault(); if (img2 == null) { var newImage = Mapper.Map <ImageRecord, LoCaTeRanker>(imageListbyCEDD[i]); newImage.CEDDRank = Convert.ToInt32(percentage); listofImageWRanker.Add(newImage); } else { img2.CEDDRank = Convert.ToInt32(percentage); } } totalImageCount = listofImageWRanker.Count; for (int i = 0; i < totalImageCount; i++) { var imageRank = listofImageWRanker[i]; if (imageRank.SurfRank > 0) { imageRank.CombinedRank = 100 - (totalimageBeforeCEDD - imageRank.SurfRank); } else { imageRank.CombinedRank = (0.5 * imageRank.CEDDRank) + (0.4 * imageRank.LocateRank); } imageRank.Distance = imageRank.CombinedRank; } listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.CombinedRank).ToList(); rtnImageList = listofImageWRanker.ToList <ImageRecord>(); } else { rtnImageList = AllImageRecordSet.OrderBy(rec => rec.Distance) .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance) .Take(25) .ToList <ImageRecord>(); } sw1.Stop(); _matchingTime = sw1.ElapsedMilliseconds; #endregion messageToLog = string.Format("Loading time {0} ms, Query image processing {1} ms, Matching {2} ms", _loadingTime, _queryingMAVTime, _matchingTime); return(rtnImageList); }
public void IndexFiles(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action<string> logWriter, LocateSettings locateSetting = null) { //For Time Profilling long extractingTime, kMeanTime = 0, calcBagOfVisualTime = 0; Stopwatch sw1; SimpleSurfSift.LoCATe descriptorExtractor = new SimpleSurfSift.LoCATe(); sw1 = Stopwatch.StartNew(); logWriter("Index started, extracting Descriptors..."); List<double[]> ListofDescriptorsForCookBook = new List<double[]>(); List<LoCATeRecord> ListOfAllImageDescriptors = new List<LoCATeRecord>(); int totalFileCount = imageFiles.Length; if (totalFileCount == 0) { logWriter("No files to index"); return; }; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Bitmap observerImage = (Bitmap)Image.FromFile(fi.FullName)) { List<double[]> locateDescriptors = descriptorExtractor.extract(observerImage, "SURF"); ListOfAllImageDescriptors.Add(new LoCATeRecord { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, LoCATeDescriptors = locateDescriptors }); if (locateSetting.IsCodeBookNeedToBeCreated) { if (locateDescriptors.Count > 4) { RandomHelper randNumGenerator = new RandomHelper(); List<int> randIndexes = randNumGenerator.GetRandomNumberInRange(0, locateDescriptors.Count, 10d); foreach (int index in randIndexes) { ListofDescriptorsForCookBook.Add(locateDescriptors[index]); } } else { Debug.WriteLine(fi.Name + " skip from index, because it didn't have significant feature"); } } } IndexBgWorker.ReportProgress(i); } sw1.Stop(); extractingTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); double[][] codeBook = null; if (locateSetting.IsCodeBookNeedToBeCreated) { logWriter("Indexing, Calculating Mean..."); sw1.Reset(); sw1.Start(); KMeans kMeans = new KMeans(locateSetting.SizeOfCodeBook); kMeans.Compute(ListofDescriptorsForCookBook.ToArray()); codeBook = kMeans.Clusters.Centroids; //------------Save CookBook string fullFileName = locateSetting.CodeBookFullPath; 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(); bf.Serialize(fs, codeBook); fs.Close(); } sw1.Stop(); kMeanTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); } else { string fullFileName = locateSetting.CodeBookFullPath; if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); codeBook = (double[][])bf.Deserialize(fs); fs.Close(); } } logWriter("Indexing, Calculating Bag of Visual Words..."); sw1.Reset(); sw1.Start(); List<LoCaTeBoWRecord> ListOfImageVisualBagOfWorks = new List<LoCaTeBoWRecord>(); for (int i = 0; i < ListOfAllImageDescriptors.Count; i++) { double[] visualWordForImage = createVisualWord(ListOfAllImageDescriptors[i].LoCATeDescriptors, codeBook); LoCaTeBoWRecord rec = new LoCaTeBoWRecord { Id = ListOfAllImageDescriptors[i].Id, ImageName = ListOfAllImageDescriptors[i].ImageName, ImagePath = ListOfAllImageDescriptors[i].ImagePath, VisaulWord = visualWordForImage }; ListOfImageVisualBagOfWorks.Add(rec); IndexBgWorker.ReportProgress(i); } logWriter("Indexing, Calculating ltcData..."); int[] histogramSumOfAllVisualWords = null; //------------Creating sum histogram of all words double[][] AllDatas = ListOfImageVisualBagOfWorks.Select(des => des.VisaulWord).ToArray(); histogramSumOfAllVisualWords = createIndex((double[][])(AllDatas)); //------------Creating Image Records Data LoCaTeDataSet locateDS = new LoCaTeDataSet { AllImageRecordSet = ListOfImageVisualBagOfWorks, HistogramSumOfAllVisualWords = histogramSumOfAllVisualWords }; logWriter("Indexing, Saving Image Data..."); //------------Save CookBook string ImageRecordName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin"); if (File.Exists(ImageRecordName)) File.Delete(ImageRecordName); using (FileStream fs = new FileStream(ImageRecordName, FileMode.Create, FileAccess.Write, FileShare.None)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(fs, locateDS); fs.Close(); } sw1.Stop(); calcBagOfVisualTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); logWriter(string.Format("Extracting: {0} sec, KMeanTime: {1} sec, CalcBagOfVisalTime: {2} sec", extractingTime, kMeanTime, calcBagOfVisualTime)); }
public void IndexFilesAsync(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action<string> logWriter, LocateSettings locateSetting = null) { throw new NotImplementedException(); }
public List<ImageRecord> QueryImage(string queryImagePath, out string messageToLog, LocateSettings locateSetting = null) { List<ImageRecord> rtnImageList = new List<ImageRecord>(); #region Diagnostic Region Stopwatch sw = new Stopwatch(); Stopwatch sw1 = new Stopwatch(); long _loadingTime = 0, _queryingMAVTime = 0, _matchingTime = 0; #endregion Diagnostic Region #region Reading Cookbook sw1.Reset(); sw1.Start(); //-----Reading Cookbook double[][] codeBook = null; string fullFileName = locateSetting.CodeBookFullPath; if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); codeBook = (double[][])bf.Deserialize(fs); fs.Close(); } #endregion #region Reading Image Data //----Read Image Data LoCaTeDataSet locateDS = null; fullFileName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin"); if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); locateDS = (LoCaTeDataSet)bf.Deserialize(fs); fs.Close(); } sw1.Stop(); _loadingTime = sw1.ElapsedMilliseconds; #endregion #region Query Image Detection and Visual Words //---Reading Query Image sw1.Reset(); sw1.Start(); LoCATe descriptor = new LoCATe(); List<double[]> queryDescriptors = descriptor.extract(new Bitmap(queryImagePath), "SURF"); double[] queryVisualWord = createVisualWord(queryDescriptors, codeBook); int totalFileCount = locateDS.AllImageRecordSet.Count; if (locateSetting.isLteSchemeNeedToAppy) { queryVisualWord = doMaths(queryVisualWord, locateDS.HistogramSumOfAllVisualWords, totalFileCount); } sw1.Stop(); _queryingMAVTime = sw1.ElapsedMilliseconds; #endregion List<LoCaTeBoWRecord> AllImageRecordSet = locateDS.AllImageRecordSet; double[][] AllDatas = AllImageRecordSet.Select(rec => rec.VisaulWord).ToArray(); #region Searching of Image sw1.Reset(); sw1.Start(); if (locateSetting.isLteSchemeNeedToAppy) { //-----------Creating ltc Data double[][] ltcData = new double[totalFileCount][]; for (int i = 0; i < AllDatas.Length; i++) { ltcData[i] = doMaths((double[])(AllDatas[i]).Clone(), locateDS.HistogramSumOfAllVisualWords, totalFileCount); } AllDatas = ltcData; } double EucDistance; for (int i = 0; i < totalFileCount; i++) { EucDistance = Accord.Math.Distance.Euclidean(queryVisualWord, AllDatas[i]); AllImageRecordSet[i].Distance = EucDistance; } List<LoCaTeRanker> listofImageWRanker = new List<LoCaTeRanker>(); if (locateSetting.IsExtendedSearch) { //Take first 25 images IEnumerable<LoCaTeBoWRecord> first25Image = AllImageRecordSet.OrderBy(rec => rec.Distance) .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance) .Take(25); //Map to Locate Ranker list Mapper.CreateMap<LoCaTeBoWRecord, LoCaTeRanker>(); Mapper.CreateMap<ImageRecord, LoCaTeRanker>(); listofImageWRanker = Mapper.Map<IEnumerable<LoCaTeBoWRecord>, List<LoCaTeRanker>>(first25Image); //Calculate locate range int totalImageCount = listofImageWRanker.Count; for (int i = 0; i < totalImageCount; i++) { double percentage = (1 - (listofImageWRanker[i].Distance / 1)) * 100; listofImageWRanker[i].LocateRank = Convert.ToInt32(percentage); } //Perform SURF query listofImageWRanker = PerformExtendedSurfSearch(queryImagePath, listofImageWRanker); int totalimageBeforeCEDD = listofImageWRanker.Count; //listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.SurfRank).ToList(); //Perform CEDD query CEDDQuery2 queryCEDD = new CEDDQuery2(); var imageListbyCEDD = queryCEDD.QueryImage(queryImagePath).Take(25).ToList(); totalImageCount = imageListbyCEDD.Count; for (int i = 0; i < totalImageCount; i++) { long id = imageListbyCEDD[i].Id; double percentage = (1 - (imageListbyCEDD[i].Distance / 35)) * 100; var img2 = listofImageWRanker.Where(img => img.Id == id).SingleOrDefault(); if (img2 == null) { var newImage = Mapper.Map<ImageRecord, LoCaTeRanker>(imageListbyCEDD[i]); newImage.CEDDRank = Convert.ToInt32(percentage); listofImageWRanker.Add(newImage); } else { img2.CEDDRank = Convert.ToInt32(percentage); } } totalImageCount = listofImageWRanker.Count; for (int i = 0; i < totalImageCount; i++ ) { var imageRank = listofImageWRanker[i]; if (imageRank.SurfRank > 0 ) { imageRank.CombinedRank = 100 - (totalimageBeforeCEDD - imageRank.SurfRank); } else { imageRank.CombinedRank = (0.5 * imageRank.CEDDRank) + (0.4 * imageRank.LocateRank); } imageRank.Distance = imageRank.CombinedRank; } listofImageWRanker = listofImageWRanker.OrderByDescending(rec => rec.CombinedRank).ToList(); rtnImageList = listofImageWRanker.ToList<ImageRecord>(); } else { rtnImageList = AllImageRecordSet.OrderBy(rec => rec.Distance) .Where(rec => rec.Distance < locateSetting.GoodThresholdDistance) .Take(25) .ToList<ImageRecord>(); } sw1.Stop(); _matchingTime = sw1.ElapsedMilliseconds; #endregion messageToLog = string.Format("Loading time {0} ms, Query image processing {1} ms, Matching {2} ms", _loadingTime, _queryingMAVTime, _matchingTime); return rtnImageList; }
public void IndexFiles(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action <string> logWriter, LocateSettings locateSetting = null) { //For Time Profilling long extractingTime, kMeanTime = 0, calcBagOfVisualTime = 0; Stopwatch sw1; SimpleSurfSift.LoCATe descriptorExtractor = new SimpleSurfSift.LoCATe(); sw1 = Stopwatch.StartNew(); logWriter("Index started, extracting Descriptors..."); List <double[]> ListofDescriptorsForCookBook = new List <double[]>(); List <LoCATeRecord> ListOfAllImageDescriptors = new List <LoCATeRecord>(); int totalFileCount = imageFiles.Length; if (totalFileCount == 0) { logWriter("No files to index"); return; } ; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Bitmap observerImage = (Bitmap)Image.FromFile(fi.FullName)) { List <double[]> locateDescriptors = descriptorExtractor.extract(observerImage, "SURF"); ListOfAllImageDescriptors.Add(new LoCATeRecord { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, LoCATeDescriptors = locateDescriptors }); if (locateSetting.IsCodeBookNeedToBeCreated) { if (locateDescriptors.Count > 4) { RandomHelper randNumGenerator = new RandomHelper(); List <int> randIndexes = randNumGenerator.GetRandomNumberInRange(0, locateDescriptors.Count, 10d); foreach (int index in randIndexes) { ListofDescriptorsForCookBook.Add(locateDescriptors[index]); } } else { Debug.WriteLine(fi.Name + " skip from index, because it didn't have significant feature"); } } } IndexBgWorker.ReportProgress(i); } sw1.Stop(); extractingTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); double[][] codeBook = null; if (locateSetting.IsCodeBookNeedToBeCreated) { logWriter("Indexing, Calculating Mean..."); sw1.Reset(); sw1.Start(); KMeans kMeans = new KMeans(locateSetting.SizeOfCodeBook); kMeans.Compute(ListofDescriptorsForCookBook.ToArray()); codeBook = kMeans.Clusters.Centroids; //------------Save CookBook string fullFileName = locateSetting.CodeBookFullPath; 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(); bf.Serialize(fs, codeBook); fs.Close(); } sw1.Stop(); kMeanTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); } else { string fullFileName = locateSetting.CodeBookFullPath; if (!File.Exists(fullFileName)) { string msg = string.Format("Couldn't find {0}, Please Index before querying with Locate", fullFileName); throw new InvalidOperationException(msg); } 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(); codeBook = (double[][])bf.Deserialize(fs); fs.Close(); } } logWriter("Indexing, Calculating Bag of Visual Words..."); sw1.Reset(); sw1.Start(); List <LoCaTeBoWRecord> ListOfImageVisualBagOfWorks = new List <LoCaTeBoWRecord>(); for (int i = 0; i < ListOfAllImageDescriptors.Count; i++) { double[] visualWordForImage = createVisualWord(ListOfAllImageDescriptors[i].LoCATeDescriptors, codeBook); LoCaTeBoWRecord rec = new LoCaTeBoWRecord { Id = ListOfAllImageDescriptors[i].Id, ImageName = ListOfAllImageDescriptors[i].ImageName, ImagePath = ListOfAllImageDescriptors[i].ImagePath, VisaulWord = visualWordForImage }; ListOfImageVisualBagOfWorks.Add(rec); IndexBgWorker.ReportProgress(i); } logWriter("Indexing, Calculating ltcData..."); int[] histogramSumOfAllVisualWords = null; //------------Creating sum histogram of all words double[][] AllDatas = ListOfImageVisualBagOfWorks.Select(des => des.VisaulWord).ToArray(); histogramSumOfAllVisualWords = createIndex((double[][])(AllDatas)); //------------Creating Image Records Data LoCaTeDataSet locateDS = new LoCaTeDataSet { AllImageRecordSet = ListOfImageVisualBagOfWorks, HistogramSumOfAllVisualWords = histogramSumOfAllVisualWords }; logWriter("Indexing, Saving Image Data..."); //------------Save CookBook string ImageRecordName = Path.Combine(DirectoryHelper.SaveDirectoryPath, "LoCATeImageRecords.bin"); if (File.Exists(ImageRecordName)) { File.Delete(ImageRecordName); } using (FileStream fs = new FileStream(ImageRecordName, FileMode.Create, FileAccess.Write, FileShare.None)) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); bf.Serialize(fs, locateDS); fs.Close(); } sw1.Stop(); calcBagOfVisualTime = Convert.ToInt32(sw1.Elapsed.TotalSeconds); logWriter(string.Format("Extracting: {0} sec, KMeanTime: {1} sec, CalcBagOfVisalTime: {2} sec", extractingTime, kMeanTime, calcBagOfVisualTime)); }
public void IndexFilesAsync(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action <string> logWriter, LocateSettings locateSetting = null) { throw new NotImplementedException(); }