private void display_MouseUp(object sender, MouseButtonEventArgs e) { isPressed = false; rectangle.Width = 0; rectangle.Height = 0; float x = (float)((float)startDisplay.X < endDisplay.X ? startDisplay.X : endDisplay.X) / (float)display.ActualWidth; float y = (float)((float)startDisplay.Y < endDisplay.Y ? startDisplay.Y : endDisplay.Y) / (float)display.ActualHeight; float width = ((float)Math.Abs(startDisplay.X - endDisplay.X)) / (float)display.ActualWidth; float height = ((float)Math.Abs(startDisplay.Y - endDisplay.Y)) / (float)display.ActualHeight; model = MatchUtil.GetModel(firstFrame, x, y, width, height); int iX = (int)Math.Floor(x * firstFrame.Width); int iY = (int)Math.Floor(y * firstFrame.Height); int iWidth = (int)Math.Ceiling(width * firstFrame.Width); int iHeight = (int)Math.Ceiling(height * firstFrame.Height); var rec = new System.Drawing.Rectangle(iX, iY, iWidth, iHeight); var im = firstFrame.ToImage <Bgr, byte>(); im.ROI = rec; display.Source = ToBitmapSource(firstFrame); colorObj = Tracking.GetColorObject(firstFrame, new System.Drawing.Rectangle(iX, iY, iWidth, iHeight)); }
public static ColorObject GetColorObject(Mat image, Rectangle rect) { var obj = new ColorObject(); obj.X = rect.X + rect.Width / 2; obj.Y = rect.Y + rect.Height / 2; var hsvMax = new List <Hsv>(); var hsvMin = new List <Hsv>(); var hsvMean = new List <Hsv>(); var hsvCount = new List <int>(); var hsvImage = image.ToImage <Hsv, byte>(); for (var i = rect.Left; i < rect.Right; i++) { for (var j = rect.Top; j < rect.Bottom; j++) { var color = hsvImage[j, i]; var found = false; for (var n = 0; n < hsvMean.Count; n++) { var mean = hsvMean[n]; double diff = 0; diff = diff + 2 * (color.Hue - mean.Hue) * (color.Hue - mean.Hue); diff = diff + (color.Satuation - mean.Satuation) * (color.Satuation - mean.Satuation); diff = diff + (color.Value - mean.Value) * (color.Value - mean.Value); if (diff <= HsvThreshold) { var max = hsvMax[n]; max.Hue = max.Hue >= color.Hue ? max.Hue : color.Hue; max.Satuation = max.Satuation >= color.Satuation ? max.Satuation : color.Satuation; max.Value = max.Value >= color.Value ? max.Value : color.Value; hsvMax[n] = max; var min = hsvMin[n]; min.Hue = min.Hue <= color.Hue ? min.Hue : color.Hue; min.Satuation = min.Satuation <= color.Satuation ? min.Satuation : color.Satuation; min.Value = min.Value <= color.Value ? min.Value : color.Value; hsvMin[n] = min; var count = hsvCount[n]; mean.Hue = (mean.Hue * count + color.Hue) / (count + 1); mean.Satuation = (mean.Satuation * count + color.Satuation) / (count + 1); mean.Value = (mean.Value * count + color.Value) / (count + 1); hsvMean[n] = mean; hsvCount[n]++; found = true; break; } } if (!found) { hsvMax.Add(color); hsvMin.Add(color); hsvMean.Add(color); hsvCount.Add(1); } } } int index = 0; int total = 0; for (var n = 0; n < hsvCount.Count; n++) { if (total < hsvCount[n]) { index = n; total = hsvCount[n]; } } if (index < hsvCount.Count) { obj.MinHsv = hsvMin[index]; obj.MaxHsv = hsvMax[index]; } return(obj); }
public static Image <Gray, byte> Color(Mat prevImage, Mat curImage, ColorObject obj) { var image = curImage.ToImage <Hsv, byte>().InRange(obj.MinHsv, obj.MaxHsv); return(Blur(image)); }