private void processImage(Image<Bgr, byte> frame, out Image<Gray, byte> probabilityMap, out Rectangle prevSearchArea, out Box2D foundBox) { prevSearchArea = searchArea; //convert to HSV var hsvImg = frame.Convert<Hsv, byte>(); //<<parallel operation>> //back-project ratio hist => create probability map probabilityMap = ratioHist.BackProject(hsvImg.SplitChannels(0, 1)); //or new Image<Gray, byte>[]{ hsvImg[0], hsvImg[1]...} //<<parallel operation>> //user constraints... Image<Gray, byte> mask = hsvImg.InRange(new Hsv(0, 0, minV), new Hsv(0, 0, maxV), Byte.MaxValue, 2); probabilityMap.And(mask, inPlace:true); //run Camshift algorithm to find new object position, size and angle foundBox = Camshift.Process(probabilityMap, searchArea); //<<parallel operation>> var foundArea = Rectangle.Round(foundBox.GetMinArea()); searchArea = foundArea.Inflate(0.05, 0.05, frame.Size); //inflate found area for search (X factor)... if (searchArea.IsEmpty) isROISelected = false; //reset tracking }
private void trackCamshift(Image<Bgr, byte> frame, Rectangle searchArea, out Image<Gray, byte> probabilityMap, out Box2D foundBox) { const int PROBABILITY_MIN_VAL = (int)(0.3f * Byte.MaxValue); //convert to HSV var hsvImg = frame.Convert<Hsv, byte>(); //<<parallel operation>> //back-project ratio hist => create probability map probabilityMap = ratioHist.BackProject(hsvImg.SplitChannels(0, 1)); //or new Image<Gray, byte>[]{ hsvImg[0], hsvImg[1]...} //<<parallel operation>> //user constraints... Image<Gray, byte> mask = hsvImg.InRange(new Hsv(0, 0, minV), new Hsv(0, 0, maxV), Byte.MaxValue, 2); probabilityMap.And(mask, inPlace: true); //run Camshift algorithm to find new object position, size and angle CentralMoments centralMoments; foundBox = Camshift.Process(probabilityMap, searchArea, Meanshift.DEFAULT_TERM, out centralMoments); //<<parallel operation>> //stopping conditions float avgIntensity = centralMoments.Mu00 / (foundBox.Size.Area() + Single.Epsilon); if (avgIntensity < PROBABILITY_MIN_VAL || foundBox.Size.IsEmpty || foundBox.GetMinArea().Area() < 5 * 5) { foundBox = Box2D.Empty; //invalid box } }