public static void AddSuperMatrixList(SuperMatrixType o) { // NOTE: Apply expiration parameters as you see fit. // I typically pull from configuration file. // In this example, I want an absolute // timeout so changes will always be reflected // at that time. Hence, the NoSlidingExpiration. if (TrackSize) { _cacheSizeinKb += MemorySize.GetBlobSizeinKb(o); } cache.Add( _superMatrixDataSet, o, DateTime.Now.AddMinutes(1440)); }
/// <summary> /// Load the cache from the File /// </summary> /// <param name="fullFileName">File the full file path, else, it will try to load from default file if it exists</param> /// <returns></returns> public static void LoadRespositoryFromFile(SurfAlgo surfAlgo, string fullFileName = "") { string repoFileStoragePath = string.Empty; string defaultRepoFileStorageName = surfAlgo == SurfAlgo.Linear ? "observerFeatureSets.bin" : "superMatrix.bin"; if (string.IsNullOrWhiteSpace(fullFileName)) { repoFileStoragePath = Path.Combine(SaveDirectoryPath, defaultRepoFileStorageName); } else { repoFileStoragePath = fullFileName; } if (!File.Exists(repoFileStoragePath)) { throw new ArgumentException(string.Format("Can't find Surf Repository file at {0} :", repoFileStoragePath)); } FileStream stream = File.OpenRead(repoFileStoragePath); var formatter = new BinaryFormatter(); if (surfAlgo == SurfAlgo.Linear) { List <SURFRecord2> observerFeatureSets = (List <SURFRecord2>)formatter.Deserialize(stream); stream.Close(); AddSURFRecord2List(observerFeatureSets); } else if (surfAlgo == SurfAlgo.Flaan) { SuperMatrixType superMatrix = (SuperMatrixType)formatter.Deserialize(stream); stream.Close(); AddSuperMatrixList(superMatrix); } //Polenter.Serialization.SharpSerializerBinarySettings st = new Polenter.Serialization.SharpSerializerBinarySettings(Polenter.Serialization.BinarySerializationMode.SizeOptimized); //Polenter.Serialization.SharpSerializer serializer = new Polenter.Serialization.SharpSerializer(st); //List<SURFRecord2> observerFeatureSets = (List<SURFRecord2>)serializer.Deserialize(repoFileStoragePath); }
private static void SaveRepoForFlann() { if (!Exists(_superMatrixDataSet)) { throw new InvalidOperationException("Can't save. Super Matrix not added to Repository"); } SuperMatrixType superMatrix = GetSurfDataSet(); string exeDirectoryPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string repoFileStoragePath = Path.Combine(exeDirectoryPath, "superMatrix.bin"); File.Delete(repoFileStoragePath); FileStream stream = File.Create(repoFileStoragePath); var formatter = new BinaryFormatter(); formatter.Serialize(stream, superMatrix); stream.Close(); //Polenter.Serialization.SharpSerializerBinarySettings st = new Polenter.Serialization.SharpSerializerBinarySettings(Polenter.Serialization.BinarySerializationMode.SizeOptimized); //Polenter.Serialization.SharpSerializer serializer = new Polenter.Serialization.SharpSerializer(st); //serializer.Serialize(featureSets, repoFileStoragePath); }
public static void AddSuperMatrixList(SuperMatrixType o) { // NOTE: Apply expiration parameters as you see fit. // I typically pull from configuration file. // In this example, I want an absolute // timeout so changes will always be reflected // at that time. Hence, the NoSlidingExpiration. if (TrackSize) _cacheSizeinKb += MemorySize.GetBlobSizeinKb(o); cache.Add( _superMatrixDataSet, o, DateTime.Now.AddMinutes(1440)); }
public void IndexFiles(FileInfo[] imageFiles, System.ComponentModel.BackgroundWorker IndexBgWorker, Action<string> logWriter, SurfSettings surfSetting = null) { #region Surf Dectator Region double hessianThresh = 500; double uniquenessThreshold = 0.8; if (surfSetting != null) { hessianThresh = surfSetting.HessianThresh.Value; uniquenessThreshold = surfSetting.UniquenessThreshold.Value; } SURFDetector surfDectector = new SURFDetector(hessianThresh, false); #endregion int rows = 0; Matrix<float> superMatrix = null; List<SURFRecord1> observerSurfImageIndexList = new List<SURFRecord1>(); Stopwatch sw1, sw2; sw1 = Stopwatch.StartNew(); logWriter("Index started..."); int totalFileCount = imageFiles.Length; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Image<Gray, byte> observerImage = new Image<Gray, byte>(fi.FullName)) { VectorOfKeyPoint observerKeyPoints = new VectorOfKeyPoint(); Matrix<float> observerDescriptor = surfDectector.DetectAndCompute(observerImage, null, observerKeyPoints); if (observerDescriptor.Rows > 4) { int initRow = rows; int endRows = rows + observerDescriptor.Rows - 1; SURFRecord1 record = new SURFRecord1 { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, IndexStart = rows, IndexEnd = endRows }; observerSurfImageIndexList.Add(record); if (superMatrix == null) superMatrix = observerDescriptor; else superMatrix = superMatrix.ConcateVertical(observerDescriptor); rows = endRows + 1; } else { Debug.WriteLine(fi.Name + " skip from index, because it didn't have significant feature"); } } IndexBgWorker.ReportProgress(i); } sw1.Stop(); logWriter(string.Format("Index Complete, it tooked {0} ms. Saving Repository...", sw1.ElapsedMilliseconds)); SurfDataSet surfDataset = new SurfDataSet { SurfImageIndexRecord = observerSurfImageIndexList, SuperMatrix = superMatrix }; sw2 = Stopwatch.StartNew(); SurfRepository.AddSuperMatrixList(surfDataset); SurfRepository.SaveRepository(SurfAlgo.Flaan); sw2.Stop(); logWriter(string.Format("Index tooked {0} ms. Saving Repository tooked {1} ms", sw1.ElapsedMilliseconds, sw2.ElapsedMilliseconds)); }