嘿那里:)我目前开始在OpenCv中使用Kalmanfilter。 我遇到了2个问题。
首先,我使用了http://www.morethantechnical.com/2011/06/17/simple-kalman-filter-for-tracking-using-opencv-2-2-w-code/中的示例。 在视频中,卡尔曼滤波器似乎降低了噪声,但是如果我运行我的代码,噪声不会降低,所以如果我移动鼠标,预测的行为几乎与鼠标移动完全相同,而不会降低噪音。是因为我的高采样率还是measurementMatrix,processNoiseCov,measurementNoiseCov和errorCovPost的值设置不好?我希望你能理解我的意思。
我的另一个问题是如果我按下" n"我想禁用新的测量,我希望Kalmanfilter仍然猜测鼠标的新位置。在http://code.opencv.org/issues/1380 Mircea Popa说:
"但在kalman.correct()调用之前将测量矩阵设置为0(这是程序员的职责)。这样做,校正因子(增益)变为0,并根据预测的内容更新滤波器的状态。"
所以我试着这样做:measurement = Mat :: zeros(2,1,CV_32F)但是那时预测的位置一直在位置(0,0),所以不是我的预期。有什么我理解错了吗?不测量"测量矩阵" Mircea Popa谈到了什么?还是有另一种方法让KalmanFilter在没有测量的情况下预测新的位置吗?
要明确我期望Kalmanfilter做什么:如果没有测量,估计位置的移动应该是均匀的,并且移动方向应该在由最后两个位置确定的直线上。
这是我的代码:
#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;
}
答案 0 :(得分:3)
从更广义的PoV说起......如果您希望使用能够软化测量噪声的卡尔曼滤波器,您必须更多地依赖过程模型而不是更多的测量更新。所以在你的情况下,调整&#34; measurementMatrix,processNoiseCov,measurementNoiseCov&#34;可能会让你产生更柔和的输出。