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); }
/// <summary> /// Utilizado para carregar as imagens de treinamento e gerar o arquivo de descritores /// </summary> private void LoadTrain() { Image <Bgr, Byte> image; for (int i = 0; i < banknotes.Count; i++) { var banknote = banknotes[i]; //J = 1 pois é o indice que está no nome das imagens de treinamento for (int j = 1; j <= banknoteSizes[banknote]; j++) { var nameFile = $@"{Path}\Train\{banknote} ({j}).jpg"; image = new Image <Bgr, Byte>(nameFile); listaImagensTreino.Add(nameFile, image); MKeyPoint[] keypoints; keypoints = extractor.Detect(image); listaKeyPointsImagensTreino.Add(nameFile, keypoints); Mat features = new Mat(); extractor.Compute(image, new VectorOfKeyPoint(keypoints), features); featureUnclustered.PushBack(features); } } //Armazenando os descritores processados da etapa de cima em um arquivo train_descriptors.yml FileStorage fs = new FileStorage($@"{Path}\SVM Datasets\train_descriptors.yml", FileStorage.Mode.Write); //Adicionando Label train_descriptors ao arquivo train_descriptors.yml fs.Write(featureUnclustered, "train_descriptors"); fs.ReleaseAndGetString(); //Adicionando descritores não processados no BOW bowTrainer.Add(featureUnclustered); }
private static void BowTest() { DescriptorMatcher matcher = new BFMatcher(); Feature2D extractor = AKAZE.Create(); Feature2D detector = AKAZE.Create(); TermCriteria criteria = new TermCriteria(CriteriaType.Count | CriteriaType.Eps, 10, 0.001); BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(200, criteria, 1); BOWImgDescriptorExtractor bowDescriptorExtractor = new BOWImgDescriptorExtractor(extractor, matcher); Mat img = null; KeyPoint[] keypoint = detector.Detect(img); Mat features = new Mat(); extractor.Compute(img, ref keypoint, features); bowTrainer.Add(features); throw new NotImplementedException(); }
static void train_svm() { int n_samples = 0; SURF surf = new SURF(400); List <Bitmap> samples = new List <Bitmap>(); List <Tuple <Bitmap, int> > data = new List <Tuple <Bitmap, int> >(); /* * foreach (string s in System.IO.Directory.GetFiles("mail_samples")) * { * Bitmap f1 = new Bitmap(s);//ImageDecoder.DecodeFromFile(s); * data.Add(new Tuple<Bitmap, int>(f1, +1)); * } * foreach (string s in System.IO.Directory.GetFiles("phone_icons")) * { * Bitmap f1 = new Bitmap(s); //ImageDecoder.DecodeFromFile(s); * data.Add(new Tuple<Bitmap, int>(f1, -1)); * } */ foreach (string s in System.IO.Directory.GetFiles(@"C:\test\iphone_icon")) { Bitmap f1 = new Bitmap(s);//ImageDecoder.DecodeFromFile(s); if (string.Compare(System.IO.Path.GetFileNameWithoutExtension(s), "temp_1") == 0 || string.Compare(System.IO.Path.GetFileNameWithoutExtension(s), "scoll_selected_icon") == 0 ) { data.Add(new Tuple <Bitmap, int>(f1, +1)); } else { data.Add(new Tuple <Bitmap, int>(f1, 0)); } } n_samples = data.Count; // computr bow Mat m = new Mat(); foreach (Tuple <Bitmap, int> v in data) { Image <Bgr, Byte> i = new Image <Bgr, byte>(v.Item1); Mat ii = new Mat(); CvInvoke.CvtColor(i, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); surf.Compute(ii, new VectorOfKeyPoint(kp), desc); m.PushBack(desc); } // Create the vocabulary with KMeans. MCvTermCriteria tc = new MCvTermCriteria(100, 0.00001); BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(16, tc, 3, KMeansInitType.PPCenters); bowTrainer.Add(m); Mat voca = new Mat(); bowTrainer.Cluster(voca); // BFMatcher matcher = new BFMatcher(DistanceType.L2); BOWImgDescriptorExtractor bowDex = new BOWImgDescriptorExtractor(surf, matcher); bowDex.SetVocabulary(voca); // Mat tDesc = new Mat(); //Matrix<int> tLabel = new Matrix<int>(1, n_samples); Matrix <int> tLabel = new Matrix <int>(n_samples, 1); //foreach (Tuple<Bitmap, int> v in data) for (int j = 0; j < data.Count; j++) { Image <Bgr, Byte> i = new Image <Bgr, byte>(data[j].Item1); Mat ii = new Mat(); CvInvoke.CvtColor(i, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); bowDex.Compute(ii, new VectorOfKeyPoint(kp), desc); tDesc.PushBack(desc); //tLabel[0, j] = data[j].Item2; tLabel[j, 0] = data[j].Item2; } // //SVM model = new SVM(); //model.SetKernel(Emgu.CV.ML.SVM.SvmKernelType.Linear); //model.Type = SVM.SvmType.CSvc; //model.C = 1; //model.TermCriteria = new MCvTermCriteria(100, 0.00001); SVM svm = new SVM(); svm.C = 312.5; svm.Gamma = 0.50625000000000009; svm.SetKernel(SVM.SvmKernelType.Rbf); svm.Type = SVM.SvmType.CSvc; svm.Nu = 0.5; TrainData td = new TrainData(tDesc, Emgu.CV.ML.MlEnum.DataLayoutType.RowSample, tLabel); bool tained = svm.TrainAuto(td); using (FileStorage fs = new FileStorage("voca.yaml", FileStorage.Mode.Write)) { svm.Write(fs); fs.Write(voca, "voca"); } //using (FileStorage fs = new FileStorage("svm.yaml", FileStorage.Mode.Write)) //{ // svm.Write(fs); //} //svm.Save("svm.yaml"); // test { //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\test\scroll_left.jpg"); Image <Bgr, Byte> test_img = new Image <Bgr, byte>(@"C:\test\iphone_icon\temp_1.jpg"); //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\projects\local\testMQ\testMQ\bin\Debug\phone_icons\icon_2.jpg"); //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\test\35928233-email-icon-on-blue-background-clean-vector.jpg"); Mat ii = new Mat(); CvInvoke.CvtColor(test_img, ii, ColorConversion.Bgr2Gray); MKeyPoint[] kp = surf.Detect(ii); Mat desc = new Mat(); bowDex.Compute(ii, new VectorOfKeyPoint(kp), desc); float r = svm.Predict(desc); } }