Ejemplo n.º 1
0
        public static void FindMatch(
            Mat modelImage,
            Mat observedImage,
            out VectorOfKeyPoint modelKeyPoints,
            out VectorOfKeyPoint observedKeyPoints,
            VectorOfVectorOfDMatch matches,
            out Mat mask,
            out Mat homography)
        {
            int    k = 9;
            double uniquenessThreshold = 0.80;

            Stopwatch watch;

            homography = null;

            modelKeyPoints    = new VectorOfKeyPoint();
            observedKeyPoints = new VectorOfKeyPoint();

            using (UMat uModelImage = modelImage.GetUMat(AccessType.Read))
                using (UMat uObservedImage = observedImage.GetUMat(AccessType.Read))
                {
                    KAZE featureDetector = new KAZE();

                    //extract features from the object image
                    Mat modelDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                    watch = Stopwatch.StartNew();

                    // extract features from the observed image
                    Mat observedDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);

                    using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                        using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                            using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                            {
                                matcher.Add(modelDescriptors);

                                matcher.KnnMatch(observedDescriptors, matches, k, null);
                                mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                                mask.SetTo(new MCvScalar(255));
                                Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

                                int nonZeroCount = CvInvoke.CountNonZero(mask);
                                if (nonZeroCount >= 4)
                                {
                                    nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                                               matches, mask, 1.5, 20);
                                    if (nonZeroCount >= 4)
                                    {
                                        homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                                              observedKeyPoints, matches, mask, 2);
                                    }
                                }
                            }
                    watch.Stop();
                }
        }
Ejemplo n.º 2
0
        public static int FindMatch2(Mat modelImage, TemplateContainer.ImageData template, out long matchTime, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask, out Mat homography)
        {
            Stopwatch watch;

            homography = null;
            BriefDescriptorExtractor descriptor = new BriefDescriptorExtractor();

            modelKeyPoints    = new VectorOfKeyPoint();
            observedKeyPoints = new VectorOfKeyPoint();

            using (UMat uModelImage = modelImage.GetUMat(AccessType.Read))
            //using (UMat uObservedImage = template.image.Mat.GetUMat(AccessType.Read))
            {
                //extract features from the object image
                Mat modelDescriptors = new Mat();
                featureDetector.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                watch = Stopwatch.StartNew();

                // extract features from the observed image

                //featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
                observedKeyPoints   = template.keyPointsSurf;
                observedDescriptors = template.descriptorSurf;
                // Bruteforce, slower but more accurate
                // You can use KDTree for faster matching with slight loss in accuracy
                using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                    using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                        using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                        {
                            matcher.Add(modelDescriptors);

                            matcher.KnnMatch(observedDescriptors, matches, k, null);
                            mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                            mask.SetTo(new MCvScalar(255));
                            Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

                            nonZeroCount = CvInvoke.CountNonZero(mask);
                            if (nonZeroCount >= 10)
                            {
                                nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                                           matches, mask, 1.8, 18);
                                if (nonZeroCount >= 12)
                                {
                                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                                          observedKeyPoints, matches, mask, 2);
                                }
                            }
                        }
                watch.Stop();
            }
            matchTime = watch.ElapsedMilliseconds;
            return(nonZeroCount);
        }
Ejemplo n.º 3
0
        public void ShowKeyPoints()
        {
            lstMat.Clear();
            lstModelDescriptors.Clear();
            var featureDetector = new SIFT();

            Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams();
            Emgu.CV.Flann.SearchParams      sp = new SearchParams();
            DescriptorMatcher matcher          = new FlannBasedMatcher(ip, sp);
            Rectangle         cropRect         = new Rectangle(842, 646, 70, 70);
            Mat mask = new Mat(new Size(70, 70), DepthType.Cv8U, 1);

            CvInvoke.Rectangle(mask, new Rectangle(0, 0, 70, 70), new MCvScalar(255, 255, 255), -1);
            CvInvoke.Circle(mask, new Point(35, 37), 22, new MCvScalar(0, 0, 0), -1);


            lstMat.Add(mask);
            String[] folders = { @"Linage2\Main\PartyAuto", @"Linage2\Main\PartyManual" };
            foreach (String folder in folders)
            {
                DirectoryInfo imageFolder = new DirectoryInfo(folder);
                FileInfo[]    files       = Utils.GetFilesByExtensions(imageFolder, ".jpg", ".png").ToArray();
                foreach (FileInfo finfo in files)
                {
                    Mat img  = CvInvoke.Imread(finfo.FullName, ImreadModes.Color);
                    Mat crop = CVUtil.crop_color_frame(img, cropRect);
                    //lstMat.Add(crop);
                    VectorOfKeyPoint modelKeyPoints = new VectorOfKeyPoint();
                    Mat modelDescriptors            = new Mat();
                    featureDetector.DetectAndCompute(crop, mask, modelKeyPoints, modelDescriptors, false);
                    lstModelDescriptors.Add(modelDescriptors);
                    Mat result = new Mat();
                    Features2DToolbox.DrawKeypoints(crop, modelKeyPoints, result, new Bgr(Color.Red));

                    lstMat.Add(result);
                    //BOWImgDescriptorExtractor bow = new BOWImgDescriptorExtractor(featureDetector, matcher);
                }
            }


            /*BOWKMeansTrainer bowtrainer = new BOWKMeansTrainer(1000, new MCvTermCriteria(10, 0.001), 1, Emgu.CV.CvEnum.KMeansInitType.PPCenters);
             * foreach (Mat m in lstModelDescriptors) {
             *  bowtrainer.Add(m);
             * }
             * Mat dict = new Mat();
             * bowtrainer.Cluster();
             * StringBuilder sb = new StringBuilder();
             * Image<Bgr, Byte> imgsave = dict.ToImage<Bgr, Byte>();
             *
             * (new XmlSerializer(typeof(Image<Bgr, Byte>))).Serialize(new StringWriter(sb), imgsave);
             * Console.WriteLine(sb.ToString());*/
        }
Ejemplo n.º 4
0
        public void FindMatches(Image <Rgb, byte> SubMap, out VectorOfKeyPoint VectorSubMapKeyPoint,
                                out Mat SubMapDiscriptors, out VectorOfVectorOfDMatch matches,
                                out Mat mask, out System.Drawing.Rectangle zone, out Mat homography, int k, double uniquenessThreshold, SIFTParametrs parametrs)
        {
            VectorSubMapKeyPoint = new VectorOfKeyPoint();
            SubMapDiscriptors    = new Mat();
            matches = new VectorOfVectorOfDMatch();
            zone    = new System.Drawing.Rectangle();
            using (SIFT siftCPU = new SIFT(parametrs.nFeatures, parametrs.nOctaveLayers,
                                           parametrs.contrastThreshold, parametrs.edgeThreshold, parametrs.sigma))
            {
                siftCPU.DetectAndCompute(SubMap, null, VectorSubMapKeyPoint, SubMapDiscriptors, false);
            }
            matches = new VectorOfVectorOfDMatch();
            using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                    using (Emgu.CV.Features2D.DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                    {
                        matcher.Add(SubMapDiscriptors);
                        matcher.KnnMatch(MapDiscriptors, matches, k, null);
                    }

            mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
            mask.SetTo(new MCvScalar(255));
            Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

            homography = null;

            int nonZeroCount = CvInvoke.CountNonZero(mask);

            if (nonZeroCount >= 4)
            {
                nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(VectorSubMapKeyPoint, VectorMapKeyPoint,
                                                                           matches, mask, 1.5, 20);
                if (nonZeroCount >= 4)
                {
                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(
                        VectorSubMapKeyPoint, VectorMapKeyPoint, matches, mask, 2);
                }
            }
        }
Ejemplo n.º 5
0
        public SimpleFeature(SimpleFeatureDesc desc, SIFT featureDetector = null)
        {
            size     = desc.size;
            cropRect = new Rectangle(desc.cropPoint, size);
            mask     = new Mat(desc.size, DepthType.Cv8U, 1);
            features = desc.features;
            CvInvoke.Rectangle(mask, desc.rect, new MCvScalar(255, 255, 255), -1);
            CvInvoke.Circle(mask, desc.maskPoint, 23, new MCvScalar(0, 0, 0), -1);

            //SimpleFeature sf = new SimpleFeature(new Point(842, 646), size, mask);
            if (desc.colorFilter != null)
            {
                SetColorFilter(desc.colorFilter.lowerBound, desc.colorFilter.upperBound);
            }
            //featureDetector = new SIFT(0,2,0.02,20,0.4);
            if (featureDetector == null)
            {
                this.featureDetector = new SIFT(0, 3, 0.04, 10, 0.8);
            }
            Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams();
            Emgu.CV.Flann.SearchParams      sp = new SearchParams();
            matcher = new FlannBasedMatcher(ip, sp);
        }
Ejemplo n.º 6
0
        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lstMat.Clear();
            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();
            Mat testImage  = CvInvoke.Imread(@"Linage2\Main\PartyAuto\2e35av2fwbk.png", ImreadModes.Color);
            Mat modelImage = CVUtil.crop_color_frame(testImage, new Rectangle(842, 646, 70, 70));

            log(modelImage.ToString());
            Image <Bgr, Byte> img = modelImage.ToImage <Bgr, Byte>();

            CvInvoke.cvSetImageROI(img, new Rectangle(0, 0, 35, 35));

            //UMat uModelImage = modelImage.GetUMat(AccessType.Read);
            var featureDetector             = new SIFT();
            Mat modelDescriptors            = new Mat();
            VectorOfKeyPoint modelKeyPoints = new VectorOfKeyPoint();

            VectorOfKeyPoint observedKeyPoints = new VectorOfKeyPoint();

            featureDetector.DetectAndCompute(modelImage, null, modelKeyPoints, modelDescriptors, false);
            log("model size = " + modelKeyPoints.Size);
            Mat observedDescriptors = new Mat();

            featureDetector.DetectAndCompute(testImage, null, observedKeyPoints, observedDescriptors, false);

            int    k = 2;
            double uniquenessThreshold = 0.80;
            Mat    mask;
            Mat    homography = null;

            // Bruteforce, slower but more accurate
            // You can use KDTree for faster matching with slight loss in accuracy
            using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                    using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                    {
                        matcher.Add(modelDescriptors);

                        matcher.KnnMatch(observedDescriptors, matches, k, null);
                        mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                        mask.SetTo(new MCvScalar(255));
                        Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

                        int nonZeroCount = CvInvoke.CountNonZero(mask);
                        if (nonZeroCount >= 4)
                        {
                            nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                                       matches, mask, 1.5, 20);
                            if (nonZeroCount >= 4)
                            {
                                homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints, observedKeyPoints, matches, mask, 2);
                                PointF[] src =
                                {
                                    new PointF(0, 0), new PointF(0, modelImage.Height - 1), new PointF(modelImage.Width - 1, modelImage.Height - 1), new PointF(modelImage.Width - 1, 0)
                                };
                                PointF[] points = CvInvoke.PerspectiveTransform(src, homography);
                                foreach (var p in points)
                                {
                                    Console.WriteLine(p.ToString());
                                }
                                Point[] ap = Array.ConvertAll(points,
                                                              new Converter <PointF, Point>(CVUtil.PointFToPoint));

                                CvInvoke.Polylines(testImage, ap, true, new MCvScalar(255, 0, 0));
                                CvInvoke.Rectangle(testImage, new Rectangle(0, 0, 100, 100), new MCvScalar(255, 255, 0));
                                CvInvoke.Circle(testImage, new Point(100, 100), 50, new MCvScalar(255, 255, 0), -1);
                                lstMat.Add(testImage);
                            }
                            //Mat modelMatches = new Mat();
                            //Features2DToolbox.DrawKeypoints(modelImage, modelKeyPoints, result, new Bgr(Color.Red));
                            //Features2DToolbox.DrawKeypoints(testImage, observedKeyPoints, result, new Bgr(Color.Red));
                            //Features2DToolbox.DrawMatches(modelImage, modelKeyPoints, testImage, observedKeyPoints, matches, modelMatches,
                            //    new MCvScalar(255, 0, 0), new MCvScalar(0, 255, 0));
                            //lstMat.Add(modelMatches);

                            //Mat model1 = new Mat();
                            //Features2DToolbox.DrawKeypoints(modelImage, modelKeyPoints, model1, new Bgr(Color.Red));
                            //lstMat.Add(model1);
                            //modelMatches = crop_color_frame(testImage,new Rectangle(842,646,70,70));
                        }
                    }
            log("Done " + mask.Size);

            Refresh();
        }
        public static void FindMatch(Mat modelImage, Mat observedImage, out long matchTime, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask, out Mat homography, out long score)
        {
            int    k = 2;
            double uniquenessThreshold = 0.80;

            Stopwatch watch;

            homography = null;

            modelKeyPoints    = new VectorOfKeyPoint();
            observedKeyPoints = new VectorOfKeyPoint();

            using (UMat uModelImage = modelImage.GetUMat(AccessType.Read))
                using (UMat uObservedImage = observedImage.GetUMat(AccessType.Read))
                {
                    KAZE featureDetector = new KAZE();

                    //extract features from the object image
                    Mat modelDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                    watch = Stopwatch.StartNew();

                    // extract features from the observed image
                    Mat observedDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);

                    // Bruteforce, slower but more accurate
                    // You can use KDTree for faster matching with slight loss in accuracy
                    using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                        using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                            using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                            {
                                matcher.Add(modelDescriptors);

                                matcher.KnnMatch(observedDescriptors, matches, k, null);
                                mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                                mask.SetTo(new MCvScalar(255));
                                Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);


                                score = 0;
                                int nonZeroCount = CvInvoke.CountNonZero(mask);
                                if (nonZeroCount >= 4)
                                {
                                    nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                                               matches, mask, 1.5, 20);
                                    if (nonZeroCount >= 4)
                                    {
                                        homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                                              observedKeyPoints, matches, mask, 2);

                                        // Calculate score based on matches size
                                        // ---------------------------------------------->

                                        for (int i = 0; i < matches.Size; i++)
                                        {
                                            //if (mask.GetData(i)[0] == 0) continue;
                                            //foreach (var e in matches[i].ToArray())
                                            //    ++score;
                                        }
                                        // <----------------------------------------------
                                    }
                                }
                            }
                    watch.Stop();
                }
            matchTime = watch.ElapsedMilliseconds;
        }
Ejemplo n.º 8
0
        public static void FindMatch(Mat modelImage, Mat modelImage2, Mat observedImage,
                                     out long matchTime, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask, out Mat homography,
                                     out VectorOfKeyPoint modelKeyPoints2, VectorOfVectorOfDMatch matches2, out Mat mask2, out Mat homography2)
        {
            int    k = 2;
            double uniquenessThreshold = 0.80;

            Stopwatch watch;

            homography  = null;
            homography2 = null;

            modelKeyPoints    = new VectorOfKeyPoint();
            modelKeyPoints2   = new VectorOfKeyPoint();
            observedKeyPoints = new VectorOfKeyPoint();

            using (UMat uModelImage = modelImage.GetUMat(AccessType.Read)) // Создаем объект модели изображения
                using (UMat uModelImage2 = modelImage2.GetUMat(AccessType.Read))
                    using (UMat uObservedImage = observedImage.GetUMat(AccessType.Read))
                    {
                        KAZE featureDetector = new KAZE();

                        //извлекаем точки интереса из изображения объекта
                        Mat modelDescriptors = new Mat();
                        featureDetector.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                        Mat modelDescriptors2 = new Mat();
                        featureDetector.DetectAndCompute(uModelImage2, null, modelKeyPoints2, modelDescriptors2, false);

                        watch = Stopwatch.StartNew();

                        // извлекаем точки интереса из исследуемого изображения
                        Mat observedDescriptors = new Mat();
                        featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);


                        // Bruteforce, slower but more accurate
                        // You can use KDTree for faster matching with slight loss in accuracy
                        using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                            using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                                using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                                {
                                    matcher.Add(modelDescriptors);

                                    matcher.KnnMatch(observedDescriptors, matches, k, null);
                                    mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                                    mask.SetTo(new MCvScalar(255));
                                    Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

                                    int nonZeroCount = CvInvoke.CountNonZero(mask);
                                    if (nonZeroCount >= 4)
                                    {
                                        nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                                                   matches, mask, 1.5, 20);
                                        if (nonZeroCount >= 4)
                                        {
                                            homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                                                  observedKeyPoints, matches, mask, 2);
                                        }
                                    }
                                }

                        using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                            using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                                using (DescriptorMatcher matcher2 = new FlannBasedMatcher(ip, sp))
                                {
                                    matcher2.Add(modelDescriptors2);

                                    matcher2.KnnMatch(observedDescriptors, matches2, k, null);
                                    mask2 = new Mat(matches2.Size, 1, DepthType.Cv8U, 1);
                                    mask2.SetTo(new MCvScalar(255));
                                    Features2DToolbox.VoteForUniqueness(matches2, uniquenessThreshold, mask2);

                                    int nonZeroCount = CvInvoke.CountNonZero(mask2);
                                    if (nonZeroCount >= 4)
                                    {
                                        nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints2, observedKeyPoints,
                                                                                                   matches2, mask2, 1.5, 20);
                                        if (nonZeroCount >= 4)
                                        {
                                            homography2 = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints2,
                                                                                                                   observedKeyPoints, matches2, mask2, 2);
                                        }
                                    }
                                }


                        watch.Stop();
                    }
            matchTime = watch.ElapsedMilliseconds;
        }
Ejemplo n.º 9
0
      public void TestIndex3D()
      {
         Random r = new Random();
         MCvPoint3D32f[] points = new MCvPoint3D32f[1000];

         for (int i = 0; i < points.Length; i++)
         {
            points[i].X = (float) r.NextDouble();
            points[i].Y = (float) r.NextDouble();
            points[i].Z = (float) r.NextDouble();
         }

         MCvPoint3D32f searchPoint = new MCvPoint3D32f();
         searchPoint.X = (float) r.NextDouble();
         searchPoint.Y = (float) r.NextDouble();
         searchPoint.Z = (float) r.NextDouble();

         int indexOfClosest1 = 0;
         double shortestDistance1 = double.MaxValue;
         for (int i = 0; i < points.Length; i++)
         {
            double dist = (searchPoint - points[i]).Norm;
            if (dist < shortestDistance1)
            {
               shortestDistance1 = dist;
               indexOfClosest1 = i;
            }
         }
         Flann.LinearIndexParams p = new LinearIndexParams();
         Flann.Index3D index3D = new Emgu.CV.Flann.Index3D(points,p);
         double shortestDistance2;
         int indexOfClosest2 = index3D.ApproximateNearestNeighbour(searchPoint, out shortestDistance2);
         shortestDistance2 = Math.Sqrt(shortestDistance2);

         EmguAssert.IsTrue(indexOfClosest1 == indexOfClosest2);
         EmguAssert.IsTrue((shortestDistance1 - shortestDistance2) <= 1.0e-3 * shortestDistance1);
      }
Ejemplo n.º 10
0
      public void TestFlannLinear()
      {
         float[][] features = new float[10][];
         for (int i = 0; i < features.Length; i++)
            features[i] = new float[] { (float) i };
         Flann.LinearIndexParams p = new LinearIndexParams();
         Flann.Index index = new Flann.Index(CvToolbox.GetMatrixFromArrays(features), p);

         float[][] features2 = new float[1][];
         features2[0] = new float[] { 5.0f };

         Matrix<int> indices = new Matrix<int>(features2.Length, 1);
         Matrix<float> distances = new Matrix<float>(features2.Length, 1);
         index.KnnSearch(CvToolbox.GetMatrixFromArrays(features2), indices, distances, 1, 32);

         EmguAssert.IsTrue(indices[0, 0] == 5);
         EmguAssert.IsTrue(distances[0, 0] == 0.0);
      }
Ejemplo n.º 11
0
        public Bitmap DrawSift(Image <Rgb, byte> modelimage, Image <Rgb, byte> observedimage)
        {
            int    k = 2;
            double uniquenessThreshold = 0.80;


            VectorOfKeyPoint modelKeyPoints    = new VectorOfKeyPoint(),
                             observedKeyPoints = new VectorOfKeyPoint();

            Mat modeldiscriptors    = new Mat();
            Mat observeddiscriptors = new Mat();

            //observedKeyPoints = observedKeyPoints.Resize(1.0 / Compression, Inter.Area);
            using (SIFT siftCPU = new SIFT(0, 5, 0.04, 10.0, 1.6))
            {
                siftCPU.DetectAndCompute(modelimage, null, modelKeyPoints, modeldiscriptors, false);
                observedKeyPoints = new VectorOfKeyPoint(siftCPU.Detect(observedimage));
                siftCPU.Compute(observedimage, observedKeyPoints, observeddiscriptors);
            }
            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

            using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                    using (Emgu.CV.Features2D.DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                    {
                        matcher.Add(modeldiscriptors);
                        matcher.KnnMatch(observeddiscriptors, matches, k, null);
                    }

            Mat mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);

            mask.SetTo(new MCvScalar(255));
            Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

            Mat homography = null;

            int nonZeroCount = CvInvoke.CountNonZero(mask);

            if (nonZeroCount >= 4)
            {
                nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, observedKeyPoints,
                                                                           matches, mask, 1.5, 20);
                if (nonZeroCount >= 4)
                {
                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                          observedKeyPoints, matches, mask, 2);
                }
            }

            observedimage = new Image <Rgb, byte>(DrawZone(observedimage.Mat, observedKeyPoints, matches, mask).Bitmap);
            //modelKeyPoints.FilterByPixelsMask(new Image<Gray, byte>(mask.Bitmap));
            //observedKeyPoints.FilterByPixelsMask(new Image<Gray, byte>(mask.Bitmap));

            Mat result = new Mat();

            //Draw the matched keypoints
            Features2DToolbox.DrawMatches(modelimage, modelKeyPoints, observedimage, observedKeyPoints,
                                          matches, result, new MCvScalar(0, 255, 0), new MCvScalar(255, 0, 0), mask);
            if (homography != null)
            {
                //draw a rectangle along the projected model
                SD.Rectangle rect = new SD.Rectangle(SD.Point.Empty, modelimage.Size);
                PointF[]     pts  = new PointF[]
                {
                    new PointF(rect.Left, rect.Bottom),
                    new PointF(rect.Right, rect.Bottom),
                    new PointF(rect.Right, rect.Top),
                    new PointF(rect.Left, rect.Top)
                };
                pts = CvInvoke.PerspectiveTransform(pts, homography);

#if NETFX_CORE
                Point[] points = Extensions.ConvertAll <PointF, Point>(pts, Point.Round);
#else
                SD.Point[] points = Array.ConvertAll <PointF, SD.Point>(pts, SD.Point.Round);
#endif
                using (VectorOfPoint vp = new VectorOfPoint(points))
                {
                    CvInvoke.Polylines(result, vp, true, new MCvScalar(0, 0, 255), 2);
                }
            }

            return(result.Bitmap);
        }
Ejemplo n.º 12
0
        public static VectorOfPoint Process(Mat logo, Mat observedImage)
        {
            VectorOfPoint    vp                = null;
            Mat              homography        = null;
            VectorOfKeyPoint logoKeyPoints     = new VectorOfKeyPoint();
            VectorOfKeyPoint observedKeyPoints = new VectorOfKeyPoint();
            Mat              mask;
            int              k = 2;
            double           uniquenessThreshold = 0.80;

            using (VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch())
            {
                using (UMat uModelImage = logo.GetUMat(AccessType.Read))
                    using (UMat uObservedImage = observedImage.GetUMat(AccessType.Read))
                    {
                        KAZE featureDetector = new KAZE();

                        //extract features from the object image
                        Mat modelDescriptors = new Mat();
                        featureDetector.DetectAndCompute(uModelImage, null, logoKeyPoints, modelDescriptors, false);


                        // extract features from the observed image
                        Mat observedDescriptors = new Mat();
                        featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);

                        // Bruteforce, slower but more accurate
                        // You can use KDTree for faster matching with slight loss in accuracy
                        using (Emgu.CV.Flann.LinearIndexParams ip = new Emgu.CV.Flann.LinearIndexParams())
                            using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                                using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                                {
                                    matcher.Add(modelDescriptors);

                                    matcher.KnnMatch(observedDescriptors, matches, k, null);
                                    mask = new Mat(matches.Size, 1, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                                    mask.SetTo(new MCvScalar(255));
                                    Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, mask);

                                    int nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(logoKeyPoints, observedKeyPoints,
                                                                                                   matches, mask, 1.5, 20);
                                    if (nonZeroCount >= 4)
                                    {
                                        homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(logoKeyPoints,
                                                                                                              observedKeyPoints, matches, mask, 2);
                                    }
                                }
                    }

                if (homography != null)
                {
                    //draw a rectangle along the projected model
                    Rectangle rect = new Rectangle(Point.Empty, logo.Size);
                    PointF[]  pts  = new PointF[]
                    {
                        new PointF(rect.Left, rect.Bottom),
                        new PointF(rect.Right, rect.Bottom),
                        new PointF(rect.Right, rect.Top),
                        new PointF(rect.Left, rect.Top)
                    };

                    pts = CvInvoke.PerspectiveTransform(pts, homography);
                    Point[] points = Array.ConvertAll <PointF, Point>(pts, Point.Round);
                    vp = new VectorOfPoint(points);
                }
                return(vp);
            }
        }