ImageprevFrame = new Image ("prev.png"); Image curFrame = new Image ("cur.png"); Mat prevMat = prevFrame.Mat; Mat curMat = curFrame.Mat; Mat curFeatures = new Mat(); Mat status = new Mat(); Mat error = new Mat(); VectorOfPointF prevPoints = new VectorOfPointF(); prevPoints.Push(new PointF[] { new PointF(100, 100), new PointF(200, 200) }); VectorOfPointF curPoints = new VectorOfPointF(); CvInvoke.CalcOpticalFlowPyrLK(prevMat, curMat, prevPoints, curFeatures, status, error); CvInvoke.DrawMatches(prevMat, prevPoints, curMat, curPoints, status, curMat, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255));
VideoCapture capture = new VideoCapture(@"test.avi"); if (!capture.IsOpened) return; ImageIn this example, we use CalcOpticalFlowPyrLK to continuously track feature points in a video stream. We first open a video file using Emgu CV's VideoCapture object, then continuously read frames from the video file. For each frame, we compute the optical flow of the feature points from the previous frame to the current frame using CalcOpticalFlowPyrLK, draw the points on the current frame using DrawMatches, and display the current frame using Imshow. We then update the previous frame and feature points to be used in the next iteration of the loop. The package library for these examples is Emgu CV, which is a wrapper for the OpenCV computer vision library.prevFrame = new Image (); Image curFrame = new Image (); Mat prevMat = new Mat(); Mat curMat = new Mat(); VectorOfPointF prevPoints = new VectorOfPointF(); prevPoints.Push(new PointF[] { new PointF(100, 100), new PointF(200, 200) }); while (true) { capture.Read(prevFrame); if (prevFrame == null) break; prevMat = prevFrame.Mat; curFrame = prevFrame.Clone(); curMat = curFrame.Mat; Mat curFeatures = new Mat(); Mat status = new Mat(); Mat error = new Mat(); VectorOfPointF curPoints = new VectorOfPointF(); CvInvoke.CalcOpticalFlowPyrLK(prevMat, curMat, prevPoints, curFeatures, status, error); CvInvoke.DrawMatches(prevMat, prevPoints, curMat, curPoints, status, curMat, new MCvScalar(255, 0, 0), new MCvScalar(0, 0, 255)); CvInvoke.Imshow("Optical Flow", curMat); if (CvInvoke.WaitKey(10) >= 0) break; prevMat = curMat.Clone(); prevPoints = curPoints; }