/// <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="prevPyrBuffer">Buffer for the pyramid for the first frame. If it is not NULL, the buffer must have a sufficient size to store the pyramid from level 1 to level #level ; the total size of (image_width+8)*image_height/3 bytes is sufficient</param>
/// <param name="currPyrBuffer">Similar to prev_pyr, used for the second frame</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>
public static void PyrLK(
Image <Gray, Byte> prev,
Image <Gray, Byte> curr,
Image <Gray, Byte> prevPyrBuffer,
Image <Gray, Byte> currPyrBuffer,
PointF[] prevFeatures,
Size winSize,
int level,
MCvTermCriteria criteria,
Emgu.CV.CvEnum.LKFLOW_TYPE flags,
out PointF[] currFeatures,
out Byte[] status,
out float[] trackError)
{
Image <Gray, Byte> prevPyrBufferParam = prevPyrBuffer ?? new Image <Gray, byte>(prev.Width + 8, prev.Height / 3);
Image <Gray, Byte> currPyrBufferParam = currPyrBuffer ?? prevPyrBufferParam.CopyBlank();
status = new Byte[prevFeatures.Length];
trackError = new float[prevFeatures.Length];
currFeatures = new PointF[prevFeatures.Length];
CvInvoke.cvCalcOpticalFlowPyrLK(
prev,
curr,
prevPyrBufferParam,
currPyrBufferParam,
prevFeatures,
currFeatures,
prevFeatures.Length,
winSize,
level,
status,
trackError,
criteria,
flags);
#region Release buffer images if they are create within this function call
if (!object.ReferenceEquals(prevPyrBufferParam, prevPyrBuffer))
{
prevPyrBufferParam.Dispose();
}
if (!object.ReferenceEquals(currPyrBufferParam, currPyrBuffer))
{
currPyrBufferParam.Dispose();
}
#endregion
}