static void Main(string[] args)
        {
            var img1 = new Mat(@"..\..\Images\left.png", LoadMode.GrayScale);
            Cv2.ImShow("Left", img1);
            Cv2.WaitKey(1); // do events

            var img2 = new Mat(@"..\..\Images\right.png", LoadMode.GrayScale);
            Cv2.ImShow("Right", img2);
            Cv2.WaitKey(1); // do events

            // detecting keypoints
            // FastFeatureDetector, StarDetector, SIFT, SURF, ORB, BRISK, MSER, GFTTDetector, DenseFeatureDetector, SimpleBlobDetector
            // SURF = Speeded Up Robust Features
            var detector = new SURF(hessianThreshold: 400); //A good default value could be from 300 to 500, depending from the image contrast.
            var keypoints1 = detector.Detect(img1);
            var keypoints2 = detector.Detect(img2);

            // computing descriptors, BRIEF, FREAK
            // BRIEF = Binary Robust Independent Elementary Features
            var extractor = new BriefDescriptorExtractor();
            var descriptors1 = new Mat();
            var descriptors2 = new Mat();
            extractor.Compute(img1, ref keypoints1, descriptors1);
            extractor.Compute(img2, ref keypoints2, descriptors2);

            // matching descriptors
            var matcher = new BFMatcher();
            var matches = matcher.Match(descriptors1, descriptors2);

            // drawing the results
            var imgMatches = new Mat();
            Cv2.DrawMatches(img1, keypoints1, img2, keypoints2, matches, imgMatches);
            Cv2.ImShow("Matches", imgMatches);
            Cv2.WaitKey(1); // do events

            Cv2.WaitKey(0);

            Cv2.DestroyAllWindows();
            img1.Dispose();
            img2.Dispose();
        }
        public void New3()
        {
            var descriptorExtractor = KAZE.Create();
            var descriptorMatcher   = new BFMatcher();

            new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher);
        }
        public void New2()
        {
            var descriptorExtractor = SURF.Create(100);
            var descriptorMatcher   = new BFMatcher();

            new BOWImgDescriptorExtractor(descriptorExtractor, descriptorMatcher);
        }
예제 #4
0
        private void MatchBySurf(Mat src1, Mat src2)
        {
            Mat gray1 = new Mat();
            Mat gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversion.BgrToGray);
            Cv2.CvtColor(src2, gray2, ColorConversion.BgrToGray);

            SURF surf = new SURF(500, 4, 2, true);

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

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

            // Matching descriptor vectors with a brute force matcher
            BFMatcher matcher = new BFMatcher(NormType.L2, false);

            DMatch[] matches = matcher.Match(descriptors1, descriptors2);

            // Draw matches
            Mat view = new Mat();

            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, matches, view);

            using (new Window("SURF matching", WindowMode.AutoSize, view))
            {
                Cv2.WaitKey();
            }
        }
예제 #5
0
        private void MatchBySift(Mat src1, Mat src2)
        {
            using var gray1 = new Mat();
            using var gray2 = new Mat();

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

            using var sift = SIFT.Create();

            // Detect the keypoints and generate their descriptors using SIFT
            using var descriptors1 = new Mat <float>();
            using var descriptors2 = new Mat <float>();
            sift.DetectAndCompute(gray1, null, out var keypoints1, descriptors1);
            sift.DetectAndCompute(gray2, null, out var keypoints2, descriptors2);

            // Match descriptor vectors
            using var bfMatcher    = new BFMatcher(NormTypes.L2, false);
            using var flannMatcher = new FlannBasedMatcher();
            DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            using var bfView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
            using var flannView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);

            using (new Window("SIFT matching (by BFMather)", bfView))
                using (new Window("SIFT matching (by FlannBasedMatcher)", flannView))
                {
                    Cv2.WaitKey();
                }
        }
예제 #6
0
파일: V2.cs 프로젝트: egrassl/BrainToolKit
        public V2()
        {
            ProcessFunction = (object obj) =>
            {
                dynamic prt          = obj as dynamic;
                var     descriptors1 = prt.descriptors1;
                var     descriptors2 = prt.descriptors2;
                var     keypoints1   = prt.keypoints1;
                var     keypoints2   = prt.keypoints2;
                Mat     gray1        = prt.gray1;
                Mat     gray2        = prt.gray2;
                // Match descriptor vectors
                var      bfMatcher    = new BFMatcher(NormTypes.L2, false);
                var      flannMatcher = new FlannBasedMatcher();
                DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
                DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

                // Draw matches
                var bfView = new Mat();
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
                var flannView = new Mat();
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);
                return(new { bfView, flannView });
            };

            PostProcessAction = () =>
            {
                Console.WriteLine("V2 terminou de processar!! Tempo gasto: {0}" + Environment.NewLine, ElapsedTime);
            };
        }
예제 #7
0
        private static DMatch[][] GetMatches(Mat query, Mat train)
        {
            BFMatcher matcher = new BFMatcher();
            var       matches = matcher.KnnMatch(query, train, k: 2);

            return(matches);
        }
예제 #8
0
        public void FindFeaturePointsBetweenTwoImages()
        {
            var filename            = "";
            var filename2           = "";
            var orb                 = new ORBDetector(2000);
            Image <Bgr, byte> left  = new Image <Bgr, byte>(filename);
            Image <Bgr, byte> right = new Image <Bgr, byte>(filename2);
            var vectorLeft          = new VectorOfKeyPoint();
            var vectorRight         = new VectorOfKeyPoint();
            var matLeft             = new Mat();
            var matRight            = new Mat();

            orb.DetectAndCompute(left, null, vectorLeft, matLeft, false);
            orb.DetectAndCompute(right, null, vectorRight, matRight, false);

            var matcher = new BFMatcher(DistanceType.Hamming2, true);
            var matches = new VectorOfVectorOfDMatch();

            matcher.Add(matLeft);
            matcher.KnnMatch(matRight, matches, 1, null);


            CalculateEssentialMAtrix(vectorLeft, vectorRight, camera.CameraMatrix);
            CalculateFundamentalMatrix(vectorLeft, vectorRight);
        }
        public Result AnalyzeImageEval(ref VectorOfKeyPoint keypointsEvalImage, ref Mat descriptorsEvalImage, float maxDistanceRatio,
                                       float reprojectionThreshold, double confidence, int maxIters, int minimumNumbersInliers)
        {
            var matches = new VectorOfDMatch();
            //Emgu.CV.Flann.KdTreeIndexParamses flannIndexParams = new Emgu.CV.Flann.KdTreeIndexParamses(4);
            //var flannIndex = new Index(descriptorsQueryImage, flannIndexParams);
            //DescriptorMatcher matcher = flannIndex;
            BFMatcher bfmatcher = new BFMatcher(DistanceType.L2);

            _util.MatchDescriptorsWithRatioTest(bfmatcher, ref descriptorsEvalImage, _descriptorsImageTrain[_LODIndex], ref matches, maxDistanceRatio);

            if (matches.Size < minimumNumbersInliers)
            {
                return(new Result());
            }

            Mat            homography     = new Mat();
            VectorOfDMatch inliers        = new VectorOfDMatch();
            VectorOfInt    inliersMaskOut = new VectorOfInt();

            _util.RefineMatchesWithHomography(keypointsEvalImage, _keypointsImageTrain[_LODIndex], matches, ref homography, inliers, inliersMaskOut, reprojectionThreshold, minimumNumbersInliers);

            if (inliers.Size < minimumNumbersInliers)
            {
                return(new Result());
            }

            float bestROIMatch = 0;

            bestROIMatch = (float)inliers.Size / (float)matches.Size;

            return(new Result(ValueBanknote, new VectorOfPoint(), ColorContour, bestROIMatch, _trainsImage[_LODIndex], _keypointsImageTrain[_LODIndex], keypointsEvalImage, ref matches, ref inliers, ref inliersMaskOut, ref homography));
        }
        private VectorOfPoint _octagon;             //Искомая область

        /// <summary>
        /// Конструктор.
        /// </summary>
        /// <param name="brickSingModel">Обрабатываемое изображение. Принимается Image<Bgr, Byte></param>
        public SingDetectorMethodCanny(IInputArray brickSingModel)
        {
            _detector = new SURF(500);

            using (Mat redMask = new Mat())
            {
                GetRedPixelMask(brickSingModel, redMask);
                _modelKeypoints   = new VectorOfKeyPoint();
                _modelDescriptors = new Mat();
                _detector.DetectAndCompute(redMask, null, _modelKeypoints, _modelDescriptors, false);
                if (_modelKeypoints.Size == 0)
                {
                    //throw new Exception("Изображение для обработки не загружено");
                }
            }

            _modelDescriptorMatcher = new BFMatcher(DistanceType.L2);
            _modelDescriptorMatcher.Add(_modelDescriptors);

            _octagon = new VectorOfPoint(
                new Point[] {
                new Point(1, 0),
                new Point(2, 0),
                new Point(3, 1),
                new Point(3, 2),
                new Point(2, 3),
                new Point(1, 3),
                new Point(0, 2),
                new Point(0, 1)
            });
        }
예제 #11
0
        private void MatchBySurf(Mat src1, Mat src2)
        {
            Mat gray1 = new Mat();
            Mat gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversion.BgrToGray);
            Cv2.CvtColor(src2, gray2, ColorConversion.BgrToGray);

            SURF surf = new SURF(500, 4, 2, true);

            // Detect the keypoints and generate their descriptors using SURF
            KeyPoint[] keypoints1, keypoints2;
            MatOfFloat descriptors1 = new MatOfFloat();
            MatOfFloat descriptors2 = new MatOfFloat();
            surf.Run(gray1, null, out keypoints1, descriptors1);
            surf.Run(gray2, null, out keypoints2, descriptors2);

            // Matching descriptor vectors with a brute force matcher
            BFMatcher matcher = new BFMatcher(NormType.L2, false);
            DMatch[] matches = matcher.Match(descriptors1, descriptors2);

            // Draw matches
            Mat view = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, matches, view);

            using (new Window("SURF matching", WindowMode.AutoSize, view))
            {
                Cv2.WaitKey();
            }
        }
예제 #12
0
        public static void FindMatch(string modelFileName, string observedFileName, out VectorOfKeyPoint modelKeyPoints, out VectorOfKeyPoint observedKeyPoints, VectorOfVectorOfDMatch matches, out Mat mask)
        {
            int    k = 2;
            double uniquenessThreshold = 0.8;

            modelKeyPoints    = new VectorOfKeyPoint();
            observedKeyPoints = new VectorOfKeyPoint();
            {
                using (UMat uModelImage = CvInvoke.Imread(modelFileName, ImreadModes.Color).GetUMat(AccessType.Read))
                    using (UMat uObservedImage = CvInvoke.Imread(observedFileName, ImreadModes.Color).GetUMat(AccessType.Read))
                    {
                        SIFT sift             = new SIFT();
                        UMat modelDescriptors = new UMat();
                        sift.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                        UMat observedDescriptors = new UMat();
                        sift.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
                        BFMatcher matcher = new BFMatcher(DistanceType.L2);
                        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);
                    }
            }
        }
예제 #13
0
        public void Run(Mat gray1, Mat gray2, Mat dst2, bool useBFMatcher, int pointsToMatch)
        {
            var sift = SIFT.Create(pointsToMatch);

            KeyPoint[] keypoints1, keypoints2;
            var        descriptors1 = new Mat();
            var        descriptors2 = new Mat();

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

            if (useBFMatcher)
            {
                var      bfMatcher = new BFMatcher(NormTypes.L2, false);
                DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, dst2);
            }
            else
            {
                var      flannMatcher = new FlannBasedMatcher();
                DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);
                Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, dst2);
            }
            kp1 = keypoints1;
            kp2 = keypoints2;
        }
예제 #14
0
        public static void GetMatches(VectorOfKeyPoint imageKeypoints, IInputArray imageDescriptors, VectorOfKeyPoint patternKeypoints, IInputArray patternDescriptors, out VectorOfVectorOfDMatch matches, out Mat homography)
        {
            int    k = 2;
            double uniquenessThreshold = 0.8;

            homography = null;

            matches = new VectorOfVectorOfDMatch();

            var matcher = new BFMatcher(DistanceType.L2);

            matcher.Add(patternDescriptors);
            matcher.KnnMatch(imageDescriptors, matches, k, null);

            var 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(patternKeypoints, imageKeypoints, matches, mask, 1.5, 20);
                if (nonZeroCount >= 4)
                {
                    homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(patternKeypoints, imageKeypoints, matches, mask, 2);
                }
            }
        }
        private void FindAndDrawHomo()
        {
            using (var template = new Mat("Images\\Circle_Template.bmp", ImreadModes.Color))
            {
                using (var surf = SURF.Create(1000))
                {
                    using (var templateDescriptors = new Mat())
                    {
                        surf.DetectAndCompute(template, null, out KeyPoint[] templateKeyPoints, templateDescriptors);
                        using (var image = new Mat("Images\\Circle.bmp", ImreadModes.Color))
                        {
                            using (var imageDescriptors = new Mat())
                            {
                                surf.DetectAndCompute(image, null, out KeyPoint[] imageKeyPoints, imageDescriptors);
                                using (var matcher = new BFMatcher())
                                {
                                    var matches     = matcher.Match(imageDescriptors, templateDescriptors);
                                    var goodMatches = matches;//.Where(m => m.Distance < 0.2).ToArray();
                                    using (var srcPoints = InputArray.Create(goodMatches.Select(m => templateKeyPoints[m.TrainIdx].Pt)))
                                    {
                                        using (var dstPoints = InputArray.Create(goodMatches.Select(m => imageKeyPoints[m.QueryIdx].Pt)))
                                        {
                                            using (var h**o = Cv2.FindHomography(srcPoints, dstPoints, HomographyMethods.Rho))
                                            {
                                                ////using (var overlay = image.Overlay())
                                                ////{
                                                ////    DrawBox(template, h**o, overlay);
                                                ////    this.Result.Source = overlay.ToBitmapSource();
                                                ////}

                                                using (var tmp = image.Overlay())
                                                {
                                                    Cv2.BitwiseNot(template, template);
                                                    Cv2.WarpPerspective(template, tmp, h**o, tmp.Size());
                                                    using (var overlay = tmp.Overlay())
                                                    {
                                                        for (var r = 0; r < tmp.Rows; r++)
                                                        {
                                                            for (var c = 0; c < tmp.Cols; c++)
                                                            {
                                                                overlay.Set(r, c,
                                                                            tmp.At <int>(r, c) == 0
                                                                        ? new Vec4b(0, 0, 0, 0)
                                                                        : new Vec4b(0, 0, 255, 150));
                                                            }
                                                        }

                                                        this.Result.Source = overlay.ToBitmapSource();
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #16
0
        private Mat MatchBySurf(Mat src1, Mat src2)
        {
            using var gray1 = new Mat();
            using var gray2 = new Mat();

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

            //using var surf = SURF.Create(200, 4, 2, true);
            using var surf = AKAZE.Create();

            // Detect the keypoints and generate their descriptors using SURF
            using var descriptors1 = new Mat <float>();
            using var descriptors2 = new Mat <float>();
            surf.DetectAndCompute(gray1, null, out var keypoints1, descriptors1);
            surf.DetectAndCompute(gray2, null, out var keypoints2, descriptors2);

            // Match descriptor vectors
            using var bfMatcher = new BFMatcher(NormTypes.L2, false);
            DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);

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

            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView, flags: DrawMatchesFlags.NotDrawSinglePoints);

            return(bfView);
        }
        private void trainModel(String plantCategoryName, List <String> eachLeafSpecies)
        {
            BFMatcher match;

            if (!categoryBfmatcherMapping.ContainsKey(plantCategoryName))
            {
                match = new BFMatcher(DistanceType.L2);
                categoryBfmatcherMapping.Add(plantCategoryName, match);
            }
            else
            {
                //Find values associated with key
                match = categoryBfmatcherMapping[plantCategoryName];
            }
            foreach (String leaf in eachLeafSpecies)
            {
                using (Image <Bgr, Byte> image = loadImage(leaf))
                {
                    using (PreProcess preProcessAlgorithm = new PreProcess(image))
                    {
                        using (Image <Hsv, Byte> HSVImage = preProcessAlgorithm._ImageToHSV())
                        {
                            using (Image <Gray, Byte> grayScaleImage = preProcessAlgorithm._ImageToGrayScaleUsingConvert())
                            {
                                using (FeatureExtractAlgorithm featureSet = new FeatureExtractAlgorithm(grayScaleImage))
                                {
                                    KeyPoints leafDescriptor = featureSet.SIFTDescriptor();
                                    match.Add(leafDescriptor.Descriptor);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #18
0
        public bool MatchDescriptorsWithRatioTest(BFMatcher descriptorMatcher, ref Mat descriptorsEvalImage, Mat trainDescriptors, ref VectorOfDMatch matchesFilteredOut, float maxDistanceRatio)
        {
            if (trainDescriptors.Rows < 4)
            {
                return(false);
            }

            matchesFilteredOut.Clear();
            descriptorMatcher.Add(trainDescriptors);

            VectorOfVectorOfDMatch matchesKNN = new VectorOfVectorOfDMatch();

            descriptorMatcher.KnnMatch(descriptorsEvalImage, matchesKNN, 2, null);
            for (int matchPos = 0; matchPos < matchesKNN.Size; ++matchPos)
            {
                if (matchesKNN[matchPos].Size >= 2)
                {
                    if (matchesKNN[matchPos][0].Distance <= maxDistanceRatio * matchesKNN[matchPos][1].Distance)
                    {
                        matchesFilteredOut.Push(new MDMatch[] { matchesKNN[matchPos][0] });
                    }
                }
            }

            return(!(matchesFilteredOut.Size == 0));
        }
예제 #19
0
        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)
        {
            int    k = 2;
            double uniquenessThreshold = 0.80;
            double hessianThresh       = 100;

            Stopwatch watch;

            homography = null;

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

            using (UMat uModelImage = modelImage.GetUMat(AccessType.Read))
                using (UMat uObservedImage = observedImage.GetUMat(AccessType.Read))
                {
                    SURF surfCPU = new SURF(hessianThresh);
                    SIFT siftCPU = new SIFT();


                    //extract features from the object image
                    UMat modelDescriptors = new UMat();

                    //surfCPU.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);
                    siftCPU.DetectAndCompute(uModelImage, null, modelKeyPoints, modelDescriptors, false);

                    watch = Stopwatch.StartNew();

                    // extract features from the observed image
                    UMat observedDescriptors = new UMat();

                    //surfCPU.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
                    siftCPU.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);

                    BFMatcher matcher = new BFMatcher(DistanceType.L2);
                    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);
                    //Features2DToolbox.VoteForUniqueness(matches, 1, 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();
                }
            matchTime = watch.ElapsedMilliseconds;
        }
 private void FindAndDrawMatches()
 {
     using (var template = new Mat("Images\\Circle_Template.bmp", ImreadModes.GrayScale))
     {
         using (var surf = SURF.Create(1000))
         {
             using (var templateDescriptors = new Mat())
             {
                 surf.DetectAndCompute(template, null, out KeyPoint[] templateKeyPoints, templateDescriptors);
                 using (var image = new Mat("Images\\Circle.bmp", ImreadModes.GrayScale))
                 {
                     using (var imageDescriptors = new Mat())
                     {
                         surf.DetectAndCompute(image, null, out KeyPoint[] imageKeyPoints, imageDescriptors);
                         using (var matcher = new BFMatcher())
                         {
                             var matches = matcher.Match(imageDescriptors, templateDescriptors);
                             using (var overLay = image.Overlay())
                             {
                                 Cv2.DrawMatches(image, imageKeyPoints, template, templateKeyPoints, matches, overLay);
                                 this.Result.Source = overLay.ToBitmapSource();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
예제 #21
0
        private void MatchBySurf(Mat src1, Mat src2)
        {
            var gray1 = new Mat();
            var gray2 = new Mat();

            Cv2.CvtColor(src1, gray1, ColorConversion.BgrToGray);
            Cv2.CvtColor(src2, gray2, ColorConversion.BgrToGray);

            var surf = new SURF(500, 4, 2, true);

            // Detect the keypoints and generate their descriptors using SURF
            KeyPoint[] keypoints1, keypoints2;
            var descriptors1 = new MatOfFloat();
            var descriptors2 = new MatOfFloat();
            surf.Run(gray1, null, out keypoints1, descriptors1);
            surf.Run(gray2, null, out keypoints2, descriptors2);

            // Match descriptor vectors 
            var bfMatcher = new BFMatcher(NormType.L2, false);
            var flannMatcher = new FlannBasedMatcher();
            DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            var bfView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
            var flannView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);

            using (new Window("SURF matching (by BFMather)", WindowMode.AutoSize, bfView))
            using (new Window("SURF matching (by FlannBasedMatcher)", WindowMode.AutoSize, flannView))
            {
                Cv2.WaitKey();
            }
        }
예제 #22
0
        private void FindMatch(Mat observedImage)
        {
            int k = 2;

            mask           = new Mat();
            homography     = null;
            matches        = new VectorOfVectorOfDMatch();
            uObservedImage = observedImage.GetUMat(AccessType.ReadWrite);

            // extract features from the observed image
            ORBCPU.DetectAndCompute(uObservedImage, null, observedKeyPoints, observedDescriptors, false);
            matcher = new BFMatcher(DistanceType.L2);
            matcher.Add(objDescriptors);
            if (objDescriptors.Size.Height > 3 && observedDescriptors.Size.Height > 3)
            {
                matcher.KnnMatch(observedDescriptors, matches, k, null);
                mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);
                mask.SetTo(new MCvScalar(255));
                Features2DToolbox.VoteForUniqueness(matches, 0.8, mask);

                int nonZeroCount = CvInvoke.CountNonZero(mask);
                if (nonZeroCount >= 4)
                {
                    //nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(objKeyPoints, observedKeyPoints,
                    //matches, mask, 1, 2);
                    if (nonZeroCount >= 4)
                    {
                        homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(objKeyPoints,
                                                                                              observedKeyPoints, matches, mask, 3);
                    }
                }
            }
        }
예제 #23
0
        public void Run(Mat gray1, Mat gray2, Mat dst, int hessianThreshold, bool useBFMatcher)
        {
            var surf = SURF.Create(hessianThreshold, 4, 2, true);

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

            if (useBFMatcher)
            {
                if (descriptors1.Rows > 0 && descriptors2.Rows > 0) // occasionally there is nothing to match!
                {
                    var      bfMatcher = new BFMatcher(NormTypes.L2, false);
                    DMatch[] bfMatches = bfMatcher.Match(descriptors1, descriptors2);
                    Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, dst);
                }
            }
            else
            {
                var flannMatcher = new FlannBasedMatcher();
                if (descriptors1.Width > 0 && descriptors2.Width > 0)
                {
                    DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);
                    Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, dst);
                }
            }
        }
        public static VectorOfVectorOfDMatch KnnMatch(this BFMatcher matcher, UMat pdesc, int i, IInputArray o = null)
        {
            var puzzelmatches = new VectorOfVectorOfDMatch();

            matcher.KnnMatch(pdesc, puzzelmatches, i, o);
            return(puzzelmatches);
        }
예제 #25
0
        public StopSignDetector(IInputArray stopSignModel)
        {
            _detector = new KAZE();
            using (Mat redMask = new Mat())
            {
                GetRedPixelMask(stopSignModel, redMask);
                _modelKeypoints   = new VectorOfKeyPoint();
                _modelDescriptors = new Mat();
                _detector.DetectAndCompute(redMask, null, _modelKeypoints, _modelDescriptors, false);
                if (_modelKeypoints.Size == 0)
                {
                    throw new Exception("No image feature has been found in the stop sign model");
                }
            }

            _modelDescriptorMatcher = new BFMatcher(DistanceType.L2);
            _modelDescriptorMatcher.Add(_modelDescriptors);

            _octagon = new VectorOfPoint(
                new Point[]
            {
                new Point(1, 0),
                new Point(2, 0),
                new Point(3, 1),
                new Point(3, 2),
                new Point(2, 3),
                new Point(1, 3),
                new Point(0, 2),
                new Point(0, 1)
            });
        }
예제 #26
0
파일: test.cs 프로젝트: chencen2000/testMQ
        static void test_svm()
        {
            FileStorage fs   = new FileStorage("test.yaml", FileStorage.Mode.Read);
            FileNode    n    = fs["voca"];
            Mat         voca = new Mat();

            n.ReadMat(voca);

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

            bowDex.SetVocabulary(voca);

            SVM svm = new SVM();

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

            foreach (string s in System.IO.Directory.GetFiles(@"C:\projects\local\testMQ\testMQ\bin\Debug\icons"))
            {
                Image <Bgr, Byte> test_img = new Image <Bgr, byte>(s);
                //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\projects\local\testMQ\testMQ\bin\Debug\mail_samples\email_icon_t.jpg");
                //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\projects\local\testMQ\testMQ\bin\Debug\phone_icons\icon_2.jpg");
                //Image<Bgr, Byte> test_img = new Image<Bgr, byte>(@"C:\test\35928233-email-icon-on-blue-background-clean-vector.jpg");
                Mat ii = new Mat();
                CvInvoke.CvtColor(test_img, ii, ColorConversion.Bgr2Gray);
                MKeyPoint[] kp   = surf.Detect(ii);
                Mat         desc = new Mat();
                bowDex.Compute(ii, new VectorOfKeyPoint(kp), desc);
                float r = svm.Predict(desc);
                Program.logIt(string.Format("{0}={1}", s, r));
            }
        }
예제 #27
0
        public void TestBOWKmeansTrainer2()
        {
            Image <Gray, byte> box    = EmguAssert.LoadImage <Gray, byte>("box.png");
            Brisk            detector = new Brisk(30, 3, 1.0f);
            VectorOfKeyPoint kpts     = new VectorOfKeyPoint();
            Mat descriptors           = new Mat();

            detector.DetectAndCompute(box, null, kpts, descriptors, false);
            Mat descriptorsF = new Mat();

            descriptors.ConvertTo(descriptorsF, CvEnum.DepthType.Cv32F);
            //Matrix<float> descriptorsF = descriptors.Convert<float>();
            BOWKMeansTrainer trainer = new BOWKMeansTrainer(100, new MCvTermCriteria(), 3, CvEnum.KMeansInitType.PPCenters);

            trainer.Add(descriptorsF);
            Mat vocabulary = new Mat();

            trainer.Cluster(vocabulary);

            BFMatcher matcher = new BFMatcher(DistanceType.L2);

            BOWImgDescriptorExtractor extractor = new BOWImgDescriptorExtractor(detector, matcher);
            Mat vocabularyByte = new Mat();

            vocabulary.ConvertTo(vocabularyByte, CvEnum.DepthType.Cv8U);
            extractor.SetVocabulary(vocabularyByte);

            Mat descriptors2 = new Mat();

            extractor.Compute(box, kpts, descriptors2);
        }
예제 #28
0
        private void MatchBySurf(Mat src1, Mat src2)
        {
            using var gray1 = new Mat();
            using var gray2 = new Mat();

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

            using var surf = SURF.Create(200, 4, 2, true);

            // Detect the keypoints and generate their descriptors using SURF
            KeyPoint[] keypoints1, keypoints2;
            using var descriptors1 = new Mat <float>();
            using var descriptors2 = new Mat <float>();
            surf.DetectAndCompute(gray1, null, out keypoints1, descriptors1);
            surf.DetectAndCompute(gray2, null, out keypoints2, descriptors2);

            // Match descriptor vectors
            using var bfMatcher    = new BFMatcher(NormTypes.L2, false);
            using var flannMatcher = new FlannBasedMatcher();
            DMatch[] bfMatches    = bfMatcher.Match(descriptors1, descriptors2);
            DMatch[] flannMatches = flannMatcher.Match(descriptors1, descriptors2);

            // Draw matches
            using var bfView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, bfMatches, bfView);
            using var flannView = new Mat();
            Cv2.DrawMatches(gray1, keypoints1, gray2, keypoints2, flannMatches, flannView);

            using (new Window("SURF matching (by BFMather)", WindowMode.AutoSize, bfView))
                using (new Window("SURF matching (by FlannBasedMatcher)", WindowMode.AutoSize, flannView))
                {
                    Cv2.WaitKey();
                }
        }
예제 #29
0
        public void TestBOWKmeansTrainer()
        {
            Image <Gray, byte> box    = EmguAssert.LoadImage <Gray, byte>("box.png");
            SURF             detector = new SURF(500);
            VectorOfKeyPoint kpts     = new VectorOfKeyPoint();
            Mat descriptors           = new Mat();

            detector.DetectAndCompute(box, null, kpts, descriptors, false);

            BOWKMeansTrainer trainer = new BOWKMeansTrainer(100, new MCvTermCriteria(), 3, CvEnum.KMeansInitType.PPCenters);

            trainer.Add(descriptors);
            Mat vocabulary = new Mat();

            trainer.Cluster(vocabulary);

            BFMatcher matcher = new BFMatcher(DistanceType.L2);

            BOWImgDescriptorExtractor extractor = new BOWImgDescriptorExtractor(detector, matcher);

            extractor.SetVocabulary(vocabulary);

            Mat descriptors2 = new Mat();

            extractor.Compute(box, kpts, descriptors2);
        }
예제 #30
0
        /// <summary>
        /// To avoid NaN's when best match has zero distance we will use inversed ratio.
        /// KNN match will return 2 nearest matches for each query descriptor
        /// </summary>
        List <DMatch> GetMatches(BFMatcher matcher, Mat queryDescriptors, Mat trainDescriptors)
        {
            List <DMatch> matchesList = new List <DMatch>();

            if (enableRatioTest)
            {
                float      minRatio = 1.0f / 1.5f;
                DMatch[][] dm       = matcher.KnnMatch(queryDescriptors, trainDescriptors, 2);

                for (int i = 0; i < dm.Length; i++)
                {
                    DMatch bestMatch   = dm[i][0];
                    DMatch betterMatch = dm[i][1];

                    float distanceRatio = bestMatch.Distance / betterMatch.Distance;

                    if (distanceRatio < minRatio)
                    {
                        matchesList.Add(bestMatch);
                    }
                }
            }
            else
            {
                matchesList.AddRange(matcher.Match(queryDescriptors, trainDescriptors));
            }
            return(matchesList);
        }
예제 #31
0
        private static void BForceMatcherSample()
        {
            var src1 = new Mat("data/match1.png");
            var src2 = new Mat("data/match2.png");

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

            Cv2.CvtColor(src1, gray1, ColorConversion.BgrToGray);
            Cv2.CvtColor(src2, gray2, ColorConversion.BgrToGray);

            var fast = new FastFeatureDetector(10);
            var descriptorExtractor = new BriefDescriptorExtractor(32);

            var descriptors1 = new Mat();
            var descriptors2 = new Mat();

            KeyPoint[] keypoints1 = fast.Run(gray1, null);
            descriptorExtractor.Compute(gray1, ref keypoints1, descriptors1);

            KeyPoint[] keypoints2 = fast.Run(gray2, null);
            descriptorExtractor.Compute(gray2, ref keypoints2, descriptors2);

            // Match descriptor vectors
            var bfMatcher = new BFMatcher(NormType.L2, false);

            DMatch[][] bfMatches = bfMatcher.KnnMatch(descriptors1, descriptors2, 3, null, false);
            bfMatches.ToString();

            var view = new Mat();

            Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, bfMatches, view);
            Window.ShowImages(view);
        }
예제 #32
0
파일: Form1.cs 프로젝트: swishposh/aoci7
        public Image <Bgr, byte> pointComp(Image <Bgr, byte> baseImg, Image <Bgr, byte> twistedImg)
        {
            Image <Gray, byte> baseImgGray    = baseImg.Convert <Gray, byte>();
            Image <Gray, byte> twistedImgGray = twistedImg.Convert <Gray, byte>();
            Brisk            descriptor       = new Brisk();
            GFTTDetector     detector         = new GFTTDetector(40, 0.01, 5, 3, true);
            VectorOfKeyPoint GFP1             = new VectorOfKeyPoint();
            UMat             baseDesc         = new UMat();
            UMat             bimg             = twistedImgGray.Mat.GetUMat(AccessType.Read);
            VectorOfKeyPoint GFP2             = new VectorOfKeyPoint();
            UMat             twistedDesc      = new UMat();
            UMat             timg             = baseImgGray.Mat.GetUMat(AccessType.Read);

            detector.DetectRaw(bimg, GFP1);
            descriptor.Compute(bimg, GFP1, baseDesc);
            detector.DetectRaw(timg, GFP2);
            descriptor.Compute(timg, GFP2, twistedDesc);
            BFMatcher matcher = new BFMatcher(DistanceType.L2);
            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

            matcher.Add(baseDesc);
            matcher.KnnMatch(twistedDesc, matches, 2, null);
            Mat mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);

            mask.SetTo(new MCvScalar(255));
            Features2DToolbox.VoteForUniqueness(matches, 0.8, mask);
            //int nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation(GFP1, GFP1, matches, mask, 1.5, 20);
            Image <Bgr, byte> res = baseImg.CopyBlank();

            Features2DToolbox.DrawMatches(twistedImg, GFP1, baseImg, GFP2, matches, res, new MCvScalar(255, 0, 0), new MCvScalar(255, 0, 0), mask);
            return(res);
        }
예제 #33
0
파일: Form1.cs 프로젝트: swishposh/aoci7
        private void button3_Click(object sender, EventArgs e)
        {
            GFTTDetector detector = new GFTTDetector(40, 0.01, 5, 3, true);

            var baseImgGray    = baseImg.Convert <Gray, byte>();
            var twistedImgGray = twistedImg.Convert <Gray, byte>();

            //генератор описания ключевых точек
            Brisk descriptor = new Brisk();

            //поскольку в данном случае необходимо посчитать обратное преобразование
            //базой будет являться изменённое изображение
            VectorOfKeyPoint GFP1     = new VectorOfKeyPoint();
            UMat             baseDesc = new UMat();
            UMat             bimg     = twistedImgGray.Mat.GetUMat(AccessType.Read);

            VectorOfKeyPoint GFP2        = new VectorOfKeyPoint();
            UMat             twistedDesc = new UMat();
            UMat             timg        = baseImgGray.Mat.GetUMat(AccessType.Read);

            //получение необработанной информации о характерных точках изображений
            detector.DetectRaw(bimg, GFP1);

            //генерация описания характерных точек изображений
            descriptor.Compute(bimg, GFP1, baseDesc);
            detector.DetectRaw(timg, GFP2);
            descriptor.Compute(timg, GFP2, twistedDesc);

            //класс позволяющий сравнивать описания наборов ключевых точек
            BFMatcher matcher = new BFMatcher(DistanceType.L2);

            //массив для хранения совпадений характерных точек
            VectorOfVectorOfDMatch matches = new VectorOfVectorOfDMatch();

            //добавление описания базовых точек
            matcher.Add(baseDesc);
            //сравнение с описанием изменённых
            matcher.KnnMatch(twistedDesc, matches, 2, null);
            //3й параметр - количество ближайших соседей среди которых осуществляется поиск совпадений
            //4й параметр - маска, в данном случае не нужна

            //маска для определения отбрасываемых значений (аномальных и не уникальных)
            Mat mask = new Mat(matches.Size, 1, DepthType.Cv8U, 1);

            mask.SetTo(new MCvScalar(255));
            //определение уникальных совпадений
            Features2DToolbox.VoteForUniqueness(matches, 0.8, mask);

            Mat homography;

            //получение матрицы гомографии
            homography = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures(GFP1, GFP2, matches, mask, 2);

            var destImage = new Image <Bgr, byte>(baseImg.Size);

            CvInvoke.WarpPerspective(twistedImg, destImage, homography, destImage.Size);
            twistedImg      = destImage;
            imageBox2.Image = destImage.Resize(640, 480, Inter.Linear);
        }
예제 #34
0
        private static void BowTest()
        {
            DescriptorMatcher matcher = new BFMatcher();
            Feature2D extractor = AKAZE.Create();
            Feature2D detector = AKAZE.Create();

            TermCriteria criteria = new TermCriteria(CriteriaType.Count | CriteriaType.Eps, 10, 0.001);
            BOWKMeansTrainer bowTrainer = new BOWKMeansTrainer(200, criteria, 1);
            BOWImgDescriptorExtractor bowDescriptorExtractor = new BOWImgDescriptorExtractor(extractor, matcher);
            
            Mat img = null;

            KeyPoint[] keypoint = detector.Detect(img);
            Mat features = new Mat();
            extractor.Compute(img, ref keypoint, features);
            bowTrainer.Add(features);

            throw new NotImplementedException();
        }