Exemple #1
0
        static void test_svm()
        {
            FileStorage fs   = new FileStorage("test.yaml", FileStorage.Mode.Read);
            FileNode    n    = fs["voca"];
            Mat         voca = new Mat();

            n.ReadMat(voca);

            SURF      surf    = new SURF(400);
            BFMatcher matcher = new BFMatcher(DistanceType.L2);
            BOWImgDescriptorExtractor bowDex = new BOWImgDescriptorExtractor(surf, matcher);

            bowDex.SetVocabulary(voca);

            SVM svm = new SVM();

            //FileStorage fs1 = new FileStorage("svm.yaml", FileStorage.Mode.Read);
            svm.Read(fs.GetRoot());

            foreach (string s in System.IO.Directory.GetFiles(@"C:\projects\local\testMQ\testMQ\bin\Debug\icons"))
            {
                Image <Bgr, Byte> test_img = new Image <Bgr, byte>(s);
                //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\projects\local\testMQ\testMQ\bin\Debug\mail_samples\email_icon_t.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);
                Program.logIt(string.Format("{0}={1}", s, r));
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
        public void RunTest()
        {
            using (var descriptorExtractor = SIFT.Create(500))
                //using (var descriptorMatcher = new FlannBasedMatcher(ip, sp))
                using (var descriptorMatcher = new BFMatcher())
                    using (var img = Image("lenna.png"))
                    {
                        KeyPoint[] keypoints;
                        Mat        dictionary;
                        var        tc = new TermCriteria(CriteriaType.MaxIter, 100, 0.001d);
                        using (var bowTrainer = new BOWKMeansTrainer(200, tc, 1, KMeansFlags.PpCenters))
                        {
                            var descriptors = new Mat();
                            descriptorExtractor.DetectAndCompute(img, null, out keypoints, descriptors);

                            Mat featuresUnclustered = new Mat();
                            featuresUnclustered.PushBack(descriptors);
                            featuresUnclustered.ConvertTo(featuresUnclustered, MatType.CV_32F);
                            dictionary = bowTrainer.Cluster(featuresUnclustered);
                        }

                        using (var bowDE = new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher))
                        {
                            bowDE.SetVocabulary(dictionary);

                            try
                            {
                                using (Mat descriptors = new Mat())
                                {
                                    descriptorExtractor.Compute(img, ref keypoints, descriptors);
                                    descriptors.ConvertTo(descriptors, MatType.CV_32F);
                                    bowDE.Compute(img, ref keypoints, descriptors, out var arr);
                                    Console.WriteLine(arr.Length);
                                    Console.WriteLine(arr[0].Length);
                                }
                            }
                            catch (OpenCVException ex)
                            {
                                Console.WriteLine(ex.FileName);
                                Console.WriteLine(ex.FuncName);
                                Console.WriteLine(ex.Line);
                                throw;
                            }
                        }

                        dictionary.Dispose();
                    }
        }
Exemple #5
0
        public void RunTest()
        {
            using var descriptorExtractor = SIFT.Create(500);
            using var descriptorMatcher   = new BFMatcher();
            using var img = Image("lenna.png");
            KeyPoint[] keypoints;
            Mat        dictionary;
            var        tc = new TermCriteria(CriteriaTypes.MaxIter, 100, 0.001d);

            using (var bowTrainer = new BOWKMeansTrainer(200, tc, 1, KMeansFlags.PpCenters))
            {
                var descriptors = new Mat();
                descriptorExtractor.DetectAndCompute(img, null, out keypoints, descriptors);

                using var featuresUnclustered = new Mat();
                featuresUnclustered.PushBack(descriptors);
                featuresUnclustered.ConvertTo(featuresUnclustered, MatType.CV_32F);
                dictionary = bowTrainer.Cluster(featuresUnclustered);
            }

            using (var bowDe = new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher))
            {
                bowDe.SetVocabulary(dictionary);

                try
                {
                    using var descriptors = new Mat();
                    descriptorExtractor.Compute(img, ref keypoints, descriptors);
                    descriptors.ConvertTo(descriptors, MatType.CV_32F);
                    bowDe.Compute(img, ref keypoints, descriptors, out var arr);
                    testOutputHelper.WriteLine(arr.Length.ToString(CultureInfo.InvariantCulture));
                    testOutputHelper.WriteLine(arr[0].Length.ToString(CultureInfo.InvariantCulture));
                }
                catch (OpenCVException ex)
                {
                    testOutputHelper.WriteLine(ex.FileName);
                    testOutputHelper.WriteLine(ex.FuncName);
                    testOutputHelper.WriteLine(ex.Line.ToString(CultureInfo.InvariantCulture));
                    throw;
                }
            }

            dictionary.Dispose();
        }
Exemple #6
0
        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);
            }
        }
        /// <summary>
        /// Utilizado para gerar os arquivos de treinados para o SVM
        /// </summary>
        public void RunTrain()
        {
            Image <Bgr, Byte> image;

            Console.WriteLine("Quantização vetorial...");

            //Chama o método responsavel por carregar as imagens de treinamento e extrair os descritores
            LoadTrain();

            //Obter a quantidade de caracteristicas (descritores) adicionados no BOW
            int count = bowTrainer.DescriptorCount;

            Console.WriteLine($"Clustering {count} características");
            Mat dictionary = new Mat();

            //Gera os vocabularios a partir dos descritores
            bowTrainer.Cluster(dictionary);

            //Cria um arquivo chamado train_clustered.yml com os vocabularios
            FileStorage fs = new FileStorage($@"{Path}\SVM Datasets\train_clustered.yml", FileStorage.Mode.Write);

            //Adiciona ao arquivo train_clustered.yml um label vocabulary
            fs.Write(dictionary, "vocabulary");
            fs.ReleaseAndGetString();
            //Realiza a extração dos histogramas em forma de BOW para cada imagem do treinamento
            bowDE.SetVocabulary(dictionary);
            Console.WriteLine("Extraindo histogramas na forma de BOW para cada imagem");
            //Labels é o nome da imagem que foi treinada, é utilizado para descobrir qual é o valor da imagem
            Matrix <int> labels;
            List <int>   listLabels   = new List <int>();
            Mat          trainingData = new Mat(0, dictionarySize, DepthType.Cv32F, 1);

            MKeyPoint[] keypoints;
            Mat         bowDescriptor = new Mat();

            //var notas = new List<int> { 2, 5, 10, 20, 50, 100 };
            for (int i = 0; i < banknotes.Count; i++)
            {
                var banknote = banknotes[i];
                for (int j = 1; j <= banknoteSizes[banknote]; j++)
                {
                    var fileName = $@"{Path}\Train\{banknote} ({j}).jpg";
                    image     = new Image <Bgr, Byte>(fileName);
                    keypoints = extractor.Detect(image);
                    bowDE.Compute(image, new VectorOfKeyPoint(keypoints), bowDescriptor);
                    trainingData.PushBack(bowDescriptor);
                    listLabels.Add(banknote);
                }
            }

            //Cria um arquivo com os dados treinados chamado trained_data.yml
            fs = new FileStorage($@"{Path}\SVM Datasets\trained_data.yml", FileStorage.Mode.Write);
            //Adiciona um label data ao arquivo trained_data.yml
            fs.Write(trainingData, "data");
            fs.ReleaseAndGetString();

            labels = new Matrix <int>(listLabels.ToArray());
            //Cria o arquivo trained_labels.yml que aramaze os labels das imagens que foram treinadas
            fs = new FileStorage($@"{Path}\SVM Datasets\trained_labels.yml", FileStorage.Mode.Write);
            //Adiciona o label no arquivo trained_labels.yml chamado labels
            fs.Write(labels.Mat, "labels");
            fs.ReleaseAndGetString();

            Console.WriteLine("Treinamento Concluido");

            //Obs.: trained_data.yml, trained_labels.yml e train_clustered.yml são os arquivos mais importantes,
            //os arquivos contem todo o conhecimento necessario para o SVM
        }