public void SetAspectRatio() { const double left = 0; const double top = 0; const double right = 100; const double bottom = 100; var rect = new DRectangle(left, top, right, bottom); rect = DRectangle.SetAspectRatio(rect, 2); Assert.AreEqual(rect.Left, -20.917784899841294); Assert.AreEqual(rect.Top, 14.791107550079353); Assert.AreEqual(rect.Right, 120.91778489984129); Assert.AreEqual(rect.Bottom, 85.208892449920654); try { rect = DRectangle.SetAspectRatio(rect, 0); Assert.Fail($"{nameof(DRectangle.SetAspectRatio)} should throw {nameof(ArgumentOutOfRangeException)} if ration is 0"); } catch (ArgumentOutOfRangeException e) { } }
public void OperatorAdd() { var lleft = (double)this.NextRandom(0, 100); var ltop = (double)this.NextRandom(0, 100); var lright = lleft * 2; var lbottom = ltop * 2; var rleft = (double)this.NextRandom(0, 100); var rtop = (double)this.NextRandom(0, 100); var rright = rleft * 2; var rbottom = rtop * 2; var r = new DRectangle(lleft, ltop, lright, lbottom); var l = new DRectangle(rleft, rtop, rright, rbottom); var rl = r + l; Assert.AreEqual(rl.Left, Math.Min(l.Left, r.Left)); Assert.AreEqual(rl.Top, Math.Min(l.Top, r.Top)); Assert.AreEqual(rl.Right, Math.Max(l.Right, r.Right)); Assert.AreEqual(rl.Bottom, Math.Max(l.Bottom, r.Bottom)); this.DisposeAndCheckDisposedState(rl); this.DisposeAndCheckDisposedState(l); this.DisposeAndCheckDisposedState(r); }
public void OperatorEqual() { for (var left = 1; left <= 100; left++) { for (var top = 1; top <= 100; top++) { var right = left * 2; var bottom = top * 2; var r = new DRectangle(left, top, right, bottom); var l = new DRectangle(left, top, right, bottom); var l1 = new DRectangle(left, top, right * 2, bottom); var l2 = new DRectangle(left, top, right, bottom * 2); var l3 = new DRectangle(left * 2, top, right, bottom); var l4 = new DRectangle(left, top * 2, right, bottom); Assert.AreEqual(r, l, $"left: {left}, top: {top}, right: {right}, bottom: {bottom}"); Assert.AreNotEqual(r, l1, $"left: {left}, top: {top}, right: {right}, bottom: {bottom}"); Assert.AreNotEqual(r, l2, $"left: {left}, top: {top}, right: {right}, bottom: {bottom}"); Assert.AreNotEqual(r, l3, $"left: {left}, top: {top}, right: {right}, bottom: {bottom}"); Assert.AreNotEqual(r, l4, $"left: {left}, top: {top}, right: {right}, bottom: {bottom}"); } } }
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); } } }
public void Create1() { var rect = new DRectangle(); }
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(); }
private static int ClusterDataset(CommandLineApplication commandLine) { // make sure the user entered an argument to this program if (commandLine.RemainingArguments.Count != 1) { Console.WriteLine("The --cluster option requires you to give one XML file on the command line."); return(ExitFailure); } var clusterOption = commandLine.Option("-cluster|--cluster", "", CommandOptionType.SingleValue); var sizeOption = commandLine.Option("-size|--size", "", CommandOptionType.SingleValue); var clusterValue = clusterOption.HasValue() ? clusterOption.Value() : "2"; var sizeValue = sizeOption.HasValue() ? sizeOption.Value() : "8000"; if (!uint.TryParse(clusterValue, out var numClusters)) { return(ExitFailure); } if (!uint.TryParse(sizeValue, out var chipSize)) { return(ExitFailure); } var argument = commandLine.RemainingArguments[0]; using (var data = Dlib.ImageDatasetMetadata.LoadImageDatasetMetadata(argument)) { Environment.CurrentDirectory = Path.GetDirectoryName(argument); double aspectRatio = MeanAspectRatio(data); var images = new Array <Array2D <RgbPixel> >(); var feats = new List <Matrix <double> >(); var dataImages = data.Images; //console_progress_indicator pbar(dataImages.Length); // extract all the object chips and HOG features. Console.WriteLine("Loading image data..."); for (var i = 0; i < dataImages.Length; ++i) { //pbar.print_status(i); if (!HasNonIgnoredBoxes(dataImages[i])) { continue; } using (var img = Dlib.LoadImage <RgbPixel>(dataImages[i].FileName)) { var boxes = dataImages[i].Boxes; for (var j = 0; j < boxes.Length; ++j) { if (boxes[j].Ignore || boxes[j].Rect.Area < 10) { continue; } var rect = new DRectangle(boxes[j].Rect); rect = DRectangle.SetAspectRatio(rect, aspectRatio); using (var chipDetail = new ChipDetails(rect, chipSize)) { var chip = Dlib.ExtractImageChip <RgbPixel>(img, chipDetail); Dlib.ExtractFHogFeatures <RgbPixel>(chip, out var feature); feats.Add(feature); images.PushBack(chip); } } } } if (!feats.Any()) { Console.WriteLine("No non-ignored object boxes found in the XML dataset. You can't cluster an empty dataset."); return(ExitFailure); } Console.WriteLine("\nClustering objects..."); var assignments = AngularCluster(feats, numClusters).ToList(); // Now output each cluster to disk as an XML file. for (uint c = 0; c < numClusters; ++c) { // We are going to accumulate all the image metadata for cluster c. We put it // into idata so we can sort the images such that images with central chips // come before less central chips. The idea being to get the good chips to // show up first in the listing, making it easy to manually remove bad ones if // that is desired. var idata = new List <Pair <double, Image> >(dataImages.Length); var idx = 0; for (var i = 0; i < dataImages.Length; ++i) { idata.Add(new Pair <double, Image> { Second = new Image() }); idata[i].First = double.PositiveInfinity; idata[i].Second.FileName = dataImages[i].FileName; if (!HasNonIgnoredBoxes(dataImages[i])) { continue; } var idataBoxes = new List <Box>(); var boxes = dataImages[i].Boxes; for (var j = 0; j < boxes.Length; ++j) { idataBoxes.Add(boxes[j]); if (boxes[j].Ignore || boxes[j].Rect.Area < 10) { continue; } // If this box goes into cluster c then update the score for the whole // image based on this boxes' score. Otherwise, mark the box as // ignored. if (assignments[idx].C == c) { idata[i].First = Math.Min(idata[i].First, assignments[idx].Distance); } else { idataBoxes.Last().Ignore = true; } ++idx; } idata[i].Second.Boxes = idataBoxes.ToArray(); } // now save idata to an xml file. idata.Sort((a, b) => { var diff = a.First - b.First; return(diff > 0 ? 1 : diff < 0 ? -1 : 0); }); using (var cdata = new Dataset()) { cdata.Comment = $"{data.Comment}\n\n This file contains objects which were clustered into group {c + 1} of {numClusters} groups with a chip size of {chipSize} by imglab."; cdata.Name = data.Name; var cdataImages = new List <Image>(); for (var i = 0; i < idata.Count; ++i) { // if this image has non-ignored boxes in it then include it in the output. if (!double.IsPositiveInfinity(idata[i].First)) { cdataImages.Add(idata[i].Second); } } cdata.Images = cdataImages.ToArray(); var outfile = $"cluster_{c + 1:D3}.xml"; Console.WriteLine($"Saving {outfile}"); Dlib.ImageDatasetMetadata.SaveImageDatasetMetadata(cdata, outfile); } } // Now output each cluster to disk as a big tiled jpeg file. Sort everything so, just // like in the xml file above, the best objects come first in the tiling. assignments.Sort(); for (uint c = 0; c < numClusters; ++c) { var temp = new Array <Array2D <RgbPixel> >(); for (var i = 0; i < assignments.Count(); ++i) { if (assignments[i].C == c) { temp.PushBack(images[(int)assignments[i].Index]); } } var outfile = $"cluster_{c + 1:D3}.jpg"; Console.WriteLine($"Saving {outfile}"); using (var tile = Dlib.TileImages(temp)) Dlib.SaveJpeg(tile, outfile); } } return(ExitSuccess); }
public void Create1() { var rect = new DRectangle(); this.DisposeAndCheckDisposedState(rect); }
public void UpdateNoscale() { const string testName = "UpdateNoscale"; var path = this.GetDataFile($"{LoadTarget}.bmp"); var tests = new[] { new { Type = ImageTypes.BgrPixel, ExpectResult = true }, new { Type = ImageTypes.RgbPixel, ExpectResult = true }, new { Type = ImageTypes.RgbAlphaPixel, ExpectResult = false }, new { Type = ImageTypes.UInt8, ExpectResult = true }, new { Type = ImageTypes.UInt16, ExpectResult = true }, new { Type = ImageTypes.UInt32, ExpectResult = true }, new { Type = ImageTypes.Int8, ExpectResult = true }, new { Type = ImageTypes.Int16, ExpectResult = true }, new { Type = ImageTypes.Int32, ExpectResult = true }, new { Type = ImageTypes.HsiPixel, ExpectResult = true }, new { Type = ImageTypes.Float, ExpectResult = true }, new { Type = ImageTypes.Double, ExpectResult = true } }; foreach (var input in tests) { var expectResult = input.ExpectResult; var inputType = input.Type; var imageObj = DlibTest.LoadImage(input.Type, path); var tracker = new CorrelationTracker(); var rect = new DRectangle(46, 15, 216, 205); var outputImageAction = new Func <bool, Array2DBase>(expect => { tracker.StartTrack(imageObj, rect); return(imageObj); }); var successAction = new Action <Array2DBase>(image => { tracker.UpdateNoscale(image, rect); }); var failAction = new Action(() => { Assert.Fail($"{testName} should throw exception for InputType: {inputType}."); }); var finallyAction = new Action(() => { this.DisposeAndCheckDisposedState(tracker); 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); } }
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); } } }