private void paintTimer_Tick(object sender, EventArgs e) { if (_trackedToPaint == null) { return; } if (_sequence == _lastSequence) { return; } _lastSequence = _sequence; foreach (TrackedColor trackedColor in _trackedToPaint) { TrackingData data = _trackingData[trackedColor.ColorIndex]; updateRow(data); } ////update stats lblTotalTracked.Text = _matchesTotal.ToString(); lblOutOfRangeTracked.Text = (_matchesTotal - _matchesFiltered).ToString(); lblInRangeTracked.Text = _matchesFiltered.ToString(); lblFrameCount.Text = _frameCount.ToString(); if (_frameCount > 0) { lblMatchesPerFrame.Text = (_matchesTotal / _frameCount).ToString(); } //force actual tracking to repaint pnlTrackedColors.Invalidate(); }
private static void updateRow(TrackingData data) { if (data.LvItem != null) { data.LvItem.SubItems[2].Text = data.MatchesTotal.ToString(); data.LvItem.SubItems[3].Text = data.MatchesFiltered.ToString(); } }
private void objectsDetected(object sender, ObjectsDetectedEventArgs args) { //update the tracking data. Actual painting is done off a timer to avoid killing CPU if (InvokeRequired) { BeginInvoke(new EventHandler <ObjectsDetectedEventArgs>(objectsDetected), new object[] { sender, args }); return; } _sequence++; var trackedColors = new List <TrackedColor>(args.TrackedColors); if (!_isPausing) { _trackedToPaint = new List <TrackedColor>(); } int lastMatchesTotal = _matchesTotal; foreach (TrackedColor trackedColor in trackedColors) { Debug.WriteLine("ColorIndex: " + (trackedColor.ColorIndex + 1)); if (trackedColor.ColorIndex < 0 || trackedColor.ColorIndex >= _trackingData.Count) { Debug.WriteLine(string.Format("Bad tracking index: {0}", trackedColor.ColorIndex)); continue; } TrackingData data = _trackingData[trackedColor.ColorIndex]; _matchesTotal++; data.MatchesTotal++; if ((trackedColor.Bounds.Width * trackedColor.Bounds.Height) < nudAreaFilter.Value) { //skip it - too small continue; } data.MatchesFiltered++; _matchesFiltered++; if (!_isPausing) { _trackedToPaint.Add(trackedColor); } } if (lastMatchesTotal != _matchesTotal) { _frameCount++; } }
void pnlTrackedColors_Paint(object sender, PaintEventArgs e) { e.Graphics.FillRectangle(Brushes.White, 0, 0, pnlTrackedColors.Size.Width, pnlTrackedColors.Size.Height); if (_trackedToPaint == null) { return; } //sort to put little before big _trackedToPaint.Sort(new Comparison <TrackedColor>(areaCompare)); foreach (TrackedColor trackedColor in _trackedToPaint) { TrackingData data = _trackingData[trackedColor.ColorIndex]; updateRow(data); var bounds = new Rectangle( trackedColor.Bounds.X * SCALING, trackedColor.Bounds.Y * SCALING, trackedColor.Bounds.Width * SCALING, trackedColor.Bounds.Height * SCALING); var panelBounds = new Rectangle(new Point(0, 0), pnlTrackedColors.Size); if (!panelBounds.Contains(bounds)) { Debug.WriteLine(string.Format("Box cordinates are out of bounds: {0}", trackedColor)); } if (cbSolid.Checked) { Brush blobBrush = cbUseColor.Checked ? data.Brush : Brushes.Gray; e.Graphics.FillRectangle(blobBrush, bounds); e.Graphics.DrawRectangle(Pens.Black, bounds); } else { Pen pen = cbUseColor.Checked ? data.Pen : Pens.Black; e.Graphics.DrawRectangle(pen, bounds); } //This determines a good color for writting inside the blob if needbe //Brush brush = null; //BlobStyle penType = (BlobStyle) ((cbUseColor.Checked ? 0x2 : 0x0) + (cbSolid.Checked ? 0x1 : 0x0)); //switch (penType) //{ // case BlobStyle.NoColorOutline: brush = Brushes.Black; break; // case BlobStyle.NoColorSolid: brush = Brushes.White; break; // case BlobStyle.ColorOutline: brush = data.Brush; break; // case BlobStyle.ColorSolid: brush = data.Color.GetBrightness() > 0.5 ? Brushes.Black : Brushes.White; break; //} var lines = new List <string>(8); //put the number in the middle if (cbColorNumber.Checked) { lines.Add(string.Format("C={0}", (trackedColor.ColorIndex + 1))); } if (cbLocation.Checked) { lines.Add(string.Format("X={0} Y={1}", trackedColor.Bounds.X, trackedColor.Bounds.Y)); } if (cbSize.Checked) { lines.Add(string.Format("W={0} H={1}", trackedColor.Bounds.Width, trackedColor.Bounds.Height)); } if (cbArea.Checked) { lines.Add(string.Format("A={0}", getArea(trackedColor))); } if (lines.Count > 0) { //assume most characters = longest line string longest = ""; foreach (string line in lines) { if (line.Length >= longest.Length) { longest = line; } } //measuresize gives short results sometimes so try this method int textWidth = 10 + MeasureDisplayStringWidth(e.Graphics, longest, lblDummyDetail.Font); int textHeight = lblDummyDetail.Font.Height * lines.Count; int offset = 5; //assume the text will fit Bottom Right Rectangle rect = new Rectangle(bounds.Right + offset, bounds.Bottom + offset, textWidth, textHeight); //make it on screen Point joinStart; Point joinEnd; if (panelBounds.Contains(rect)) { joinStart = new Point(bounds.X + bounds.Width / 2, bounds.Bottom); joinEnd = new Point(rect.X, rect.Y + rect.Height / 2); } else { if (bounds.X + (bounds.Width / 2) < panelBounds.Width / 2) { //box is on Bottom Left so put the text Top Right rect = new Rectangle(bounds.Right + offset, bounds.Top - (offset + textHeight), textWidth, textHeight); joinStart = new Point(bounds.X + bounds.Width / 2, bounds.Y); joinEnd = new Point(rect.X, rect.Y + rect.Height / 2); } else { if (bounds.Y + (bounds.Height / 2) < panelBounds.Height / 2) { //box is on the Top Right so put the text Bottom Left rect = new Rectangle(bounds.X - (offset + textWidth), bounds.Bottom + offset, textWidth, textHeight); joinStart = new Point(bounds.X + bounds.Width / 2, bounds.Bottom); joinEnd = new Point(rect.Right, rect.Y + rect.Height / 2); } else { //box is on the Bottom Right so put the text Top Left rect = new Rectangle(bounds.X - (offset + textWidth), bounds.Top - (offset + textHeight), textWidth, textHeight); joinStart = new Point(bounds.X + bounds.Width / 2, bounds.Y); joinEnd = new Point(rect.Right, rect.Y + rect.Height / 2); } } } e.Graphics.DrawRectangle(_detailPen, rect); e.Graphics.DrawString(string.Join("\n", lines.ToArray()), lblDummyDetail.Font, _detailBrush, rect); e.Graphics.DrawLine(_detailPen, joinStart, joinEnd); } } }