首页 新闻 搜索 专区 学院

使用OpencvSharp仿c++的鼠标追踪卡尔曼滤波算法

0
悬赏园豆:100 [已关闭问题] 关闭于 2021-05-11 09:28
模仿C++的opencv 卡尔曼鼠标追踪代码,用C#实现了一遍,得出的结果有问题。如截图,总共四位数,前两位是鼠标原始xy坐标点,后面两位整数是卡尔曼得出的结果,相关太过悬殊,正常情况下卡尔螺滤波的结果数据会平缓很多,现在得到的,都是超出屏幕的极限值。根本不能使用,请大佬帮看看。
 
复制代码
        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);
            }
        }
复制代码

 

风徐徐的主页 风徐徐 | 初学一级 | 园豆:36
提问于:2021-05-10 16:49
< >
分享
所有回答(1)
0

哈哈,已解决。

风徐徐 | 园豆:36 (初学一级) | 2021-05-11 09:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册