2016-03-30 01:02:35 +08:00
|
|
|
#!/usr/bin/env python
|
2014-09-18 07:45:48 +08:00
|
|
|
"""
|
|
|
|
Tracking of rotating point.
|
|
|
|
Rotation speed is constant.
|
|
|
|
Both state and measurements vectors are 1D (a point angle),
|
|
|
|
Measurement is the real point angle + gaussian noise.
|
|
|
|
The real and the estimated points are connected with yellow line segment,
|
|
|
|
the real and the measured points are connected with red line segment.
|
|
|
|
(if Kalman filter works correctly,
|
|
|
|
the yellow segment should be shorter than the red one).
|
|
|
|
Pressing any key (except ESC) will reset the tracking with a different speed.
|
|
|
|
Pressing ESC will stop the program.
|
|
|
|
"""
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
long = int
|
|
|
|
|
2014-09-18 07:45:48 +08:00
|
|
|
import cv2
|
2016-03-29 23:27:35 +08:00
|
|
|
from math import cos, sin, sqrt
|
2014-09-18 07:45:48 +08:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
img_height = 500
|
|
|
|
img_width = 500
|
|
|
|
kalman = cv2.KalmanFilter(2, 1, 0)
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
code = long(-1)
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
cv2.namedWindow("Kalman")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
state = 0.1 * np.random.randn(2, 1)
|
|
|
|
|
2014-09-18 22:39:35 +08:00
|
|
|
kalman.transitionMatrix = np.array([[1., 1.], [0., 1.]])
|
|
|
|
kalman.measurementMatrix = 1. * np.ones((1, 2))
|
|
|
|
kalman.processNoiseCov = 1e-5 * np.eye(2)
|
|
|
|
kalman.measurementNoiseCov = 1e-1 * np.ones((1, 1))
|
|
|
|
kalman.errorCovPost = 1. * np.ones((2, 2))
|
|
|
|
kalman.statePost = 0.1 * np.random.randn(2, 1)
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
def calc_point(angle):
|
|
|
|
return (np.around(img_width/2 + img_width/3*cos(angle), 0).astype(int),
|
2014-09-18 22:39:35 +08:00
|
|
|
np.around(img_height/2 - img_width/3*sin(angle), 1).astype(int))
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
state_angle = state[0, 0]
|
|
|
|
state_pt = calc_point(state_angle)
|
|
|
|
|
|
|
|
prediction = kalman.predict()
|
|
|
|
predict_angle = prediction[0, 0]
|
|
|
|
predict_pt = calc_point(predict_angle)
|
|
|
|
|
2014-09-18 22:39:35 +08:00
|
|
|
measurement = kalman.measurementNoiseCov * np.random.randn(1, 1)
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
# generate measurement
|
2014-09-18 22:39:35 +08:00
|
|
|
measurement = np.dot(kalman.measurementMatrix, state) + measurement
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
measurement_angle = measurement[0, 0]
|
|
|
|
measurement_pt = calc_point(measurement_angle)
|
|
|
|
|
|
|
|
# plot points
|
|
|
|
def draw_cross(center, color, d):
|
2014-09-18 22:39:35 +08:00
|
|
|
cv2.line(img,
|
|
|
|
(center[0] - d, center[1] - d), (center[0] + d, center[1] + d),
|
|
|
|
color, 1, cv2.LINE_AA, 0)
|
|
|
|
cv2.line(img,
|
|
|
|
(center[0] + d, center[1] - d), (center[0] - d, center[1] + d),
|
|
|
|
color, 1, cv2.LINE_AA, 0)
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
img = np.zeros((img_height, img_width, 3), np.uint8)
|
|
|
|
draw_cross(np.int32(state_pt), (255, 255, 255), 3)
|
|
|
|
draw_cross(np.int32(measurement_pt), (0, 0, 255), 3)
|
|
|
|
draw_cross(np.int32(predict_pt), (0, 255, 0), 3)
|
|
|
|
|
|
|
|
cv2.line(img, state_pt, measurement_pt, (0, 0, 255), 3, cv2.LINE_AA, 0)
|
|
|
|
cv2.line(img, state_pt, predict_pt, (0, 255, 255), 3, cv2.LINE_AA, 0)
|
|
|
|
|
|
|
|
kalman.correct(measurement)
|
|
|
|
|
2016-03-29 23:27:35 +08:00
|
|
|
process_noise = sqrt(kalman.processNoiseCov[0,0]) * np.random.randn(2, 1)
|
2014-09-18 22:39:35 +08:00
|
|
|
state = np.dot(kalman.transitionMatrix, state) + process_noise
|
2014-09-18 07:45:48 +08:00
|
|
|
|
|
|
|
cv2.imshow("Kalman", img)
|
|
|
|
|
2016-03-29 23:27:35 +08:00
|
|
|
code = cv2.waitKey(100)
|
2014-09-18 07:45:48 +08:00
|
|
|
if code != -1:
|
|
|
|
break
|
|
|
|
|
2016-03-30 01:02:35 +08:00
|
|
|
if (code % 0x100) in [27, ord('q'), ord('Q')]:
|
2014-09-18 07:45:48 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
cv2.destroyWindow("Kalman")
|