private void button11_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { Image <Gray, Byte> last = null; Image <Gray, float> flowX = null; Image <Gray, float> flowY = null; Image <Gray, byte> draw = null; MKeyPoint[] keys; Accord.Video.FFMPEG.VideoFileWriter wr = new Accord.Video.FFMPEG.VideoFileWriter(); VideoProcessig videoProcessig = new VideoProcessig(openFileDialog1.FileName, (map) => { Image <Gray, Byte> image = new Image <Gray, byte>(map); if (last == null) { last = image; flowX = new Image <Gray, float>(map.Size); flowY = new Image <Gray, float>(map.Size); draw = new Image <Gray, byte>(map.Size); wr.Open("result.avi", map.Width, map.Height); return(map); } else { CvInvoke.CalcOpticalFlowFarneback(last, image, flowX, flowY, 0.5, 3, 10, 3, 5, 1.5, Emgu.CV.CvEnum.OpticalflowFarnebackFlag.Default); last = image; } CvInvoke.AccumulateSquare(flowX, flowY); CvInvoke.Canny(flowY.Convert <byte>(FloatToByte), draw, 40, 50); var result = draw.Bitmap; wr.WriteVideoFrame(result); return(result); }); videoProcessig.Show(); } byte FloatToByte(float val) { return((byte)val); } }
/// <summary> /// 对速度限幅 /// 奈何ref参数不能放在lambda表达式中。。 /// </summary> private void ApplySaturationVel() { Matrix <double> square = new Matrix <double>(rtVel.Rows, 3); //平方 Matrix <double> norm = new Matrix <double>(rtVel.Rows, 1); //平方和 Matrix <double> res = new Matrix <double>(rtVel.Rows, 1); //平方和的开方 CvInvoke.AccumulateSquare(rtVel, square); CvInvoke.Add(square.GetCol(0), square.GetCol(1), norm); CvInvoke.Add(square.GetCol(2), norm, norm); CvInvoke.Sqrt(norm, res); Parallel.For(0, rtVel.Rows, (i) => { if (res[i, 0] > MaxVel) { for (int j = 0; j < rtVel.Cols; j++) { rtVel[i, j] = rtVel[i, j] * MaxVel / res[i, 0]; } } }); }
/// <summary> /// 对数组进行限幅 /// 每一行作为一个向量,对其幅值进行限幅 /// </summary> /// <param name="data">矩阵</param> /// <param name="max">最大值</param> private void ApplySaturation(ref Matrix <double> data, double max) { Matrix <double> square = new Matrix <double>(data.Rows, 3); //平方 Matrix <double> norm = new Matrix <double>(data.Rows, 1); //平方和 Matrix <double> res = new Matrix <double>(data.Rows, 1); //平方和的开方 CvInvoke.AccumulateSquare(data, square); CvInvoke.Add(square.GetCol(0), square.GetCol(1), norm); CvInvoke.Add(square.GetCol(2), norm, norm); CvInvoke.Sqrt(norm, res); for (int i = 0; i < data.Rows; i++) { if (res[i, 0] > max) { for (int j = 0; j < data.Cols; j++) { data[i, j] = data[i, j] * max / res[i, 0]; } } } }