private static Rectangle process(Gray<byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria, out CentralMoments centralMoments) { Rectangle imageArea = new Rectangle(0, 0, probabilityMap.Width(), probabilityMap.Height()); Rectangle searchWindow = roi; RawMoments moments = new RawMoments(order: 1); // Mean shift with fixed number of iterations int i = 0; double shift = Byte.MaxValue; while (termCriteria.ShouldTerminate(i, shift) == false && !searchWindow.IsEmptyArea()) { // Locate first order moments moments.Compute(probabilityMap, searchWindow); int shiftX = (int)(moments.CenterX - searchWindow.Width / 2f); int shiftY = (int)(moments.CenterY - searchWindow.Height / 2f); // Shift the mean (centroid) searchWindow.X += shiftX; searchWindow.Y += shiftY; // Keep the search window inside the image searchWindow.Intersect(imageArea); shift = System.Math.Abs((double)shiftX) + System.Math.Abs((double)shiftY); //for term criteria only i++; } if (searchWindow.IsEmptyArea() == false) { // Locate second order moments and perform final shift moments.Order = 2; moments.Compute(probabilityMap, searchWindow); searchWindow.X += (int)(moments.CenterX - searchWindow.Width / 2f); searchWindow.Y += (int)(moments.CenterY - searchWindow.Height / 2f); // Keep the search window inside the image searchWindow.Intersect(imageArea); } centralMoments = new CentralMoments(moments); // moments to be used by camshift return searchWindow; }
/// <summary> /// Computes the Hu moments from the specified central moments. /// </summary> /// /// <param name="moments">The central moments to use as base of calculations.</param> /// public void Compute(CentralMoments moments) { double inv = 1.0 / moments.Mu00; double inv2 = 1.0 / (moments.Mu00 * moments.Mu00); double inv5d2 = System.Math.Sqrt(inv2 * inv2 * inv); float n20 = (float)(moments.Mu20 * inv2); float n02 = (float)(moments.Mu02 * inv2); float n11 = (float)(moments.Mu11 * inv2); float n21 = (float)(moments.Mu21 * inv5d2); float n12 = (float)(moments.Mu12 * inv5d2); float n30 = (float)(moments.Mu30 * inv5d2); float n03 = (float)(moments.Mu03 * inv5d2); // (η20 + η02) I1 = (n20 + n02); // (η20 − η02)² + 4 η11² I2 = (n20 - n02) * (n20 - n02) + 4 * (n11 * n11); // (η30 − 3 η12)² I3 = (n30 - 3 * n12) * (n30 - 3 * n12) // + (3 η21 − η03)² + (3 * n21 - n03) * (3 * n21 - n03); // (η30 + η12)² + (η21 + η03)² I4 = (n30 + n12) * (n30 + n12) + (n21 + n03) * (n21 + n03); // (η30 − 3 η12) (η30 + η12) [(η30 + η12)² −3 (η21 + η03)² ] I5 = (n30 - 3 * n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03)) // (3 η21 − η03) (η21 + η03) [3 (η30 + η12)² − (η21 + η03)² ] + (3 * n21 - n03) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)); // (η20 − η02) [(η30 + η12)² − (η21 + η03)² ] I6 = (n20 - n02) * ((n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)) // + 4 η11 (η30 + η12) (η21 + η03) + 4 * n11 * (n30 + n12) * (n21 + n03); // (3 η21 − η03) (η30 + η12) [(η30 + η12)² − 3 (η21 + η03)² ] I7 = (3 * n21 - n03) * (n30 + n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03)) // - (η30 − 3 η12) (η21 + η03) [3 (η30 + η12)² − (η21 + η03)² ] - (n30 - 3 * n12) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03)); }
/// <summary> /// Computes moments for the provided image. /// </summary> /// <param name="image">Image.</param> /// <param name="area">Area</param> public void Compute(Gray <float>[,] image, Rectangle area) { RawMoments raw = new RawMoments(Order); raw.Compute(image, area); CentralMoments center = new CentralMoments(raw); this.Compute(center); }
/// <summary> /// Camshift algorithm /// </summary> /// <param name="probabilityMap">Probability map [0-255].</param> /// <param name="roi">Initial Search area</param> /// <param name="termCriteria">Mean shift termination criteria (PLEASE DO NOT REMOVE (but you can move it) THIS CLASS; PLEASE!!!)</param> /// <param name="centralMoments">Calculated central moments (up to order 2).</param> /// <returns>Object position, size and angle packed into a structure.</returns> public static Box2D Process(Gray<byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria, out CentralMoments centralMoments) { // Compute mean shift Rectangle objArea = Meanshift.Process(probabilityMap, roi, termCriteria, out centralMoments); //fit ellipse Ellipse ellipse = centralMoments.GetEllipse(); ellipse.Center = objArea.Center(); //return empty structure is the object is lost var sz = ellipse.Size; if (Single.IsNaN(sz.Width) || Single.IsNaN(sz.Height) || sz.Width < 1 || sz.Height < 1) { return Box2D.Empty; } return (Box2D)ellipse; }
/// <summary> /// Meanshift algorithm /// </summary> /// <param name="probabilityMap">Probability map [0-255].</param> /// <param name="roi">Initial search area</param> /// <param name="termCriteria">Mean shift termination criteria</param> /// <param name="centralMoments">Calculated central moments (up to order 2).</param> /// <returns>Object area.</returns> public static Rectangle Process(Gray<byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria, out CentralMoments centralMoments) { return process(probabilityMap, roi, termCriteria, out centralMoments); }
/// <summary> /// Computes moments for the provided image. /// </summary> /// <param name="image">Image.</param> /// <param name="area">Area</param> public void Compute(Gray<float>[,] image, Rectangle area) { RawMoments raw = new RawMoments(Order); raw.Compute(image, area); CentralMoments center = new CentralMoments(raw); this.Compute(center); }