我在Visual Studio 2019中使用的是emgucv 3.3.0.2824.在这个版本的emgucv中,我找不到optical flow alg.我知道在emgucv 2.4.9.1847版本中,有opticalflow.hs func。但在我的版本中,我无法成功使用opticalflow算法。在我的代码中,我把视频分成几帧,然后把每一帧转换成灰度。现在我想把这些帧用于光流算法。这样我的输出将是一个灰度的视频,而我移动的对象将被标记为红色或绿色的方块。
提前感谢任何能帮助我的人。
这是代码(在private async void ReadAllFrames() func中)。
private async void ReadAllFrames()
{
Mat m = new Mat();
//read all the frames
while (isReadingFrames == true && FrameNo < TotalFrame)
{
FrameNo += Convert.ToInt16(numericUpDown1.Value);
capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.PosFrames, FrameNo);
capture.Read(m);//read the frame that we choose in the upper line
pictureBox2.Image = m.Bitmap;
Bitmap img = null;
if (FrameNo < TotalFrame)
{
img = new Bitmap(pictureBox2.Image);
}
if (FrameNo < TotalFrame)
{
imgInput = new Image<Bgr, byte>(m.Bitmap);
Image<Gray, byte> imgOutput =
new Image<Gray, byte>(m.Bitmap.Width, m.Bitmap.Height, new Gray(0));
imgOutput = imgInput.Convert<Gray, byte>();
pictureBox3.Image = imgOutput.Bitmap;
await Task.Delay(1000 / Convert.ToInt16(Fps));
label1.Text = FrameNo.ToString() + "/" + TotalFrame.ToString();
}
}
}
我用的是这个。
protected TrackingPoint[] TrackFrame(Mat lastImage,Mat currentImage, PointF[] pointToLookFor,int index)
{
PointF[] nextPts;
byte[] status;
float[] errors;
CvInvoke.CalcOpticalFlowPyrLK(lastImage, currentImage, pointToLookFor, new Size(30, 30), 1, new MCvTermCriteria(20, 0.03), out nextPts, out status, out errors);
var returnList = new List<TrackingPoint>();
for(int i=0;i<status.Length;i++)
{
returnList.Add(new TrackingPoint()
{
index=index,
status = status[i] == 1 ? true : false,
toPoint = nextPts[i],
fromPoint = pointToLookFor[i]
}
);
}
return returnList.ToArray();
}
}
}