C#联合OpenCV特征匹配和单应性查找

特征匹配+单应性查找 

public static void forexample2()
        {
            Mat Src_Images1 = Cv2.ImRead("D:\\Desktop\\CSsharpOpenCVTest\\bin\\Debug\\_data\\image\\match1.png", ImreadModes.AnyColor);
            Mat Src_Images2 = Cv2.ImRead("D:\\Desktop\\CSsharpOpenCVTest\\bin\\Debug\\_data\\image\\match2.png", ImreadModes.AnyColor);
            var sift = SIFT.Create();
            var descriptors1 = new Mat<float>();
            var descriptors2 = new Mat<float>();
            sift.DetectAndCompute(Src_Images1, null, out var keypoints1, descriptors1);
            sift.DetectAndCompute(Src_Images2, null, out var keypoints2, descriptors2);
            var knnmatcher = FlannBasedMatcher.Create("FlannBased");
            var Matchs = knnmatcher.KnnMatch(descriptors1, descriptors2, 2);
            if (Matchs.ToList().Count > 10)
            {
                List<DMatch> good = new List<DMatch>();
                for (int i = 0; i < Matchs.Length; i++)
                {
                    if (Matchs[i][0].Distance < Matchs[i][1].Distance * 0.7)
                    {
                        good.Add(Matchs[i][0]);
                    }
                }
                List<List<DMatch>> good1 = new List<List<DMatch>>() { good.ToList() };

                //3特征点匹配
                List<Point2d> imagepoint1 = new List<Point2d> { };
                List<Point2d> imagepoint2 = new List<Point2d> { };
                for (int j = 0; j < good.Count; j++)
                {
                    //查找特征点可连接处   变形
                    Point2d point2D = new Point2d(0, 0);
                    if (good[j].TrainIdx > Matchs.Length)
                    {
                        continue;
                    }
                    point2D.X = Convert.ToDouble(keypoints1[good[j].QueryIdx].Pt.X);
                    point2D.Y = Convert.ToDouble(keypoints1[good[j].QueryIdx].Pt.Y);
                    imagepoint1.Add(point2D);
                    //查找特征点可连接处                          查找基准线
                    point2D.X = Convert.ToDouble(keypoints2[good[j].TrainIdx].Pt.X);
                    point2D.Y = Convert.ToDouble(keypoints2[good[j].TrainIdx].Pt.Y);
                    imagepoint2.Add(point2D);
                }
                //4 透视变换图形融合

                Mat homo = Cv2.FindHomography(imagepoint1, imagepoint2, HomographyMethods.Ransac);
                Point2f[] AffinePointsSrc = new Point2f[4] { new Point2f(0, 0), new Point2f(0, Src_Images1.Height - 1), new Point2f(Src_Images1.Width - 1, Src_Images1.Height - 1), new Point2f(Src_Images1.Width - 1, 0) };
                Point2f[] TransImage = Cv2.PerspectiveTransform(AffinePointsSrc, homo);
                Point[] pp = new Point[TransImage.Length];
                for (int k = 0; k < TransImage.Length; k++)
                {
                    pp[k] = TransImage[k].ToPoint();
                }
                List<List<Point>> pplist = new List<List<Point>>() { pp.ToList() };
                Cv2.CvtColor(Src_Images2, Src_Images2, ColorConversionCodes.GRAY2BGR);
                Cv2.Polylines(Src_Images2, pplist, true, Scalar.Blue);
                Mat Dsr_Images = new Mat();
                Cv2.DrawMatches(Src_Images1, keypoints1, Src_Images2, keypoints2, good, Dsr_Images, Scalar.Red, Scalar.Green, null, DrawMatchesFlags.NotDrawSinglePoints);
                Cv2.ImShow("123", Dsr_Images);
            }
            else
            {
                Console.WriteLine("没有足够多的关键点");
            }
        }

结果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值