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> /// 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> /// 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-1].</param> /// <param name="roi">Initial search area</param> /// <param name="termCriteria">Mean shift termination criteria.</param> /// <returns>Object area.</returns> public static Rectangle Process(Gray<byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria) { CentralMoments centralMoments; return process(probabilityMap, roi, termCriteria, out centralMoments); }
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> /// Meanshift algorithm /// </summary> /// <param name="probabilityMap">Probability map [0-1].</param> /// <param name="roi">Initial search area</param> /// <param name="termCriteria">Mean shift termination criteria.</param> /// <returns>Object area.</returns> public static Rectangle Process(Gray <byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria) { CentralMoments centralMoments; return(process(probabilityMap, roi, termCriteria, out centralMoments)); }