Removing Snow with a Kalman Filter

Preface

Removing Snow

This article uses the characteristics of a Kalman filter to track snowflakes, with the goal of removing snow from a static background image.

For the basic idea behind Kalman filters, you can refer to this article.

I will go straight to the code here. Since matrix operations are much more convenient in MATLAB, I used MATLAB for the experiment. For an engineering implementation, OpenCV and C++ would still be the more practical first choice.

MATLAB Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% demo.m

filename = 'snow1.avi';
[F1,E1] = Kalman(filename);
N = size(F1,2);
WriterObj = VideoWriter([filename(1:end-4) '_kalman_removal.avi']);
open(WriterObj);

for i = 1:N-1
// Show the result
subplot(1,2,1);imshow(F1{i});
subplot(1,2,2);imshow(E1{i});
pause(0.001);
// Concatenate the original frame and the result frame side by side
R = cat(2,F1{i},E1{i});
// Write the frame back to the file
writeVideo(WriterObj,R);
end
close(WriterObj);

Kalman filter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
% Kalman.m

function [Frames,Estimates] = Kalman(filename)

% Read the video file object
ReaderObj = VideoReader(filename);

% Get the number of video frames
N = get(ReaderObj, 'NumberOfFrames');
% Trim the final frames
N = N-10;

% Video data has four dimensions: width, height, RGB, and frame count
VideoFrames = read(ReaderObj);

% Get the frame size
sz = size(VideoFrames(:,:,:,1));

% Extract brightness data
light = zeros(sz(1),sz(2),N);
for i = 1:N
% Convert color space: RGB -> HSI
HSI = rgb2hsv(VideoFrames(:,:,:,i));
% Only the brightness channel is used here; hue and saturation are ignored
light(:,:,i) = HSI(:,:,3);
end

% Process noise
W = randn(sz(1),sz(2),N) * 0.2;
% Standard deviation and covariance of the process noise
R = std(W,0,3);Rww = R.^2;

% Measurement noise
V = randn(sz(1),sz(2),N) * 5;
% Standard deviation and covariance of the measurement noise
Q = std(V,0,3);Rvv = Q.^2;

% System parameter
A = 1;
% Measurement-system parameter
H = 1;

% X represents brightness data with process noise; Y represents the original brightness data
X{1} = light(:,:,1) * 255;
Y{1} = X{1};
for k = 2:N
X{k} = A*X{k-1} + W(:,:,k);
Y{k} = light(:,:,k)*255;
end

% Initial covariance
prior = ones(sz(1),sz(2)) * 2;

% Use the original data directly for the first frame
s{1} = X{1};

for t = 2:N
% Calculate the covariance at time t, including system noise
p = A.^2 * prior + Rww;

% Calculate the Kalman gain
Kg = H*p ./ ((H.^2)*p + Rvv);

% Update the result at time t
s{t} = A*s{t-1} + Kg .* (Y{t} - A*H*s{t-1});

% Update the covariance
prior = p - H*Kg.*p;
end

% Combine hue and saturation, convert the color space, and output the result
for t = 1:N
HSI = rgb2hsi(VideoFrames(:,:,:,i));
s{t} = hsv2rgb(HSI(:,:,1),HSI(:,:,2),im2double(s{t}/255));
Y{t} = hsv2rgb(HSI(:,:,1),HSI(:,:,2),im2double(Y{t}/255));
end
Frames = Y;
Estimates = s;
end

Result

Summary

That is all for now. If I have time later, I will add an OpenCV version as well.

-------------本文结束 感谢您的阅读-------------
If this article helped you, your support encourages me to keep writing.

Title:Removing Snow with a Kalman Filter

Author:lingyun

Published:2018-11-13 22:49

Updated:2026-09-23 00:37

Original link:https://tsuijunxi.github.io/en/2018/11/13/removing-snow-with-a-kalman-filter/

License: Attribution-NonCommercial-NoDerivatives 4.0 International Please keep the original link and author when sharing.