public void computeAndExtract() { using (detector = new SURF(30)) using (matcher = new BFMatcher(DistanceType.L2)) { bowDE = new BOWImgDescriptorExtractor(detector, matcher); BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(100, new MCvTermCriteria(100, 0.01), 3, Emgu.CV.CvEnum.KMeansInitType.PPCenters); foreach(FileInfo[] folder in _folders) foreach (FileInfo file in folder) { using (Image<Bgr, Byte> model = new Image<Bgr, byte>(file.FullName)) using (VectorOfKeyPoint modelKeyPoints = new VectorOfKeyPoint()) //Detect SURF key points from images { detector.DetectRaw(model, modelKeyPoints); //Compute detected SURF key points & extract modelDescriptors Mat modelDescriptors = new Mat(); detector.Compute(model, modelKeyPoints, modelDescriptors); //Add the extracted BoW modelDescriptors into BOW trainer bowTrainer.Add(modelDescriptors); } input_num++; } //Cluster the feature vectors bowTrainer.Cluster(vocabulary); //Store the vocabulary bowDE.SetVocabulary(vocabulary); //training descriptors tDescriptors = new Mat(); labels = new Matrix<int>(1, input_num); int index = 0; //compute and store BOWDescriptors and set labels for (int i = 1; i <= _folders.Count; i++) { FileInfo[] files = _folders[i-1]; for (int j = 0; j < files.Length; j++) { FileInfo file = files[j]; using (Image<Bgr, Byte> model = new Image<Bgr, Byte>(file.FullName)) using (VectorOfKeyPoint modelKeyPoints = new VectorOfKeyPoint()) using (Mat modelBOWDescriptor = new Mat()) { detector.DetectRaw(model, modelKeyPoints); bowDE.Compute(model, modelKeyPoints, modelBOWDescriptor); tDescriptors.PushBack(modelBOWDescriptor); labels[0, index++] = i; } } } } }
public Prep_L2BOF(int classnum, string src) { SecondLayerDic = null; directory = src; classNum = classnum; bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); }
public Prep_L1BOF(int classnum,string loc) { directory = loc; topLayerDic = null; classNum = classnum; bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); }
public Load_L2BOF(int classnum, string src) { SecondLayerDic = null; directory = src; classNum = classnum; bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); IFormatter formatter = new BinaryFormatter(); FileStream fs = File.OpenRead(directory + "\\obj\\dic.xml"); SecondLayerDic = (Matrix<float>)formatter.Deserialize(fs); fs.Dispose(); bowDe.SetVocabulary(SecondLayerDic); SecondLayerSVM = new SVM(); SecondLayerSVM.Load(directory + "\\obj\\svm.xml"); Console.WriteLine("Finished Loading L2 SVM.");loaded = true; }
public Load_L1BOF(int classnum,string loc) { prefix = loc; topLayerDic = null; classNum = classnum; bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); IFormatter formatter = new BinaryFormatter(); FileStream fs = File.OpenRead(loc + "obj\\dic.xml"); topLayerDic = (Matrix<float>)formatter.Deserialize(fs); fs.Dispose(); bowDe.SetVocabulary(topLayerDic); topLayerSVM = new SVM(); topLayerSVM.Load(loc + "obj\\svm.xml"); Console.WriteLine("Finished Loading L1 SVM."); loaded = true; }
public Prep(List<Image<Bgr, byte>> imgs,string directory) { dir = directory; images = imgs; bowTrainer = new BOWKMeansTrainer(classNum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); labels = new Matrix<float>(images.Count, 1); foreach (Image<Bgr, byte> img in images) { Extract(img); } MakeDic(); double i = 1; foreach (Image<Bgr, byte> img in images) { int label_value = Convert.ToInt32(Math.Floor(i)); MakeDescriptors(img,label_value); i += 0.5; } Save(); }
//make Dictionarys private void MakeDic() { l1: try { Dic = bowTrainer.Cluster(); bowDe.SetVocabulary(Dic); trainingDescriptors = new Matrix<float>(images.Count, classNum); } catch (Exception e) { int int_classnum = classNum; int_classnum++; trainingDescriptors = new Matrix<float>(images.Count, int_classnum); bowTrainer = new BOWKMeansTrainer(int_classnum, new MCvTermCriteria(10, 0.01), 3, KMeansInitType.PPCenters); goto l1; } }
public void TestBOWKmeansTrainer2() { Image<Gray, byte> box = EmguAssert.LoadImage<Gray, byte>("box.png"); Brisk detector = new Brisk(30, 3, 1.0f); VectorOfKeyPoint kpts = new VectorOfKeyPoint(); Mat descriptors = new Mat(); detector.DetectAndCompute(box, null, kpts, descriptors, false); Mat descriptorsF = new Mat(); descriptors.ConvertTo(descriptorsF, CvEnum.DepthType.Cv32F); //Matrix<float> descriptorsF = descriptors.Convert<float>(); BOWKMeansTrainer trainer = new BOWKMeansTrainer(100, new MCvTermCriteria(), 3, CvEnum.KMeansInitType.PPCenters); trainer.Add(descriptorsF); Mat vocabulary = new Mat(); trainer.Cluster(vocabulary); BFMatcher matcher = new BFMatcher(DistanceType.L2); BOWImgDescriptorExtractor extractor = new BOWImgDescriptorExtractor(detector, matcher); Mat vocabularyByte = new Mat(); vocabulary.ConvertTo(vocabularyByte, CvEnum.DepthType.Cv8U); extractor.SetVocabulary(vocabularyByte); Mat descriptors2 = new Mat(); extractor.Compute(box, kpts, descriptors2); }
public void TestBOWKmeansTrainer() { Image<Gray, byte> box = EmguAssert.LoadImage<Gray, byte>("box.png"); SURF detector = new SURF(500); VectorOfKeyPoint kpts = new VectorOfKeyPoint(); Mat descriptors = new Mat(); detector.DetectAndCompute(box, null, kpts, descriptors, false); BOWKMeansTrainer trainer = new BOWKMeansTrainer(100, new MCvTermCriteria(), 3, CvEnum.KMeansInitType.PPCenters); trainer.Add(descriptors); Mat vocabulary = new Mat(); trainer.Cluster(vocabulary); BFMatcher matcher = new BFMatcher(DistanceType.L2); BOWImgDescriptorExtractor extractor = new BOWImgDescriptorExtractor(detector, matcher); extractor.SetVocabulary(vocabulary); Mat descriptors2 = new Mat(); extractor.Compute(box, kpts, descriptors2); }