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
        /// <summary>
        ///
        /// </summary>
        /// <param name="observedKeyPoints"></param>
        /// <param name="modelKeyPoint"></param>
        /// <returns></returns>
        public static VectorOfPoint Matching(VectorOfKeyPoint observedKeyPoints, VectorOfKeyPoint modelKeyPoints, Mat observedDescriptors, Mat modelDescriptors, Size modelSize)
        {
            KAZE featureDetector = new KAZE();

            using (var ip = new LinearIndexParams())
                using (var sp = new SearchParams())
                    using (var matcher = new FlannBasedMatcher(ip, sp))
                        using (var matches = new VectorOfVectorOfDMatch())
                        {
                            Mat homography = new Mat();
                            matcher.Add(modelDescriptors);

                            matcher.KnnMatch(observedDescriptors, matches, 2, null);
                            var mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                            mask.SetTo(new MCvScalar(255));
                            Features2DToolbox.VoteForUniqueness(matches, 0.80, 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);
                                }
                            }

                            if (homography != null && !homography.Size.IsEmpty)
                            {
                                Rectangle rect = new Rectangle(Point.Empty, modelSize);
                                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);

                                return(new VectorOfPoint(points));
                            }
                            else
                            {
                                return(new VectorOfPoint());
                            }
                        }
        }
        /// <summary>
        /// Match this sign to an other sign
        /// </summary>
        /// <param name="otherSign"> other traffic sign </param>
        /// <returns> matched features as matches </returns>
        public VectorOfVectorOfDMatch MatchToOtherSign(TrafficSign otherSign)
        {
            if (!IsKnownSign)
            {
                throw new Exception("Only known signs support matching.");
            }
            if (otherSign.Features.Cols < 1)
            {
                throw new Exception("Other sign has no descriptors to match.");
            }
            if (Features.Cols < 1)
            {
                throw new Exception("Current sign has no features to match.");
            }
            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

            matcher.KnnMatch(otherSign.features, matches, 2, null);
            return(matches);
        }
Ejemplo n.º 5
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.º 6
0
        public VectorOfVectorOfDMatch GetMatchesForModel(SurfData scene, SurfData model)
        {
            using (FlannBasedMatcher matcher =
                       new FlannBasedMatcher(_hierarchicalParams, _searchParams))
            {
                matcher.Add(model.Descriptors);

                VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();
                matcher.KnnMatch(scene.Descriptors, matches, _kConstant, null);

                MDMatch[][] newMatches = matches
                                         .ToArrayOfArray()
                                         .OrderBy(m => m[0].Distance)
                                         .Take(_matchLimit)
                                         .ToArray();

                VectorOfVectorOfDMatch limitMatches = new VectorOfVectorOfDMatch(newMatches);
                matches.Dispose();
                return(limitMatches);
            }
        }
Ejemplo n.º 7
0
        private void find(Mat modelImage, out VectorOfKeyPoint modelKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask, out Mat homography)
        {
            int    k = 2;
            double uniquenessThreshold = 0.70;

            homography = null;

            VectorOfKeyPoint currentKeyPoints = new VectorOfKeyPoint();
            Mat currentDescriptors            = new Mat();

            detector.DetectAndCompute(current, null, currentKeyPoints, currentDescriptors, false);

            modelKeyPoints = new VectorOfKeyPoint();
            Mat modelDescriptors = new Mat();

            detector.DetectAndCompute(modelImage, null, modelKeyPoints, modelDescriptors, false);

            LinearIndexParams ip      = new LinearIndexParams();
            SearchParams      sp      = new SearchParams();
            DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp);

            matcher.Add(modelDescriptors);

            matcher.KnnMatch(currentDescriptors, 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, currentKeyPoints, matches, mask, 1.5, 20);
                if (nonZeroCount >= 4)
                {
                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints, currentKeyPoints, matches, mask, 2);
                }
            }
        }
Ejemplo n.º 8
0
        private static VectorOfVectorOfDMatch GetSceneMatchesForModel(SURFData sceneData, SURFData modelData)
        {
            FlannBasedMatcher matcher =
                new FlannBasedMatcher(new HierarchicalClusteringIndexParams(), new SearchParams());

            matcher.Add(modelData.Descriptors);

            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

            matcher.KnnMatch(sceneData.Descriptors, matches, 1, null);

            MDMatch[][] newMatches = matches
                                     .ToArrayOfArray()
                                     .OrderBy(m => m[0].Distance)
                                     .Take(8)
                                     .ToArray();

            VectorOfVectorOfDMatch limitMatches = new VectorOfVectorOfDMatch(newMatches);

            matches.Dispose();
            return(limitMatches);
        }
Ejemplo n.º 9
0
        public override void Process(Image <Bgr, byte> image, out Image <Bgr, byte> annotatedImage, out List <object> data)
        {
            base.Process(image, out annotatedImage, out data);

            using (var detector = GetDetector())
                using (var modelKeyPoints = new VectorOfKeyPoint())
                    using (var imageKeyPoints = new VectorOfKeyPoint())
                        using (var modelDescriptors = new Mat())
                            using (var imageDescriptors = new Mat())
                                using (var flannMatcher = new FlannBasedMatcher(GetIndexParams(), new SearchParams()))
                                    using (var bfMatcher = new BFMatcher(_distanceType))
                                        using (var matches = new VectorOfVectorOfDMatch())
                                        {
                                            // get features from image
                                            detector.DetectAndCompute(
                                                image.Convert <Gray, byte>(),
                                                null,
                                                imageKeyPoints,
                                                imageDescriptors,
                                                false);

                                            // optionally view image keypoints and return
                                            if (_showKeypoints)
                                            {
                                                Features2DToolbox.DrawKeypoints(
                                                    annotatedImage,
                                                    imageKeyPoints,
                                                    annotatedImage,
                                                    new Bgr(_annoColor.Color()),
                                                    Features2DToolbox.KeypointDrawType.DrawRichKeypoints);
                                                data = new List <object>();
                                                data.AddRange(imageKeyPoints.ToArray().Select(k => new KeyPoint(k)));
                                                return;
                                            }

                                            // do not proceed if there is no template
                                            if (Template == null)
                                            {
                                                return;
                                            }

                                            // get features from object
                                            detector.DetectAndCompute(
                                                Template.Convert <Gray, byte>(),
                                                null,
                                                modelKeyPoints,
                                                modelDescriptors,
                                                false);

                                            // perform match with selected matcher
                                            if (_matcherType == MatcherType.Flann)
                                            {
                                                flannMatcher.Add(modelDescriptors);
                                                flannMatcher.KnnMatch(
                                                    imageDescriptors,
                                                    matches,
                                                    2,
                                                    null);
                                            }
                                            else
                                            {
                                                bfMatcher.Add(modelDescriptors);
                                                bfMatcher.KnnMatch(
                                                    imageDescriptors,
                                                    matches,
                                                    2,
                                                    null);
                                            }

                                            // find homography
                                            using (var mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1))
                                            {
                                                Mat homography = null;

                                                // filter for unique matches
                                                mask.SetTo(new MCvScalar(255));
                                                Features2DToolbox.VoteForUniqueness(matches, 0.8, mask);

                                                // if 4 or more patches continue
                                                var nonZeroCount = CvInvoke.CountNonZero(mask);
                                                if (nonZeroCount >= 4)
                                                {
                                                    // filter for majority scale and rotation
                                                    nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(modelKeyPoints, imageKeyPoints,
                                                                                                               matches, mask, 1.5, 20);

                                                    // if 4 or more patches continue
                                                    if (nonZeroCount >= 4)
                                                    {
                                                        // get the homography
                                                        homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                                                                              imageKeyPoints, matches, mask, 2);
                                                    }
                                                }

                                                // if no homography, return
                                                if (homography == null)
                                                {
                                                    return;
                                                }

                                                // initialize a rectangle of the template size
                                                var rect = new Rectangle(Point.Empty, Template.Size);

                                                // create points array for the vertices of the template
                                                var pts = new[]
                                                {
                                                    new PointF(rect.Left, rect.Bottom),
                                                    new PointF(rect.Right, rect.Bottom),
                                                    new PointF(rect.Right, rect.Top),
                                                    new PointF(rect.Left, rect.Top)
                                                };

                                                // transform the perspective of the points array based on the homography
                                                // and get a rotated rectangle for the homography
                                                pts = CvInvoke.PerspectiveTransform(pts, homography);
                                                var rotRect = CvInvoke.MinAreaRect(pts);

                                                // annotate the image and return the rotated rectangle model
                                                annotatedImage.Draw(rotRect, new Bgr(_annoColor.Color()), _lineThick);
                                                data = new List <object> {
                                                    new RotatedBox(rotRect)
                                                };
                                            }
                                        }
        }
Ejemplo n.º 10
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);
            }
        }
        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();

                    Mat modelDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                    watch = Stopwatch.StartNew();

                    Mat observedDescriptors = new Mat();
                    featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);

                    // KdTree for faster results / less accuracy
                    using (var ip = new Emgu.CV.Flann.KdTreeIndexParams())
                        using (var 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);

                                // Calculate score based on match size
                                score = 0;
                                for (int i = 0; i < matches.Size; i++)
                                {
                                    if (mask.GetData(i)[0] == 0)
                                    {
                                        continue;
                                    }
                                    foreach (var e in matches[i].ToArray())
                                    {
                                        ++score;
                                    }
                                }

                                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();
                }
            matchTime = watch.ElapsedMilliseconds;
        }
Ejemplo n.º 12
0
        //public static long Classify(VectorOfKeyPoint modelKeyPoints, Mat modelDescriptors, Mat observedImage, double uniquenessThreshold, int k, int detectionType)
        public static long Classify(Mat modelDescriptors, Mat observedImage, double uniquenessThreshold, int k, int detectionType)
        {
            var score = 0L;

            using (var matches = new VectorOfVectorOfDMatch())
            {
                Mat mask = null;
                //Mat homography = null;
                var observedKeyPoints = new VectorOfKeyPoint();
                var obsImage          = new Mat();
                CvInvoke.Threshold(observedImage, obsImage, 127.0, 255.0, ThresholdType.BinaryInv);
                using (UMat uObservedImage = obsImage.GetUMat(AccessType.Read))
                {
                    switch (detectionType)
                    {
                    default:
                        using (var featureDetector = new SIFT(0, 3, 0.04, 10.0, 1.6))
                        {
                            var observedDescriptors = new Mat();
                            featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
                            using (var ip = new KdTreeIndexParams())
                                using (var 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;
                                        for (int i = 0; i < matches.Size; i++)
                                        {
                                            if (mask.GetData(i)[0] == 0)
                                            {
                                                continue;
                                            }
                                            foreach (var e in matches[i].ToArray())
                                            {
                                                ++score;
                                            }
                                        }
                                        //var 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);
                                        //}
                                    }
                        }
                        break;

                    case 1:
                        using (var featureDetector = new KAZE())
                        {
                            var observedDescriptors = new Mat();
                            featureDetector.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
                            using (var ip = new KdTreeIndexParams())
                                using (var 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;
                                        for (int i = 0; i < matches.Size; i++)
                                        {
                                            if (mask.GetData(i)[0] == 0)
                                            {
                                                continue;
                                            }
                                            foreach (var e in matches[i].ToArray())
                                            {
                                                ++score;
                                            }
                                        }
                                        //var 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);
                                        //}
                                    }
                        }
                        break;
                    }
                }
            }
            return(score);
        }
Ejemplo n.º 13
0
        private static VectorOfPoint ProcessImageFLANN(Image <Gray, byte> template, Image <Gray, byte> sceneImage)
        {
            try
            {
                // initialization
                VectorOfPoint    finalPoints        = null;
                Mat              homography         = null;
                VectorOfKeyPoint templateKeyPoints  = new VectorOfKeyPoint();
                VectorOfKeyPoint sceneKeyPoints     = new VectorOfKeyPoint();
                Mat              tempalteDescriptor = new Mat();
                Mat              sceneDescriptor    = new Mat();

                Mat    mask;
                int    k = 2;
                double uniquenessthreshold     = 0.80;
                VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

                // feature detectino and description
                KAZE featureDetector = new KAZE();
                featureDetector.DetectAndCompute(template, null, templateKeyPoints, tempalteDescriptor, false);
                featureDetector.DetectAndCompute(sceneImage, null, sceneKeyPoints, sceneDescriptor, false);


                // Matching

                //KdTreeIndexParams ip = new KdTreeIndexParams();
                //var ip = new AutotunedIndexParams();
                var               ip      = new LinearIndexParams();
                SearchParams      sp      = new SearchParams();
                FlannBasedMatcher matcher = new FlannBasedMatcher(ip, sp);


                matcher.Add(tempalteDescriptor);
                matcher.KnnMatch(sceneDescriptor, matches, k);

                mask = new Mat(matches.Size, 1, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                mask.SetTo(new MCvScalar(255));

                Features2DToolbox.VoteForUniqueness(matches, uniquenessthreshold, mask);

                int count = Features2DToolbox.VoteForSizeAndOrientation(templateKeyPoints, sceneKeyPoints, matches, mask, 1.5, 20);

                if (count >= 4)
                {
                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(templateKeyPoints,
                                                                                          sceneKeyPoints, matches, mask, 5);
                }

                if (homography != null)
                {
                    System.Drawing.Rectangle rect = new System.Drawing.Rectangle(System.Drawing.Point.Empty, template.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);
                    System.Drawing.Point[] points = Array.ConvertAll <PointF, System.Drawing.Point>(pts, System.Drawing.Point.Round);
                    finalPoints = new VectorOfPoint(points);
                }

                return(finalPoints);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Ejemplo n.º 14
0
        public FeatureResult GetFeature(Bitmap bmpSrc)
        {
            Mat matTest = CVUtil.BitmapToMat(bmpSrc);

            matTest = ProcessImage(matTest);

            /*Bitmap bmp = Utils.cropImage(bmpSrc, cropRect);
             * Mat matTest = CVUtil.BitmapToMat(bmp);
             * if (colorIndex != -1)
             * {
             *  matTest = matTest.Split()[colorIndex];
             * }*/
            Mat observedDescriptors            = new Mat();
            VectorOfKeyPoint observedKeyPoints = new VectorOfKeyPoint();

            featureDetector.DetectAndCompute(matTest, mask, observedKeyPoints, observedDescriptors, false);
            int    k = 2;
            double uniquenessThreshold = 0.80;

            //Mat homography = null;
            // Bruteforce, slower but more accurate
            // You can use KDTree for faster matching with slight loss in accuracy
            using (Emgu.CV.Flann.KdTreeIndexParams ip = new Emgu.CV.Flann.KdTreeIndexParams())
                using (Emgu.CV.Flann.SearchParams sp = new SearchParams())
                    using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                    {
                        VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();
                        foreach (SimpleFeatureData sd in this)
                        {
                            matcher.Add(sd.descriptors);
                            //break;
                        }

                        matcher.KnnMatch(observedDescriptors, matches, k, null);
                        lastMatches          = matches;
                        lastObserved         = matTest;
                        lastObservedKeyPoint = observedKeyPoints;
                        //Mat mat = new Mat();
                        //Features2DToolbox.DrawKeypoints(matTest, observedKeyPoints, mat, new Bgr(Color.Blue));
                        //FormOpenCV.lstMat.Add(mat);
                        //Console.WriteLine(CVUtil.ToString(observedDescriptors));
                        //Console.WriteLine(CVUtil.ToString(observedKeyPoints));
                        //Console.WriteLine(CVUtil.ToString(matches));
                        //Console.WriteLine(MatchesToString(matches));
                        Mat uniqueMask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                        uniqueMask.SetTo(new MCvScalar(255));
                        Features2DToolbox.VoteForUniqueness(matches, uniquenessThreshold, uniqueMask);

                        int nonZeroCount = CvInvoke.CountNonZero(uniqueMask);
                        if (nonZeroCount > 4)
                        {
                            //Console.WriteLine(CVUtil.ToString(uniqueMask));
                            String            retLabel = GetLabelFromMatches(matches, uniqueMask);
                            SimpleFeatureData mfd      = lastMatchFeatureData;
                            try
                            {
                                //int nonZeroCount2 = Features2DToolbox.VoteForSizeAndOrientation(mfd.keyPoints, observedKeyPoints, matches, uniqueMask, 1.5, 20);
                                //Console.WriteLine("nonZeroCount2=" + nonZeroCount2);
                                if (nonZeroCount > 4)
                                {
                                    Mat homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(mfd.keyPoints, observedKeyPoints, matches, uniqueMask, 2);

                                    /*Console.WriteLine(CVUtil.ToString(homography));
                                     * Rectangle rect = CVUtil.GetRect(mfd.keyPoints);
                                     * PointF[] src = {
                                     *  new PointF(rect.X,rect.Y),new PointF(rect.X,rect.Y + rect.Height-1),
                                     *  new PointF(rect.X + rect.Width-1,rect.Y + rect.Height-1),new PointF(rect.X + rect.Width-1,rect.Y)
                                     * };
                                     * 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));
                                     * Mat testImage = matTest.Clone();
                                     * 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);
                                    FeatureResult ret = new FeatureResult();
                                    ret.keyPoint         = observedKeyPoints;
                                    ret.label            = retLabel;
                                    ret.homography       = homography;
                                    ret.matchFeatureData = mfd;
                                    return(ret);
                                }
                            }catch (Exception ex)
                            {
                            }
                        }
                        return(null);
                    }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// The method used to discover similarities amongst the images, and populating arrays.
        /// </summary>
        /// <param name="m_modelImage"> The model image (library basic). </param>
        /// <param name="m_observedImage"> The observed image (test).  </param>
        /// <param name="d_matchTime"> The output total time for computing the homography matrix. </param>
        /// <param name="v_modelKeyPoints"></param>
        /// <param name="v_observedKeyPoints"></param>
        /// <param name="v_matches"></param>
        /// <param name="m_mask"></param>
        /// <param name="m_homography"></param>
        /// <param name="l_score"> Field contains the score of matching. </param>
        public static void FindMatch(Mat m_modelImage, Mat m_observedImage, out double d_matchTime, out VectorOfKeyPoint v_modelKeyPoints,
                                     out VectorOfKeyPoint v_observedKeyPoints, VectorOfVectorOfDMatch v_matches, out Mat m_mask,
                                     out Mat m_homography, out long l_score)
        {
            ErrInfLogger.LockInstance.InfoLog("Start of the FindMatch");

            TimerAbstraction _tim = new TimerRefinedAbstraction();

            _tim._iTimer = new TimerFractional();

            m_homography = null;

            v_modelKeyPoints    = new VectorOfKeyPoint();
            v_observedKeyPoints = new VectorOfKeyPoint();

            KAZE featureDetector = new KAZE();

            Mat modelDescriptors = new Mat();

            featureDetector.DetectAndCompute(m_modelImage, null, v_modelKeyPoints, modelDescriptors, false);

            _tim.MeasureStart();

            Mat observedDescriptors = new Mat();

            featureDetector.DetectAndCompute(m_observedImage, null, v_observedKeyPoints, observedDescriptors, false);

            // KdTree for faster results / less accuracy
            using (KdTreeIndexParams ip = new KdTreeIndexParams())
                using (SearchParams sp = new SearchParams())
                    using (DescriptorMatcher matcher = new FlannBasedMatcher(ip, sp))
                    {
                        matcher.Add(modelDescriptors);

                        matcher.KnnMatch(observedDescriptors, v_matches, SettingsContainer.Instance.i_K, null);
                        m_mask = new Mat(v_matches.Size, 1, DepthType.Cv8U, 1);
                        m_mask.SetTo(new MCvScalar(255));
                        Features2DToolbox.VoteForUniqueness(v_matches, SettingsContainer.Instance.d_UniquenessThreshold, m_mask);

                        // Calculate score based on matches size
                        // ---------------------------------------------->
                        l_score = 0;
                        for (int i = 0; i < v_matches.Size; i++)
                        {
                            if (m_mask.GetData(i)[0] == 0)
                            {
                                continue;
                            }
                            foreach (var e in v_matches[i].ToArray())
                            {
                                ++l_score;
                            }
                        }
                        // <----------------------------------------------

                        int nonZeroCount = CvInvoke.CountNonZero(m_mask);
                        if (nonZeroCount >= 4)
                        {
                            nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(v_modelKeyPoints, v_observedKeyPoints, v_matches,
                                                                                       m_mask, 1.5, 20);
                            if (nonZeroCount >= 4)
                            {
                                m_homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(v_modelKeyPoints, v_observedKeyPoints,
                                                                                                        v_matches, m_mask, 2);
                            }
                        }
                    }
            _tim.MeasureStop();
            d_matchTime = Math.Round(_tim.MeasureResult().TotalMilliseconds, 2);
            _tim.MeasureRestart();

            ErrInfLogger.LockInstance.InfoLog("End of the FindMatch");
        }
Ejemplo n.º 16
0
        public void MatchBySurf(string filepath)
        {
            Mat src1 = new Mat();

            originalImg.CopyTo(src1);
            Mat src2 = new Mat(filepath);

            //크기 조정
            //Cv2.Resize(src2, src2, new Size(src2.Size().Width / 2, src2.Size().Height / 2));
            //Cv2.Resize(src1, src1, new Size(src2.Size().Width / 2, src2.Size().Height / 2));


            var gray1 = new Mat();
            var gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversionCodes.BGR2GRAY);
            Cv2.CvtColor(src2, gray2, ColorConversionCodes.BGR2GRAY);

            var surf = SURF.Create(1000);

            // Detect the keypoints and generate their descriptors using SURF
            KeyPoint[] keypoints1, keypoints2;
            var        descriptors1 = new Mat();
            var        descriptors2 = new Mat();

            surf.DetectAndCompute(gray1, null, out keypoints1, descriptors1);
            surf.DetectAndCompute(gray2, null, out keypoints2, descriptors2);

            // Match descriptor vectors

            OpenCvSharp.Flann.SearchParams sParam = new OpenCvSharp.Flann.SearchParams(50);
            var flannMatcher = new FlannBasedMatcher(null, sParam);

            DMatch[][] flannMatches = flannMatcher.KnnMatch(descriptors1, descriptors2, 2);

            double max_dist = 0;
            double min_dist = 100;

            for (int i = 0; i < descriptors1.Rows; i++)
            {
                double dist = flannMatches[i][1].Distance;
                if (dist < min_dist)
                {
                    min_dist = dist;
                }
                if (dist > max_dist)
                {
                    max_dist = dist;
                }
            }

            //find goodMatches
            float         ratio_thresh = 3 * (float)min_dist;
            List <DMatch> goodMatches  = new List <DMatch>();

            for (int i = 0; i < flannMatches.Length; i++)
            {
                if ((flannMatches[i][0].Distance < ratio_thresh)) //* flannMatches[i][1].Distance))// && (flannMatches[i][0].Distance > 0.4 * flannMatches[i][1].Distance))
                {
                    goodMatches.Add(flannMatches[i][0]);
                }
            }

            // Draw matches
            var flannView = new Mat();

            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, goodMatches, flannView, new Scalar(0, 255, 0), new Scalar(255, 255, 0), null, DrawMatchesFlags.Default);

            List <Point2f> obj   = new List <Point2f>();
            List <Point2f> scene = new List <Point2f>();

            if (goodMatches.Count == 0)
            {
                return;
            }

            for (int i = 0; i < goodMatches.Count; i++)
            {
                obj.Add(keypoints1[goodMatches[i].QueryIdx].Pt);
                scene.Add(keypoints2[goodMatches[i].TrainIdx].Pt);
            }

            List <Point2d> obj_corners = new List <Point2d>();

            obj_corners.Add(new Point(0, 0));
            obj_corners.Add(new Point(gray1.Cols, 0));
            obj_corners.Add(new Point(gray1.Cols, gray1.Rows));
            obj_corners.Add(new Point(0, gray1.Rows));
            List <Point2d> scene_corners = new List <Point2d>();

            Mat H = Cv2.FindHomography(obj.ConvertAll(Point2fToPoint2d), scene.ConvertAll(Point2fToPoint2d), HomographyMethods.Ransac);

            if (H.Empty())
            {
                return;
            }
            scene_corners = Cv2.PerspectiveTransform(obj_corners, H).ToList();

            Mat h2 = Cv2.GetPerspectiveTransform(scene_corners.ConvertAll(Point2dToPoint2f), obj_corners.ConvertAll(Point2dToPoint2f));

            Mat crop = new Mat();

            Cv2.WarpPerspective(src2, crop, h2, src1.Size());
            //Cv2.ImShow("original", src1);
            new Window("Cropped", WindowMode.AutoSize, crop);

            crop.CopyTo(processImg);
        }
Ejemplo n.º 17
0
        public AlgorithmResult DetectFeatureMatch(
            string modelName,
            string observedeName,
            FeatureDetectType detectType,
            FeatureMatchType matchType,
            int k,
            double uniquenessThreshold)
        {
            AlgorithmResult   result        = new AlgorithmResult();
            Image <Bgr, byte> modelImage    = ImageHelper.GetImage(modelName);
            Image <Bgr, byte> observedImage = ImageHelper.GetImage(observedeName);
            Mat resultImage = new Mat();

            // Get features from modelImage
            var modelKeyPoints   = new VectorOfKeyPoint();
            var modelDescriptors = new UMat();

            GetDetector(detectType).DetectAndCompute(
                modelImage.Convert <Gray, byte>(),
                null,
                modelKeyPoints,
                modelDescriptors,
                false);

            // Get features from observedImage
            var observedKeyPoints   = new VectorOfKeyPoint();
            var observedDescriptors = new UMat();

            GetDetector(detectType).DetectAndCompute(
                observedImage.Convert <Gray, byte>(),
                null,
                observedKeyPoints,
                observedDescriptors,
                false);

            // Perform match with selected matcher
            var matches = new VectorOfVectorOfDMatch();

            if (matchType == FeatureMatchType.Flann)
            {
                // TODO: Add parameters for GetIndexParams
                var flannMatcher = new FlannBasedMatcher(new AutotunedIndexParams(), new SearchParams());
                flannMatcher.Add(modelDescriptors);
                flannMatcher.KnnMatch(
                    observedDescriptors,
                    matches,
                    k,
                    null);
            }
            else
            {
                // TODO: Add parameters for DistanceType
                var bfMatcher = new BFMatcher(DistanceType.L2);
                bfMatcher.Add(modelDescriptors);
                bfMatcher.KnnMatch(
                    observedDescriptors,
                    matches,
                    k,
                    null);
            }

            // Find homography
            Mat homography = null;
            var mask       = new Mat(matches.Size, 1, DepthType.Cv8U, 1);

            mask.SetTo(new MCvScalar(255));

            VoteForUniqueness(matches, uniquenessThreshold, mask);

            // If 4 or more patches continue
            int nonZeroCount = CvInvoke.CountNonZero(mask);

            if (nonZeroCount >= 4)
            {
                // Filter for majority scale and rotation
                nonZeroCount = VoteForSizeAndOrientation(
                    modelKeyPoints, observedKeyPoints, matches, mask, 1.5, 20);

                // If 4 or more patches continue
                if (nonZeroCount >= 4)
                {
                    homography = GetHomographyMatrixFromMatchedFeatures(modelKeyPoints,
                                                                        observedKeyPoints, matches, mask, 2);
                }
            }

            // Draw the matched keypoints
            DrawMatches(
                modelImage, modelKeyPoints, observedImage, observedKeyPoints,
                matches, resultImage, new MCvScalar(255, 255, 255), new MCvScalar(255, 255, 255), mask);

            // Draw the projected region on the image
            if (homography != null)
            {
                // Draw a rectangle along the projected model
                Rectangle rect = new Rectangle(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)
                };

                // Transform the perspective of the points array based on the homography
                // And get a rotated rectangle for the homography
                pts = CvInvoke.PerspectiveTransform(pts, homography);

                Point[] points = Array.ConvertAll(pts, Point.Round);
                using (VectorOfPoint vp = new VectorOfPoint(points))
                {
                    CvInvoke.Polylines(resultImage, vp, true, new MCvScalar(255, 0, 0, 255), 5);
                }
            }

            result.ImageArray = ImageHelper.SetImage(resultImage.ToImage <Bgr, byte>());

            return(result);
        }
Ejemplo n.º 18
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.º 19
0
        private static void FindMatch(Mat modelImage, Mat observedImage, out long matchTime, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask, out Mat homography)
        {
            int    k = 2;
            double uniquenessThreshold = 0.80;

            Stopwatch watch;

            homography = null;

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

            try
            {
                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);

                                    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();
                    }
            }
            catch (Exception e)
            {
                throw e;
            }

            matchTime = watch.ElapsedMilliseconds;
        }
Ejemplo n.º 20
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();
        }
Ejemplo n.º 21
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;
        }