private void initTracking(Bgr <byte>[,] frame) { initializeKalman(roi.Center()); //get hue channel from search area var hsvImg = frame.ToHsv(); //user constraints... Gray <byte>[,] mask = hsvImg.InRange(new Hsv <byte>(0, 0, (byte)minV), new Hsv <byte>(0, 0, (byte)maxV), Byte.MaxValue, 2); originalObjHist.Calculate(hsvImg.SplitChannels <Hsv <byte>, byte>(roi, 0, 1), false, mask, roi.Location); originalObjHist.Scale((float)1 / roi.Area()); //originalObjHist.Normalize(Byte.MaxValue); var backgroundArea = roi.Inflate(1.5, 1.5, frame.Size()); var backgroundMask = mask.Clone(backgroundArea); backgroundMask.SetValue <Gray <byte> >(0, new Rectangle(roi.X - backgroundArea.X, roi.Y - backgroundArea.Y, roi.Width, roi.Height)); backgroundHist.Calculate(hsvImg.SplitChannels <Hsv <byte>, byte>(backgroundArea, 0, 1), false, mask, backgroundArea.Location); backgroundHist.Scale((float)1 / (backgroundArea.Area() - roi.Area())); //backgroundHist.Normalize(Byte.MaxValue); //how good originalObjHist and objHist match (suppresses possible selected background) ratioHist = originalObjHist.CreateRatioHistogram(backgroundHist, Byte.MaxValue, 3); searchArea = roi; roi = Rectangle.Empty; }
/// <summary> /// Creates template from the input image by using provided parameters. /// </summary> /// <param name="sourceImage">Input image.</param> /// <param name="minFeatureStrength">Minimum gradient value for the feature.</param> /// <param name="maxNumberOfFeatures">Maximum number of features per template. The features will be extracted so that their locations are semi-uniformly spread.</param> /// <param name="classLabel">Template class label.</param> /// <param name="angle"></param> public override void Initialize(Gray<byte>[,] sourceImage, int minFeatureStrength, int maxNumberOfFeatures, string classLabel, float angle) { base.Initialize(sourceImage, minFeatureStrength, maxNumberOfFeatures, classLabel, angle); this.BinaryMask = sourceImage.Clone(BoundingRect); if (this.BinaryMask[0, 0].Intensity != 0) //background should be black BinaryMask = this.BinaryMask.Not(); this.BinaryMask = this.BinaryMask.ThresholdToZero((byte)(255 * 0.75), (byte)255); //if Gauss kernel was applied... }