public TrackedObject([NotNull] Rect2d rect, [NotNull] string label, [NotNull] CameraIntrinsic intrinsic, [NotNull] CameraExtrinsic extrinsic, int frameHeigth) { if (rect == null) { throw new ArgumentNullException(nameof(rect)); } if (label == null) { throw new ArgumentNullException(nameof(label)); } if (intrinsic == null) { throw new ArgumentNullException(nameof(intrinsic)); } if (extrinsic == null) { throw new ArgumentNullException(nameof(extrinsic)); } if (frameHeigth == 0) { throw new ArgumentOutOfRangeException(nameof(frameHeigth)); } Label = label; Rect = rect; Intrinsic = intrinsic; Extrinsic = extrinsic; FrameHeight = frameHeigth; }
private void RenderPerTexture(EntityCache cache, Rect2d blockRange) { bool firstRender; for (int depth = EntityCache.DISTANCE_COUNT - 1; depth >= 0; depth--) { for (int rtype = 0; rtype < EntityCache.RenderTypeCount; rtype++) { firstRender = true; foreach (Entity e in cache.GetEntitiesAt(depth, rtype)) { if (blockRange.Includes(e.position)) { if (firstRender || rtype == (int)EntityRenderType.BRT_DYNAMIC) { e.GetCurrentTexture().bind(); firstRender = false; } RenderNoBindRectangle(e.GetCorrectTexturePosition(), e.GetCurrentTexture(), e.GetDistance(), e.GetTransparency()); } } } } }
public virtual TrackedObject Initialize(CameraFrame frame, Rect2d rect, string label) { if (frame == null) { throw new ArgumentNullException(nameof(frame)); } if (rect == null) { throw new ArgumentNullException(nameof(rect)); } if (label == null) { throw new ArgumentNullException(nameof(label)); } Tracker tracker = CreateTracker(); bool initialized = Initialize(tracker, frame.Mat, rect); if (!initialized) { Debug.LogError("Tracker initialization failed"); } TrackedObject trackedObject = new TrackedObject(rect, label, frame.Intrinsic, frame.Extrinsic, frame.Height); CvTrackedObject cvTrackedObject = new CvTrackedObject(trackedObject, tracker); CvTrackedObjects.Add(cvTrackedObject); Debug.Log($"Initialized tracker with status: {initialized}"); return(trackedObject); }
public Vec2d GetDistanceTo(Rect2d rect) { double vecX; double vecY; if (rect.br.X < this.tl.X) { vecX = rect.br.X - this.tl.X; } else if (rect.tl.X > this.br.X) { vecX = rect.tl.X - this.br.X; } else { vecX = 0; } if (rect.br.Y > this.tl.Y) { vecY = rect.br.Y - this.tl.Y; } else if (rect.tl.Y < this.br.Y) { vecY = rect.tl.Y - this.br.Y; } else { vecY = 0; } return(new Vec2d(vecX, vecY)); }
public void Calculate(Rect2d target, int window_width, int window_height, double mapW, double mapH, bool moveDown, bool capCorrection = true) { Rect2d cameraBox = GetOffsetBox(window_width, window_height); Vec2d playerPos = target.GetMiddle(); // Wenn man aus der Mittleren Box rausläuft - Offset Verschieben //############################################################################### Vec2d moveCorrection = cameraBox.GetDistanceTo(playerPos); if (moveDown && moveCorrection.Y == 0 && playerPos.Y != cameraBox.tl.Y) { moveCorrection.Y -= 5; } // Offset NICHT verschieben wenn amn damit die Grenzen der aktuellen Zone verletzten Würde //############################################################################### Vec2d zoneCorrection = GetVisionZoneCorrection(Value + moveCorrection, playerPos, window_width, window_height); Vec2d realCorrection = moveCorrection + zoneCorrection; if (capCorrection) { realCorrection.DoMaxLength(MAX_CORRECTION_SPEED); } if (realCorrection.GetLength() < 0.001) { realCorrection = Vec2d.Zero; } Value += realCorrection; }
private double DoXCollisionMove(Vec2d vec) { Rect2d newpos = new Rect2d(new Vec2d(ent.position.X + vec.X, ent.position.Y), ent.width, ent.height); // TEST ENTITIES foreach (Entity e in owner.GetSurroundingEntityList(ent.GetCollisionMapPosition())) { if (e != ent && Entity.TestBlocking(e, ent) && newpos.IsColldingWith(e.GetPosition())) { return(ent.GetPosition().GetDistanceTo(e.GetPosition()).X); } } //TEST BLOCKS IN RANGE int left = (int)((newpos.br.X - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int bottom = (int)((newpos.br.Y - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); int right = (int)Math.Ceiling((newpos.tl.X + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int top = (int)Math.Ceiling((newpos.tl.Y + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Block b = owner.GetBlock(x, y); if (b != null && Entity.TestBlocking(b, ent) && newpos.IsColldingWith(b.GetPosition())) { return(ent.GetPosition().GetDistanceTo(b.GetPosition()).X); } } } return(vec.X); }
public void CanCreateFromValues() { var x = 1; var y = 2; var width = 3; var height = 4; var anchor = Anchor2d.MiddleRight; var rect = new Rect2d(x, y, width, height); DolphAssert.EqualF(x, rect.X); DolphAssert.EqualF(y, rect.Y); DolphAssert.EqualF(width, rect.Width); DolphAssert.EqualF(height, rect.Height); rect = new Rect2d(x, y, width, height, anchor); DolphAssert.EqualF(x, rect.X); DolphAssert.EqualF(y, rect.Y); DolphAssert.EqualF(width, rect.Width); DolphAssert.EqualF(height, rect.Height); Assert.Equal(anchor, rect.Origin.Anchor); rect = new Rect2d(x, y, width, height, new Origin2d(anchor)); DolphAssert.EqualF(x, rect.X); DolphAssert.EqualF(y, rect.Y); DolphAssert.EqualF(width, rect.Width); DolphAssert.EqualF(height, rect.Height); Assert.Equal(anchor, rect.Origin.Anchor); }
protected virtual void RenderNoBindRectangle(Rect2d rect, OGLTexture texture, double distance, double transparency) { Rect2d coords = texture.GetCoordinates(); if (transparency < 1.0) { GL.Color4(1.0, 1.0, 1.0, transparency); } //########## GL.Begin(BeginMode.Quads); //########## GL.TexCoord2(coords.bl); GL.Vertex3(rect.tl.X, rect.tl.Y, distance); GL.TexCoord2(coords.tl); GL.Vertex3(rect.bl.X, rect.bl.Y, distance); GL.TexCoord2(coords.tr); GL.Vertex3(rect.br.X, rect.br.Y, distance); GL.TexCoord2(coords.br); GL.Vertex3(rect.tr.X, rect.tr.Y, distance); //########## GL.End(); //########## if (transparency < 1.0) { GL.Color4(1.0, 1.0, 1.0, 1.0); } }
public void CanCreateFromPositionAndSize() { var pos = new Position2d(1, 2); var size = new Size2d(3, 4); var anchor = Anchor2d.Center; var rect = new Rect2d(pos, size); DolphAssert.EqualF(pos.X, rect.X); DolphAssert.EqualF(pos.Y, rect.Y); DolphAssert.EqualF(size.Width, rect.Width); DolphAssert.EqualF(size.Height, rect.Height); rect = new Rect2d(pos, size, anchor); DolphAssert.EqualF(pos.X, rect.X); DolphAssert.EqualF(pos.Y, rect.Y); DolphAssert.EqualF(size.Width, rect.Width); DolphAssert.EqualF(size.Height, rect.Height); Assert.Equal(anchor, rect.Origin.Anchor); rect = new Rect2d(pos, size, new Origin2d(anchor)); DolphAssert.EqualF(pos.X, rect.X); DolphAssert.EqualF(pos.Y, rect.Y); DolphAssert.EqualF(size.Width, rect.Width); DolphAssert.EqualF(size.Height, rect.Height); Assert.Equal(anchor, rect.Origin.Anchor); }
public void ContainsTopLeft() { var rect = new Rect2d(10, 10, 100, 100); Assert.True(rect.Contains(rect.TopLeft)); Assert.True(rect.Contains(rect.Left, rect.Top)); }
public DetectedObject([NotNull] DetectedObject detectedObject) { Rect = detectedObject.Rect.clone(); Label = detectedObject.Label; Probability = detectedObject.Probability; Frame = detectedObject.Frame; }
/// <summary> /// 打开摄像头传输图像 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Btn_Parameter_Click(object sender, EventArgs e) { th = new Thread(Cap_Run); Isopen = 1; th.Start(); bbox = new Rect2d(0, 0, bboxSize, bboxSize); }
public void AddMany2() { var bbox1 = new Rect2d(10, 10, 200, 200); var bbox2 = new Rect2d(300, 300, 100, 100); var bboxArray = new Rect2d[] { bbox1, bbox2, }; using (var mt = MultiTracker.Create()) { using (var tracker1 = TrackerMIL.Create()) using (var vc = Image("lenna.png")) { var ret = mt.Add( new Tracker[] { tracker1, tracker1, }, vc, bboxArray); Assert.False(ret); // duplicate init call } } using (var mt = MultiTracker.Create()) { using (var tracker1 = TrackerMIL.Create()) using (var tracker2 = TrackerMIL.Create()) using (var vc = Image("lenna.png")) { var ret = mt.Add( new Tracker[] { tracker1, tracker2, }, vc, bboxArray); Assert.True(ret); } } }
public bool IsTouching(Rect2d rect) { return((this.tl.X == rect.br.X && this.tl.X > rect.tl.X) || (this.br.X == rect.tl.X && this.br.X < rect.br.X) || (this.tl.Y == rect.br.Y && this.tl.Y < rect.tl.Y) || (this.br.Y == rect.tl.Y && this.br.Y > rect.br.Y)); }
public bool IsCollidingLeft(Type exclude = null) { Rect2d newpos = new Rect2d(new Vec2d(position.X - DETECTION_TOLERANCE, position.Y), width, height); //TEST BLOCKS IN RANGE int left = (int)((newpos.br.X - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int bottom = (int)((newpos.br.Y - DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); int right = (int)Math.Ceiling((newpos.tl.X + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_WIDTH) / Block.BLOCK_WIDTH); int top = (int)Math.Ceiling((newpos.tl.Y + DynamicEntity.BLOCK_DETECTION_RANGE * Block.BLOCK_HEIGHT) / Block.BLOCK_HEIGHT); for (int x = left; x < right; x++) { for (int y = bottom; y < top; y++) { Block b = owner.GetBlock(x, y); if (b != null && Entity.TestBlocking(b, this) && newpos.IsColldingWith(b.GetPosition()) && b.GetMiddle().X < this.GetMiddle().X&& (exclude == null || !exclude.IsAssignableFrom(b.GetType()))) { return(true); } } } // TEST ENTITIES foreach (Entity e in owner.GetSurroundingEntityList(GetCollisionMapPosition())) { if (e != this && Entity.TestBlocking(e, this) && newpos.IsColldingWith(e.GetPosition()) && e.GetMiddle().X < this.GetMiddle().X&& (exclude == null || !exclude.IsAssignableFrom(e.GetType()))) { return(true); } } return(false); }
static void Main(string[] args) { var capture = new VideoCapture(@"G:\videodata\char 5s.mp4"); var win = new Window("capture"); var mat = new Mat(); var tracker = TrackerCSRT.Create(); var bbox = new Rect(); var bboxes = new Rect[] { }; capture.Read(mat); bbox = Cv2.SelectROI("capture", mat); //bboxes = Cv2.SelectROIs("capture", mat); //for(int i = 0; i < bboxes.Length; i++) //{ // Console.WriteLine(bboxes[i]); //} //var mt = MultiTracker.Create(); Console.WriteLine(bbox); var bbox2d = new Rect2d(bbox.X, bbox.Y, bbox.Width, bbox.Height); //rect2d to rect tracker.Init(mat, bbox2d); while (true) { capture.Read(mat); // 読み込めるフレームがなくなったら終了 if (mat.Empty()) { break; } tracker.Update(mat, ref bbox2d); Cv2.Rectangle(mat, bbox2d.ToRect(), new Scalar(0, 255, 0)); //for (int i = 0; i < bboxes.Length; i++) //{ // Cv2.Rectangle(mat, bbox2d.ToRect(), new Scalar(0, 255, 0)); //} Console.WriteLine(bbox2d); win.ShowImage(mat); Cv2.WaitKey(33); } Console.ReadKey(); }
/// <summary> /// Add a new object to be tracked. /// </summary> /// <param name="newTracker">tracking algorithm to be used</param> /// <param name="image">input image</param> /// <param name="boundingBox">a rectangle represents ROI of the tracked object</param> /// <returns></returns> public bool Add(Tracker newTracker, InputArray image, Rect2d boundingBox) { if (newTracker == null) { throw new ArgumentNullException(nameof(newTracker)); } if (image == null) { throw new ArgumentNullException(nameof(image)); } newTracker.ThrowIfDisposed(); image.ThrowIfDisposed(); if (newTracker.PtrObj == null) { throw new ArgumentException("newTracker.PtrObj == null", nameof(newTracker)); } NativeMethods.HandleException( NativeMethods.tracking_MultiTracker_add1(ptr, newTracker.PtrObj.CvPtr, image.CvPtr, boundingBox, out var ret)); GC.KeepAlive(newTracker); GC.KeepAlive(image); GC.KeepAlive(this); return(ret != 0); }
public void DoNotContainsBottomRight() { var rect = new Rect2d(10, 10, 100, 100); Assert.False(rect.Contains(rect.BottomRight)); Assert.False(rect.Contains(rect.Right, rect.Bottom)); }
public void Contains() { var rect = new Rect2d(new Point2d(0, 0), new Size2d(3, 3)); // OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, // while the right and bottom boundaries are not. https://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=rect Assert.False(rect.Contains(0, -1)); Assert.False(rect.Contains(-1, 0)); Assert.False(rect.Contains(-1, -1)); Assert.True(rect.Contains(0, 0)); Assert.True(rect.Contains(0, 1)); Assert.True(rect.Contains(1, 0)); Assert.True(rect.Contains(1, 1)); Assert.True(rect.Contains(2, 0)); Assert.True(rect.Contains(2, 1)); Assert.True(rect.Contains(2, 2)); Assert.True(rect.Contains(0, 2)); Assert.True(rect.Contains(1, 2)); Assert.True(rect.Contains(2, 2)); Assert.False(rect.Contains(0, 3)); Assert.False(rect.Contains(1, 3)); Assert.False(rect.Contains(2, 3)); Assert.False(rect.Contains(3, 3)); Assert.False(rect.Contains(3, 0)); Assert.False(rect.Contains(3, 1)); Assert.False(rect.Contains(3, 2)); Assert.False(rect.Contains(3, 3)); }
public static void TrackObject() { var tracker = TrackerMOSSE.Create(); var cap = new VideoCapture(); cap.Open(0); var image = new Mat(); cap.Read(image); Cv2.ImShow("Debug", image); var bbox = Cv2.SelectROI("Tracking", image, false); var bbox2d = new Rect2d(bbox.X, bbox.Y, bbox.Width, bbox.Height); tracker.Init(image, bbox2d); while (true) { var timer = Cv2.GetTickCount(); var img = new Mat(); var success = cap.Read(img); success = tracker.Update(img, ref bbox2d); if (success) { DrawBox(img, bbox2d); } else { Cv2.PutText(img, "Lost", new OpenCvSharp.Point(100, 75), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2); } Cv2.Rectangle(img, new OpenCvSharp.Point(15, 15), new OpenCvSharp.Point(200, 90), new Scalar(255, 0, 255), 2); Cv2.PutText(img, "Fps:", new OpenCvSharp.Point(20, 40), HersheyFonts.HersheySimplex, 0.7, new Scalar(255, 0, 255), 2); Cv2.PutText(img, "Status:", new OpenCvSharp.Point(20, 70), HersheyFonts.HersheySimplex, 0.7, new Scalar(255, 0, 255), 2); var fps = (int)(Cv2.GetTickFrequency() / (Cv2.GetTickCount() - timer)); Scalar myColor; if (fps > 60) { myColor = new Scalar(20, 230, 20); } else if (fps > 20) { myColor = new Scalar(230, 20, 20); } else { myColor = new Scalar(20, 20, 230); } Cv2.PutText(img, fps.ToString(), new OpenCvSharp.Point(75, 40), HersheyFonts.HersheySimplex, 0.7, myColor, 2); Cv2.ImShow("Tracking", img); if (Cv2.WaitKey(1) == 113) { Cv2.DestroyWindow("Tracking"); break; } } }
private Rect2d UpdateTracker([NotNull] CameraFrame frame) { Mat image = frame.Mat; Rect2d rect = new Rect2d(); bool update = _tracker.update(image, rect); return(update ? rect : null); }
public override TrackedObject Initialize(CameraFrame frame, Rect2d rect, string label) { if (frame.Format != ColorFormat.RGB) { throw new ArgumentException("KCF Tracker requires RGB format (OpenCVForUnity)"); } return(base.Initialize(frame, rect, label)); }
public override TrackedObject Initialize(CameraFrame frame, Rect2d rect, string label) { if (frame.Format != ColorFormat.Grayscale) { Debug.LogWarning("TLD Tracker works with grayscale, but the configured color format is RGB"); } return(base.Initialize(frame, rect, label)); }
public void CanConvertToPolygon() { var rect = new Rect2d(100, 200, 300, 400, Anchor2d.BottomCenter); var poly = rect.ToPolygon(); // Extra point from where corner 4 closes back to corner 1 Assert.Equal(5, poly.Points.Count); }
public override TrackedObject Initialize(CameraFrame frame, Rect2d rect, string label) { if (frame.Format != ColorFormat.Grayscale) { Debug.LogWarning("Median Flow Tracker uses grayscale"); } return(base.Initialize(frame, rect, label)); }
public override Rect2d GetPosition() { if (position2dCache == null) { position2dCache = new Rect2d(position, width, height); } return(position2dCache); }
public void BottomRight() { var rect = new Rect2d(10, 10, 100, 100); Assert.Equal(110, rect.Bottom); Assert.Equal(110, rect.Right); Assert.Equal(new Point2d(110, 110), rect.BottomRight); }
public void CanConvertToSize() { var size1 = new Size2d(8, 12); var rect = new Rect2d(new Position2d(4, 4), size1); var size2 = rect.GetSize(); Assert.Equal(size1, size2); }
public void TopLeft() { var rect = new Rect2d(10, 10, 100, 100); Assert.Equal(10, rect.Top); Assert.Equal(10, rect.Left); Assert.Equal(new Point2d(10, 10), rect.TopLeft); }
public void CanGetPositionWithAnchor(Anchor2d anchor, float expectedX, float expectedY) { var rect = new Rect2d(X, Y, W, H); var pos = rect.GetAnchorPosition(anchor); DolphAssert.EqualF(expectedX, pos.X); DolphAssert.EqualF(expectedY, pos.Y); }