示例#1
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);
        }
示例#2
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();
                    }
        }
示例#4
0
 public SvmBof()
 {
     //Utiliza KMeans
     bowTrainer         = new BOWKMeansTrainer(dictionarySize, tc, retries, KMeansInitType.PPCenters);
     bowDE              = new BOWImgDescriptorExtractor(extractor, matcher);
     banknoteSizes      = new Dictionary <int, int>();
     featureUnclustered = new Mat();
     synthesizer        = new SpeechSynthesizer();
     Path = Application.StartupPath;
     Path = Directory.GetParent(Directory.GetParent(Path).ToString()).ToString();
     banknoteSizes.Add(2, 35);   //2 Reais = 35 cédulas usadas para o treinamento
     banknoteSizes.Add(5, 17);   //5 Reais = 17 cédulas usadas para o treinamento
     banknoteSizes.Add(10, 20);  //10 Reais = 20 cédulas usadas para o treinamento
     banknoteSizes.Add(20, 22);  //20 Reais = 22 cédulas usadas para o treinamento
     banknoteSizes.Add(50, 25);  //50 Reais = 25 cédulas usadas para o treinamento
     banknoteSizes.Add(100, 24); //100 Reais = 24 cédulas usadas para o treinamento
 }
示例#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();
        }
示例#6
0
        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();
        }
示例#7
0
        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();
        }
示例#8
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);
            }
        }
        public void New()
        {
            var bow = new BOWKMeansTrainer(100);

            bow.Dispose();
        }