Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the simple implementation of Kalman filter

Shulou Source: shulou.com Published: 2022-06-01 11:38:28 10月03日 Update

This article will explain in detail how the simple implementation of the Kalman filter is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In the field of target tracking, Kalman filter is a very common method.

Taking tracking the position and velocity of an object in a two-dimensional plane as an example, this paper illustrates how to implement a simple Kalman tracker.

It is implemented using the KalmanFilter class in OpenCV.

1. Parameter initialization kalman = cv2.KalmanFilter (4primer 2)

Indicates that the transfer matrix dimension of the Kalman filter is 4 and the measurement matrix dimension is 2.

Because the state variables include four (displacements and velocities in x and y directions, respectively), two quantities can be observed (displacements in x and y directions, respectively).

Kalman.measurementMatrix = np.array ([[1jc0pj0d0d0re0], [0rect 1d0d0re0]], np.float32)

The measurement matrix and its meaning are:

Kalman.transitionMatrix = np.array ([[1rect 0rem 1je 0], [0rect 1je 0je 1], [0je 0re 0re 1je 0], [0je 0je 0je 0st]], np.float32)

The transfer matrix and its meaning are:

Kalman.processNoiseCov = np.array (np.float32) * 0.003kalman.measurementNoiseCov = np.array ([[1rect 0rect 0je 0], [0rect 1je 0je 0je 0], [0je 0re 0je 1je 0], np.float32) * 0.003kalman.measurementNoiseCov = np.array ([[1jue 0], [0je 1]], np.float32) * 0.5)

Process noise and measurement noise are estimated by an empirical value.

two。 Generate test data

The trigonometric function is used to superimpose random disturbance to generate test data.

Def data_generator (length=100): dxy = [] xy = [] last_xy = [0 0] for i in range (length): x_base = 5-5 * math.cos (2 * I * math.pi / length) y_base = 50-50 * math.cos (2 * I * math.pi / length) x_noise = 1 * (random ()-0.5) y_noise = 20 * (random ()-0.5) dx_base = math.sin (2 * I * Math.pi / length) dy_base = 30 * math.sin (2 * I * math.pi / length) dx_noise = 1 * (random ()-0.5) dy_noise = 5 * (random ()-0.5) cur_xy = [x_base + x_noise + dx_base + dx_noise \ y_base + y_noise + dy_base + dy_noise] cur_dxy = [cur_xy [0]-last_xy [0], cur_xy [1]-last_xy [1]] xy.append (cur_xy) dxy.append (cur_dxy) last_xy = cur_xy return np.array (dxy, dtype=np.float32),\ np.array (xy, dtype=np.float32) 3. Running

The core is the two methods of kalman:

Correct updates current measurements

Predict predicts the value of the next frame.

Length = 100dxy, xy = data_generator2 (length) dxy_pred = [] xy_pred = [] for i in range (length): kalman.correct (XYY [I]) current_prediction = kalman.predict () xy_pred.append (current_prediction [: 2,0]) dxy_pred.append (current_prediction [2xy]) dxy_pred = np.stack (dxy_pred, axis=0) xy_pred = np.stack (xy_pred, axis=0) 4. Visualization

Using Matplotlib to visualize the result

The visualization part of the code is as follows:

Plot_image ((xy, dxy, xy_pred, dxy_pred) def plot_image (inputs): xy, dxy, xy_pred, dxy_pred = inputs fig, axes = plt.subplots (2,2) fig.set_size_inches (18,9) axes [0,0] .plot (xy [:, 0], color='red', label='Measured') axes [0,0] .plot (xy_pred [:, 0], color='blue' Label='Predicted') axes [0,1] .plot (xy [:, 1], color='red', label='Measured') axes [0,1] .plot (xy_pred [:, 1], color='blue', label='Predicted') axes [1,0] .plot (dxy [:, 0], color='red', label='Measured') axes [1,0] .plot (dxy_pred [:, 0], color='blue', label='Predicted') axes [1 1] .plot (dxy [:, 1], color='red', label='Measured') axes [1,1] .plot (dxy_pred [:, 1], color='blue', label='Predicted') axes [0,0] .Set _ title ('Distance-Xerox title title') axes [0,1] .plot [1,0] .set _ title ('Speed-X' Loc='center',fontstyle='normal') axes [1,1] .Set _ title ('Speed-Yanzhidagi localizedcenterprising paramedics Fontstylewritten filters') axes [0,0] .circle () axes [0,1] .benchmark () axes [1,0] .plt.show () plt.show () return's simple implementation of the Kalman filter is shared here. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Filter matrix measurement visualization tracking content meaning noise data article direction method more knowledge article dimension speed test generation good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info vpn MariaDB Linux