/// <summary> /// Create the standard vector of VectorOfPointF /// </summary> public VectorOfVectorOfPointF(PointF[][] values) : this() { using (VectorOfPointF v = new VectorOfPointF()) { for (int i = 0; i < values.Length; i++) { v.Push(values[i]); Push(v); v.Clear(); } } }
/// <summary> /// Calculates optical flow for a sparse feature set using iterative Lucas-Kanade method in pyramids /// </summary> /// <param name="prev">First frame, at time t</param> /// <param name="curr">Second frame, at time t + dt </param> /// <param name="prevFeatures">Array of points for which the flow needs to be found</param> /// <param name="winSize">Size of the search window of each pyramid level</param> /// <param name="level">Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels are used, etc</param> /// <param name="criteria">Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped</param> /// <param name="flags">Flags</param> /// <param name="currFeatures">Array of 2D points containing calculated new positions of input features in the second image</param> /// <param name="status">Array. Every element of the array is set to 1 if the flow for the corresponding feature has been found, 0 otherwise</param> /// <param name="trackError">Array of double numbers containing difference between patches around the original and moved points</param> /// <param name="minEigThreshold">the algorithm calculates the minimum eigen value of a 2x2 normal matrix of optical flow equations (this matrix is called a spatial gradient matrix in [Bouguet00]), divided by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding feature is filtered out and its flow is not processed, so it allows to remove bad points and get a performance boost.</param> public static void CalcOpticalFlowPyrLK( IInputArray prev, IInputArray curr, PointF[] prevFeatures, Size winSize, int level, MCvTermCriteria criteria, out PointF[] currFeatures, out Byte[] status, out float[] trackError, Emgu.CV.CvEnum.LKFlowFlag flags = CvEnum.LKFlowFlag.Default, double minEigThreshold = 1.0e-4) { using (Util.VectorOfPointF prevPts = new Util.VectorOfPointF()) using (Util.VectorOfPointF nextPts = new Util.VectorOfPointF()) using (Util.VectorOfByte statusVec = new Util.VectorOfByte()) using (Util.VectorOfFloat errorVec = new Util.VectorOfFloat()) { prevPts.Push(prevFeatures); CalcOpticalFlowPyrLK( prev, curr, prevPts, nextPts, statusVec, errorVec, winSize, level, criteria, flags, minEigThreshold); status = statusVec.ToArray(); trackError = errorVec.ToArray(); currFeatures = nextPts.ToArray(); } }
/* #region Kalman Filter /// <summary> /// Allocates CvKalman and all its matrices and initializes them somehow. /// </summary> /// <param name="dynamParams">dimensionality of the state vector</param> /// <param name="measureParams">dimensionality of the measurement vector </param> /// <param name="controlParams">dimensionality of the control vector </param> /// <returns>Pointer to the created Kalman filter</returns> [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)] public static extern IntPtr cvCreateKalman(int dynamParams, int measureParams, int controlParams); /// <summary> /// Adjusts stochastic model state on the basis of the given measurement of the model state. /// The function stores adjusted state at kalman->state_post and returns it on output /// </summary> /// <param name="kalman">Pointer to the structure to be updated</param> /// <param name="measurement">Pointer to the structure CvMat containing the measurement vector</param> /// <returns>The function stores adjusted state at kalman->state_post and returns it on output</returns> [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)] public static extern IntPtr cvKalmanCorrect(ref MCvKalman kalman, IntPtr measurement); /// <summary> /// Estimates the subsequent stochastic model state by its current state and stores it at kalman->state_pre /// The function returns the estimated state /// </summary> /// <param name="kalman">Kalman filter state</param> /// <param name="control">Control vector (uk), should be NULL iff there is no external control (controlParams=0). </param> /// <returns>the estimated state</returns> [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)] public static extern IntPtr cvKalmanPredict(ref MCvKalman kalman, IntPtr control); /// <summary> /// Releases the structure CvKalman and all underlying matrices /// </summary> /// <param name="kalman">reference of the pointer to the Kalman filter structure.</param> [DllImport(OpencvVideoLibrary, CallingConvention = CvInvoke.CvCallingConvention)] public static extern void cvReleaseKalman(ref IntPtr kalman); #endregion */ #region optical flow /// <summary> /// Calculates optical flow for a sparse feature set using iterative Lucas-Kanade method in pyramids /// </summary> /// <param name="prev">First frame, at time t</param> /// <param name="curr">Second frame, at time t + dt </param> /// <param name="prevFeatures">Array of points for which the flow needs to be found</param> /// <param name="winSize">Size of the search window of each pyramid level</param> /// <param name="level">Maximal pyramid level number. If 0 , pyramids are not used (single level), if 1 , two levels are used, etc</param> /// <param name="criteria">Specifies when the iteration process of finding the flow for each point on each pyramid level should be stopped</param> /// <param name="flags">Flags</param> /// <param name="currFeatures">Array of 2D points containing calculated new positions of input features in the second image</param> /// <param name="status">Array. Every element of the array is set to 1 if the flow for the corresponding feature has been found, 0 otherwise</param> /// <param name="trackError">Array of double numbers containing difference between patches around the original and moved points</param> /// <param name="minEigThreshold">the algorithm calculates the minimum eigen value of a 2x2 normal matrix of optical flow equations (this matrix is called a spatial gradient matrix in [Bouguet00]), divided by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding feature is filtered out and its flow is not processed, so it allows to remove bad points and get a performance boost.</param> public static void CalcOpticalFlowPyrLK( IInputArray prev, IInputArray curr, PointF[] prevFeatures, Size winSize, int level, MCvTermCriteria criteria, out PointF[] currFeatures, out Byte[] status, out float[] trackError, Emgu.CV.CvEnum.LKFlowFlag flags = CvEnum.LKFlowFlag.Default, double minEigThreshold = 1.0e-4) { using (Util.VectorOfPointF prevPts = new Util.VectorOfPointF()) using (Util.VectorOfPointF nextPts = new Util.VectorOfPointF()) using (Util.VectorOfByte statusVec = new Util.VectorOfByte()) using (Util.VectorOfFloat errorVec = new Util.VectorOfFloat()) { prevPts.Push(prevFeatures); CalcOpticalFlowPyrLK( prev, curr, prevPts, nextPts, statusVec, errorVec, winSize, level, criteria, flags, minEigThreshold); status = statusVec.ToArray(); trackError = errorVec.ToArray(); currFeatures = nextPts.ToArray(); } }