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 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); }