#include <stdio.h>
static inline Point calcPoint(
Point2f center,
double R,
double angle)
{
return center +
Point2f((
float)
cos(angle), (
float)-
sin(angle))*(float)R;
}
static void help()
{
printf( "\nExample of c calls to OpenCV's Kalman filter.\n"
" Tracking of rotating point.\n"
" Point moves in a circle and is characterized by a 1D state.\n"
" state_k+1 = state_k + speed + process_noise N(0, 1e-5)\n"
" The speed is constant.\n"
" Both state and measurements vectors are 1D (a point angle),\n"
" Measurement is the real state + gaussian noise N(0, 1e-1).\n"
" The real and the measured points are connected with red line segment,\n"
" the real and the estimated points are connected with yellow line segment,\n"
" the real and the corrected estimated points are connected with green line segment.\n"
" (if Kalman filter works correctly,\n"
" the yellow segment should be shorter than the red one and\n"
" the green segment should be shorter than the yellow one)."
"\n"
" Pressing any key (except ESC) will reset the tracking.\n"
" Pressing ESC will stop the program.\n"
);
}
{
help();
Mat processNoise(2, 1,
CV_32F);
Mat measurement = Mat::zeros(1, 1,
CV_32F);
char code = (char)-1;
for(;;)
{
img = Scalar::all(0);
state.at<float>(0) = 0.0f;
state.at<
float>(1) = 2.f * (
float)
CV_PI / 6;
KF.transitionMatrix = (Mat_<float>(2, 2) << 1, 1, 0, 1);
setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
randn(KF.statePost, Scalar::all(0), Scalar::all(0.1));
for(;;)
{
Point2f center(img.cols*0.5f, img.rows*0.5f);
float R = img.cols/3.f;
double stateAngle = state.at<float>(0);
Point statePt = calcPoint(center, R, stateAngle);
Mat prediction = KF.predict();
double predictAngle = prediction.at<float>(0);
Point predictPt = calcPoint(center, R, predictAngle);
randn( measurement, Scalar::all(0), Scalar::all(KF.measurementNoiseCov.at<
float>(0)));
measurement += KF.measurementMatrix*state;
double measAngle = measurement.at<float>(0);
Point measPt = calcPoint(center, R, measAngle);
KF.correct(measurement);
double improvedAngle = KF.statePost.at<float>(0);
Point improvedPt = calcPoint(center, R, improvedAngle);
img = img * 0.2;
Mat test = Mat(KF.transitionMatrix*KF.statePost);
drawMarker(img, calcPoint(center, R, Mat(KF.transitionMatrix*KF.statePost).at<
float>(0)),
randn( processNoise,
Scalar(0), Scalar::all(
sqrt(KF.processNoiseCov.at<
float>(0, 0))));
state = KF.transitionMatrix*state + processNoise;
if( code > 0 )
break;
}
if( code == 27 || code == 'q' || code == 'Q' )
break;
}
return 0;
}
void sqrt(InputArray src, OutputArray dst)
Calculates a square root of array elements.
void setIdentity(InputOutputArray mtx, const Scalar &s=Scalar(1))
Initializes a scaled identity matrix.
void randn(InputOutputArray dst, InputArray mean, InputArray stddev)
Fills the array with normally distributed random numbers.
Point2i Point
Definition: modules/core/include/opencv2/core/types.hpp:209
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
Point_< float > Point2f
Definition: modules/core/include/opencv2/core/types.hpp:207
#define CV_32F
Definition: core/include/opencv2/core/hal/interface.h:78
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
Quat< T > cos(const Quat< T > &q)
Quat< T > sin(const Quat< T > &q)
#define CV_PI
Definition: cvdef.h:380
GMat KalmanFilter(const GMat &measurement, const GOpaque< bool > &haveMeasurement, const GMat &control, const cv::gapi::KalmanParams &kfParams)
Standard Kalman filter algorithm http://en.wikipedia.org/wiki/Kalman_filter.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void drawMarker(InputOutputArray img, Point position, const Scalar &color, int markerType=MARKER_CROSS, int markerSize=20, int thickness=1, int line_type=8)
Draws a marker on a predefined position in an image.
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
@ MARKER_SQUARE
A square marker shape.
Definition: imgproc/include/opencv2/imgproc.hpp:921
@ MARKER_STAR
A star marker shape, combination of cross and tilted cross.
Definition: imgproc/include/opencv2/imgproc.hpp:919
@ LINE_AA
antialiased line
Definition: imgproc/include/opencv2/imgproc.hpp:894
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
Definition: core/include/opencv2/core.hpp:107