private int _width { get; set; } private int _height { get; set; } private Mat _measurement { get; set; } private KalmanFilter _kf; public TransformBlock<(float x,float y),(ushort w,ushort h)> CoordinateTransformation=null; private static readonly Lazy<KalmanHelp> lazy = new Lazy<KalmanHelp>(() => { int stateNum = 4;//初始状态向量配置参数{x,y,vx,vy} int measureNum = 2;//观测向量配置参数{x,y} var instance = new KalmanHelp(); instance._width = 1920; //int.Parse(Environment.GetEnvironmentVariable("Screen.Width")??"0"); instance._height = 1080;// int.Parse(Environment.GetEnvironmentVariable("Screen.Height")??"0"); instance._kf = new KalmanFilter(stateNum,measureNum,0); instance._kf.TransitionMatrix = Mat.FromArray<float>(new float[4, 4] {{ 1, 0, 1, 0 },{0,1,0,1 },{0,0,1,0 },{0,0,0,1 } }); Cv2.SetIdentity(instance._kf.MeasurementMatrix);//测量矩阵H Cv2.SetIdentity(instance._kf.ProcessNoiseCov,Scalar.All(10));//系统噪声方差矩阵Q Cv2.SetIdentity(instance._kf.MeasurementNoiseCov,Scalar.All(35));//测量噪声方差距阵R Cv2.SetIdentity(instance._kf.ErrorCovPost,Scalar.All(10));//后验错误方差距阵P RNG r = Cv2.GetTheRNG(); r.Fill(instance._kf.StatePost,DistributionType.Uniform,0,0);//初始状态值 //r.Fill(instance._kf.StatePost,DistributionType.Uniform,0,instance._height>instance._width?instance._width:instance._height);//初始状态值 instance._measurement = Mat.Zeros(measureNum,1,type:MatType.CV_32F);//测量初始值 instance.CoordinateTransformation = new TransformBlock<(float x, float y), (ushort w, ushort h)>(instance.Work,new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism=1, BoundedCapacity=50, }); return instance; }); /// <summary> /// 座标转换 /// </summary> public static KalmanHelp Instance { get { return lazy.Value; } } private KalmanHelp() { } ~KalmanHelp() { this.Dispose(); } private (ushort w,ushort h) Work((float x,float y)xy) { using(Mat prediction = KalmanHelp.Instance._kf.Predict()) { Instance._measurement.At<float>(0) = xy.x; Instance._measurement.At<float>(1) = xy.y; _kf.Correct(Instance._measurement); var x = prediction.At<ushort>(0); var y = prediction.At<ushort>(1); Console.WriteLine($"{xy.x},{xy.y},{x},{y}"); return (x, y); } }
哈哈,已解决。
请问你问题怎么解决的啊?