public void CenteredRect2() { const int left = 0; const int top = 0; const int right = 100; const int bottom = 100; var rect = new DRectangle(left, top, right, bottom); double width = 20; double height = 30; var result = DRectangle.CenteredRect(rect, width, height); width--; height--; var expected = new DRectangle(); var eLeft = ((left + right) / 2) - width / 2; var eTop = ((top + bottom) / 2) - height / 2; var eRight = eLeft + width - 1; var eBottom = eTop + height - 1; Assert.AreEqual(result.Left, eLeft); Assert.AreEqual(result.Top, eTop); Assert.AreEqual(result.Right, eRight + 1); Assert.AreEqual(result.Bottom, eBottom + 1); }
public void CenteredRect() { const int x = 50; const int y = 60; var p = new DPoint(x, y); double width = 20; double height = 30; var result = DRectangle.CenteredRect(p, width, height); width--; height--; var eLeft = x - width / 2; var eTop = y - height / 2; var eRight = eLeft + width - 1; var eBottom = eTop + height - 1; Assert.AreEqual(result.Left, eLeft); Assert.AreEqual(result.Top, eTop); Assert.AreEqual(result.Right, eRight + 1); Assert.AreEqual(result.Bottom, eBottom + 1); this.DisposeAndCheckDisposedState(p); this.DisposeAndCheckDisposedState(result); }
private static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Call this program like this: "); Console.WriteLine("VideoTracking.exe <path of video_frames directory>"); return; } var path = args[0]; var files = new DirectoryInfo(path).GetFiles("*.jpg").Select(info => info.FullName).ToList(); files.Sort(); if (files.Count == 0) { Console.WriteLine($"No images found in {path}"); return; } using (var win = new ImageWindow()) using (var tracker = new CorrelationTracker()) { var firstFile = files.First(); using (var img = Dlib.LoadImage <byte>(firstFile)) using (var rect = DRectangle.CenteredRect(93, 110, 38, 86)) tracker.StartTrack(img, rect); foreach (var file in files.GetRange(1, files.Count - 1)) { using (var img = Dlib.LoadImage <byte>(file)) { tracker.Update(img); win.SetImage(img); win.ClearOverlay(); using (var pos = tracker.GetPosition()) win.AddOverlay(pos); Console.WriteLine("hit enter to process next frame"); Console.ReadKey(); } } } }
public void TryTrack() { const string testName = "TryTrack"; var tests = new[] { new { Type = ImageTypes.Int32, ExpectResult = true } }; var type = this.GetType().Name; foreach (var input in tests) { var tracker = new CorrelationTracker(); try { var index = 0; foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg"))) { var expectResult = input.ExpectResult; var inputType = input.Type; var imageObj = DlibTest.LoadImage(input.Type, file); if (index == 0) { using (var rect = DRectangle.CenteredRect(93, 110, 38, 86)) tracker.StartTrack(imageObj, rect); } var outputImageAction = new Func <bool, Array2DBase>(expect => { if (index != 0) { tracker.Update(imageObj); } return(imageObj); }); var successAction = new Action <Array2DBase>(image => { if (index != 0) { tracker.Update(image); } using (var r = tracker.GetPosition()) { using (var img = Dlib.LoadImage <RgbPixel>(file.FullName)) { Dlib.DrawRectangle(img, (Rectangle)r, new RgbPixel { Red = 255 }, 3); Dlib.SaveJpeg(img, Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg")); } } }); var failAction = new Action(() => { Assert.Fail($"{testName} should throw excption for InputType: {inputType}."); }); var finallyAction = new Action(() => { index++; if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } } finally { this.DisposeAndCheckDisposedState(tracker); } } }
private static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Call this program like this: "); Console.WriteLine("VideoTracking.exe <path of video_frames directory>"); return; } var path = args[0]; var files = new DirectoryInfo(path).GetFiles("*.jpg").Select(info => info.FullName).ToList(); files.Sort(); if (files.Count == 0) { Console.WriteLine($"No images found in {path}"); return; } // 定义图像捕捉方式 从摄像头 , 注意 Windows下需要选择 VideoCaptureAPIs.DSHOW var cap = new VideoCapture(0, VideoCaptureAPIs.DSHOW); // 定义图像捕捉方式 从摄像头 视频文件 //var cap = new VideoCapture("video.webm"); //判断捕捉设备是否打开 if (!cap.IsOpened()) { Console.WriteLine("Unable to connect to camera"); return; } Mat temp = null; var tracker = new CorrelationTracker(); int init = 0; //定义显示窗口 using (var win = new ImageWindow()) { Console.WriteLine("对象追踪程序启动"); Console.WriteLine("选择命令行为当前窗口,通过按键选择需要追踪的区域Width: [A,Z] Height:[S,X] X:[right,left] Y:[up,down] ,点击Enter开始追踪"); Console.WriteLine("注意:切换命令行窗口输入法为英文输入状态"); //选择追踪对象 while (!win.IsClosed()) { //获得1帧图片 temp = cap.RetrieveMat();// new Mat(); if (temp == null) { Console.WriteLine("图像获取错误!"); return; } var array = new byte[temp.Width * temp.Height * temp.ElemSize()]; Marshal.Copy(temp.Data, array, 0, array.Length); using (var cimg = Dlib.LoadImageData <BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize()))) { init++; if (init > 1) { var KK = Console.ReadKey(); if (KK.Key == ConsoleKey.Enter) { Console.WriteLine("开始追踪目标!"); //确定 追踪 位置 var rect2 = DRectangle.CenteredRect(a_X, a_Y, a_W, a_H); //开始追踪 tracker.StartTrack(cimg, rect2); win.SetImage(cimg); win.ClearOverlay(); win.AddOverlay(rect2); break; } //选择 追踪区域 if (KK.Key == ConsoleKey.RightArrow || KK.Key == ConsoleKey.LeftArrow || KK.Key == ConsoleKey.UpArrow || KK.Key == ConsoleKey.DownArrow || KK.Key == ConsoleKey.A || KK.Key == ConsoleKey.Z || KK.Key == ConsoleKey.S || KK.Key == ConsoleKey.X) { if (KK.Key == ConsoleKey.RightArrow) { a_X++; if (a_X > cimg.Rect.Width - a_W) { a_X = cimg.Rect.Width - a_W; } } if (KK.Key == ConsoleKey.LeftArrow) { a_X--; if (a_X < 0) { a_X = 0; } } if (KK.Key == ConsoleKey.UpArrow) { a_Y--; if (a_Y < 0) { a_Y = 0; } } if (KK.Key == ConsoleKey.DownArrow) { a_Y++; if (a_Y > cimg.Rect.Height - a_H) { a_Y = cimg.Rect.Height - a_H; } } if (KK.Key == ConsoleKey.A) { a_W++; if (a_W >= cimg.Rect.Width - a_X) { a_W = cimg.Rect.Width - a_X; } } if (KK.Key == ConsoleKey.Z) { a_W--; if (a_W < 10) { a_W = 10; } } if (KK.Key == ConsoleKey.S) { a_H++; if (a_H > cimg.Rect.Height - a_Y) { a_H = cimg.Rect.Height - a_Y; } } if (KK.Key == ConsoleKey.X) { a_H--; if (a_H < 10) { a_H = 10; } } } } var rect = DRectangle.CenteredRect(a_X, a_Y, a_W, a_H); Console.WriteLine("Set RECT:" + a_X + " " + a_Y + " " + a_W + " " + a_H); //显示图片 win.SetImage(cimg); win.ClearOverlay(); //显示框 win.AddOverlay(rect); } } //选择追踪对象 while (!win.IsClosed()) { //获得1帧图片 temp = cap.RetrieveMat();// new Mat(); if (temp == null) { Console.WriteLine("图像获取错误!"); return; } var array = new byte[temp.Width * temp.Height * temp.ElemSize()]; Marshal.Copy(temp.Data, array, 0, array.Length); using (var cimg = Dlib.LoadImageData <BgrPixel>(array, (uint)temp.Height, (uint)temp.Width, (uint)(temp.Width * temp.ElemSize()))) { //更新追踪图像 tracker.Update(cimg); win.SetImage(cimg); win.ClearOverlay(); //获得追踪到的目标位置 DRectangle rect = tracker.GetPosition(); win.AddOverlay(rect); Console.WriteLine("OBJ RECT:" + (int)rect.Left + " " + (int)rect.Top + " " + (int)rect.Width + " " + (int)rect.Height); System.Threading.Thread.Sleep(100); } } } Console.WriteLine("任意键退出"); Console.ReadKey(); }
public void TryTrack() { const string testName = "TryTrack"; var tests = new[] { new { Type = ImageTypes.UInt8, ExpectResult = true } }; var type = this.GetType().Name; foreach (var input in tests) { var tracker = new CorrelationTracker(); try { var index = 0; foreach (var file in this.GetDataFiles("video_frames").Where(info => info.Name.EndsWith("jpg"))) { var expectResult = input.ExpectResult; var inputType = input.Type; var imageObj = DlibTest.LoadImage(input.Type, file); if (index == 0) { using (var rect = DRectangle.CenteredRect(93, 110, 38, 86)) tracker.StartTrack(imageObj, rect); } var outputImageAction = new Func <bool, Array2DBase>(expect => { if (index != 0) { tracker.Update(imageObj); } return(imageObj); }); var successAction = new Action <Array2DBase>(image => { if (index != 0) { tracker.Update(image); } using (var r = tracker.GetPosition()) { #if DEBUG using (var tmp = Image.FromFile(file.FullName)) using (var bmp = new Bitmap(tmp)) using (var g = Graphics.FromImage(bmp)) using (var p = new Pen(Color.Red, 3f)) { g.DrawRectangle(p, (float)r.Left, (float)r.Top, (float)r.Width, (float)r.Height); bmp.Save(Path.Combine(this.GetOutDir(type), $"{Path.GetFileNameWithoutExtension(file.FullName)}.jpg")); } #endif } }); var failAction = new Action(() => { Assert.Fail($"{testName} should throw excption for InputType: {inputType}."); }); var finallyAction = new Action(() => { index++; if (imageObj != null) { this.DisposeAndCheckDisposedState(imageObj); } }); var exceptionAction = new Action(() => { Console.WriteLine($"Failed to execute {testName} to InputType: {inputType}."); }); DoTest(outputImageAction, expectResult, successAction, finallyAction, failAction, exceptionAction); } } finally { this.DisposeAndCheckDisposedState(tracker); } } }