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