无需测量的Kalman-OpenCV估计
嘿:)我现在开始在OpenCv中使用Kalmanfilter。 我遇到了两个问题 第一个是,我使用的例子。 在视频中,Kalman过滤器似乎减少了噪音,但如果我运行代码,噪音不会减少,因此如果我移动鼠标,预测行为几乎与鼠标移动完全相同,但没有减少噪音。是由于我的高采样率,还是measurementMatrix、processNoiseCov、measurementNoiseCov和errorCovPost的值设置错误?我希望你能理解我的意思 我的另一个问题是,如果我按下“n”,我想禁用新的测量,我想Kalmanfilter仍然猜测鼠标的新位置。在米尔恰,波帕说: 但是在kalman.correct()调用之前,将测量矩阵设置为0(这是程序员的职责)。这样做,校正系数(增益)变为0,并且根据预测更新滤波器的状态 所以我试着这样做:measurement=Mat::zeros(2,1,CV_32F),但是预测的位置一直在位置(0,0),所以不是我所期望的。有什么我理解错了吗?测量不是Mircea Popa所说的“测量矩阵”吗?或者有没有其他方法可以让KalmanFilter预测新位置而无需测量 为了明确我期望Kalmanfilter做什么:如果没有测量,估计位置的移动应该是均匀的,移动方向应该在由最后两个位置确定的直线上 这是我的密码:无需测量的Kalman-OpenCV估计,opencv,filter,noise,measurement,kalman-filter,Opencv,Filter,Noise,Measurement,Kalman Filter,嘿:)我现在开始在OpenCv中使用Kalmanfilter。 我遇到了两个问题 第一个是,我使用的例子。 在视频中,Kalman过滤器似乎减少了噪音,但如果我运行代码,噪音不会减少,因此如果我移动鼠标,预测行为几乎与鼠标移动完全相同,但没有减少噪音。是由于我的高采样率,还是measurementMatrix、processNoiseCov、measurementNoiseCov和errorCovPost的值设置错误?我希望你能理解我的意思 我的另一个问题是,如果我按下“n”,我想禁用新的测量,
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/tracking.hpp"
using namespace cv;
using namespace std;
int m_X = 0;
int m_Y = 0;
bool cursorSet = false;
bool noDetection = false;
Mat_<float> lastPosition(2,1);
void drawCross(Mat &img, Point ¢er, Scalar color, int d) {
line( img, Point( center.x - d, center.y - d ), Point( center.x + d, center.y + d ), color, 2, CV_AA, 0);
line( img, Point( center.x + d, center.y - d ), Point( center.x - d, center.y + d ), color, 2, CV_AA, 0);
};
void CallBackFunc(int event, int x, int y, int flags, void* userdata) {
m_X = x;
m_Y = y;
cursorSet = true;
cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
};
void GetCursorPos(Point &mousepos){
mousepos = Point(m_X, m_Y);
};
int main( ) {
//create window and set callback for mouse movement
namedWindow("window", 1);
setMouseCallback("window", CallBackFunc, NULL);
// Image to show mouse tracking
Mat img(600, 800, CV_8UC3);
vector<Point> mousev,kalmanv;
mousev.clear();
kalmanv.clear();
//wait until mouse has an initial position inside the window
while(!cursorSet) {
imshow("window", img);
waitKey(10);
};
//create kalman Filter
KalmanFilter KF(6, 2, 0);
//position of the mouse will be observed
Point mousePos;
GetCursorPos(mousePos);
// intialization of KF...
KF.transitionMatrix = *(Mat_<float>(6, 6) << 1,0,1,0,.5,0, // x + v_x + 1/2 a_x
0,1,0,1,0,0.5, // y + v_Y + 1/2 a_y
0,0,1,0,1,0, // v_x + a_x
0,0,0,1,0,1, // v_y + a_y
0,0,0,0,1,0, // a_x
0,0,0,0,0,1); // a_y
Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
//initialize the pre state
KF.statePost.at<float>(0) = mousePos.x;
KF.statePost.at<float>(1) = mousePos.y;
KF.statePost.at<float>(2) = 0;
KF.statePost.at<float>(3) = 0;
KF.statePost.at<float>(4) = 0;
KF.statePost.at<float>(5) = 0;
setIdentity(KF.measurementMatrix);
setIdentity(KF.processNoiseCov, Scalar::all(1e-1));
setIdentity(KF.measurementNoiseCov, Scalar::all(1e-5));
setIdentity(KF.errorCovPost, Scalar::all(1e-3));
while(1) {
img = Scalar::all(0);
// First predict, to update the internal statePre variable
Mat prediction = KF.predict();
Point predictPt(prediction.at<float>(0),prediction.at<float>(1));
// Get mouse point
if (!noDetection) {
GetCursorPos(mousePos);
measurement(0) = mousePos.x;
measurement(1) = mousePos.y;
}else {
measurement(0) = prediction.at<float>(0);
measurement(1) = prediction.at<float>(1);
}
// The update phase3
Mat estimated = KF.correct(measurement);
Point statePt(estimated.at<float>(0),estimated.at<float>(1));
Point measPt(measurement(0),measurement(1));
// draw cross for actual mouse position and kalman guess
mousev.push_back(measPt);
kalmanv.push_back(statePt);
drawCross(img, statePt, Scalar(255,255,255), 5);
drawCross(img, measPt, Scalar(0,0,255), 5 );
// draw lines of movement
for (int i = 0; i < mousev.size()-1; i++)
line(img, mousev[i], mousev[i+1], Scalar(0,0,255), 1);
for (int i = 0; i < kalmanv.size()-1; i++)
line(img, kalmanv[i], kalmanv[i+1], Scalar(255,255,255), 1);
imshow("window", img);
char key = waitKey(10);
if (key == 'c') {
mousev.clear();
kalmanv.clear(); // press c to clear screen
}if (key == 'n') {
noDetection = true; //press n to simulate that no measurement is made
}if (key == 'd') {
noDetection = false;//press d to allow measurements again
}else if(key == 'x') {
break; // press x to exit program
};
}
return 0;
}
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/video/tracking.hpp”
使用名称空间cv;
使用名称空间std;
int m_X=0;
int m_Y=0;
bool-cursorSet=false;
bool noDetection=false;
最后位置(2,1);
虚线十字(垫和img、点和中心、标量颜色、int d){
线(img,点(center.x-d,center.y-d),点(center.x+d,center.y+d),颜色,2,CV_AA,0);
线(img,点(center.x+d,center.y-d),点(center.x-d,center.y+d),颜色,2,CV_AA,0);
};
void CallBackFunc(int事件、int x、int y、int标志、void*userdata){
m_X=X;
m_Y=Y;
cursorSet=true;
cout从更广义的角度讲……如果你希望有一个卡尔曼滤波器来软化测量噪声,你必须更多地依赖过程模型,更少地依赖测量更新。因此,在你的例子中,调整“measurementMatrix,processNoiseCov,measurementNoiseCov”可能会使您的输出更柔和。谢谢您快速回答梅斯堡。我理解正确了吗:这些矩阵在初始化时设置了一次?还是每次都需要更改?在初始化时设置一次…是的。