OpenCVSharp 4.5 AKAZE、ORB 平面追踪

用OpenCVSharp 4.5 跑一遍 OpenCV官方教程

原 OpenCV 官方教程链接:OpenCV: AKAZE and ORB planar tracking

using System;
using System.Collections.Generic;
using OpenCvSharp;
namespace ConsoleApp1
{
    class tutorial51 : ITutorial
    {
        public void Run()
        {
            int stats_update_period = 10;
            string videoFile = @"I:\csharp\videos\blais.mp4";

            VideoCapture video_in = new VideoCapture(videoFile);
            if (video_in.IsOpened() == false)
            {
                Console.WriteLine("打开视频文件或摄像头出错");
                return;
            }

            Stats stats = new Stats(), akaze_stats = new Stats(), orb_stats = new Stats();

            AKAZE akaze = AKAZE.Create();
            akaze.Threshold = 3e-4;
            ORB orb = ORB.Create();
            DescriptorMatcher matcher = DescriptorMatcher.Create("BruteForce-Hamming");
            Tracker akaze_tracker = new Tracker(akaze, matcher);
            Tracker orb_tracker = new Tracker(orb, matcher);

            Mat frame = new Mat();
            Cv2.NamedWindow("Video", WindowFlags.Normal);
            Console.WriteLine("\nPress any key to stop the video and select a bounding box");
            while (Cv2.WaitKey(10) < 1)
            {
                video_in.Read(frame);
                Cv2.ResizeWindow("Video", frame.Size());
                Cv2.ImShow("Video", frame);
            }
            Point2f[] bb = new Point2f[4];
            Rect uBox = Cv2.SelectROI("video", frame);
            bb[0] = new Point2f(uBox.X, uBox.Y);
            bb[1] = new Point2f(uBox.X + uBox.Width, uBox.Y);
            bb[2] = new Point2f(uBox.X + uBox.Width, uBox.Y + uBox.Height);
            bb[3] = new Point2f(uBox.X, uBox.Y + uBox.Height);

            akaze_tracker.setFirstFrame(frame, bb, "AKAZE", stats);
            orb_tracker.setFirstFrame(frame, bb, "ORB", stats);

            Stats akaze_draw_stats=new Stats(), orb_draw_stats=new Stats();
            Mat akaze_res = new Mat();
            Mat orb_res = new Mat();
            Mat res_frame = new Mat();

            int i = 0;
            for (; ; )
            {
                i++;
                bool update_stats = (i % stats_update_period == 0);
                video_in.Read(frame);
                // stop the program if no more images
                if (frame.Empty()) break;
                akaze_res = akaze_tracker.process(frame, stats);
                akaze_stats += stats;
                if (update_stats)
                {
                    akaze_draw_stats = stats;
                }
                orb.MaxFeatures = stats.keypoints;
                orb_res = orb_tracker.process(frame, stats);
                orb_stats += stats;
                if (update_stats)
                {
                    orb_draw_stats = stats;
                }
                Util.drawStatistics(akaze_res, akaze_draw_stats);
                Util.drawStatistics(orb_res, orb_draw_stats);
                Cv2.VConcat(akaze_res, orb_res, res_frame);
                Cv2.ImShow("Video", res_frame);
                if (Cv2.WaitKey(1) == 27) break; //quit on ESC button
            }
            akaze_stats /= i - 1;
            orb_stats /= i - 1;
            Util.printStatistics("AKAZE", akaze_stats);
            Util.printStatistics("ORB", orb_stats);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值