Exemplo n.º 1
1
        public float classify(Image<Bgr, Byte> predImg)
        {
            using (SURF detector = new SURF(30))
            using (BFMatcher matcher = new BFMatcher(DistanceType.L2))
            using (Image<Gray, Byte> testImgGray = predImg.Convert<Gray, Byte>())
            using (VectorOfKeyPoint testKeyPoints = new VectorOfKeyPoint())
            using (Mat testBOWDescriptor = new Mat())
            using( bowDE = new BOWImgDescriptorExtractor(detector, matcher))
            {
                float result = 0;
                bowDE.SetVocabulary(vocabulary);
                detector.DetectRaw(predImg, testKeyPoints, null);
                bowDE.Compute(predImg, testKeyPoints, testBOWDescriptor);
                if(!testBOWDescriptor.IsEmpty)
                    result = svmClassifier.Predict(testBOWDescriptor);

                //result will indicate whether test image belongs to trainDescriptor label 1, 2
                return result;
            }
        }
Exemplo n.º 2
1
        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;

                        }
                    }
                }
            }
        }