-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_KLT_preflow.m
114 lines (101 loc) · 3.11 KB
/
func_KLT_preflow.m
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function [ uvklt,varargout ] = func_KLT_preflow( img1c,img2c,pre_flow,varargin )
% Compute flow optic by KLT methods through pyramidal images
% Input: img1,img2 RGB 8 bit, color
% Output: uvklt
addpath(genpath('.\'))
%% Default parameters
b_color = 0;
b_pyramid = 0;
nb_wrap=10;
median_filter_size=[5,5];
if (nargin > 4)
b_color=varargin{1};
b_pyramid = varargin{2};
end
if b_color
img1 = double(img1c);
img2 = double(img2c);
else
img1 = double(rgb2gray(img1c));
img2 = double(rgb2gray(img2c));
end
%[aa,img1] = structure_texture_decomposition_rof(img1);
%[ab,img2] = structure_texture_decomposition_rof(img2);
sz = [ size(img1,1),size(img1,2)];
h_size = 10;
w_size=(h_size*ceil(sz(2)/sz(1)))+1-mod(ceil(sz(2)/sz(1)),2);
%% Create pyramidal images
if(b_pyramid)
pyramid_spacing=(1/0.5); % 0.7 ?
%auto-detect pyramid levels
pyramid_levels = 1 + floor( log( min(sz(1), sz(2))/16) / log(pyramid_spacing) );
factor = sqrt(2); % sqrt(3)
smooth_sigma = sqrt(pyramid_spacing)/factor; % or sqrt(3) recommended by Manuel Werlberger
f = fspecial('gaussian', 2*round(1.5*smooth_sigma) +1, smooth_sigma);
%Create pyramidal images
pyramid_images1 = compute_image_pyramid(img1, f, pyramid_levels, 1/pyramid_spacing);
pyramid_images2 = compute_image_pyramid(img2, f, pyramid_levels, 1/pyramid_spacing);
else
pyramid_levels =1;
pyramid_images1{1}=img1;
pyramid_images2{1}=img2;
end
%% Preparation before computing
%H_min = size(pyramid_images1{pyramid_levels},1);
%W_min = size(pyramid_images1{pyramid_levels},2);
uvklt=pre_flow;
%% Start KLT
for lvl=pyramid_levels:-1:1
tic
%fprintf('Pyr lvl: %d \n',lvl)
pyr_image1 = pyramid_images1{lvl};
pyr_image2 = pyramid_images2{lvl};
H = size(pyr_image2, 1);
W = size(pyr_image2, 2);
% [x,y] = meshgrid(1:W,1:H);
% figure(1);
% subplot(2,2,1)
% imshow(pyr_image1/255)
% subplot(2,2,2)
% imshow(pyr_image2/255)
% subplot(2,2,3)
% imshow(pyr_image1c/255)
% subplot(2,2,4)
% imshow(pyr_image2c/255)
% figure(2)
% imshow(tup);
uvklt = resample_flow(uvklt,[H W]);
% DO SOMETHING ? PROPAGATE FLOW ?
clear warpImc warpIm;
clear ux uxB uxG uxR eigx;
clear vx vxB vxG vxR;
%taux= makemtrx_prop(H,W,pyr_image2c);
maxh = h_size;
maxw = w_size;
%maxw = maxh;
for k=1:nb_wrap
%% Warping Image
[Ix,Iy,It,~]=partial_derivation(pyr_image1,pyr_image2,uvklt,1);
%% KLT Flow
%fprintf('KLT ... ')
if (b_color)
[ures,vres,~,~]=LKT_res_color(Ix,Iy,It,maxh,maxw);
else
[ures,vres,~,~]=LKT_res(Ix,Iy,It,maxh,maxw);
end
%Limite ures,vres:
ures(ures > 1 ) = 1;
ures(ures < -1 ) = -1;
vres(vres > 1 ) = 1;
vres(vres < -1 ) = -1;
%Add residual value to flow
uvklt(:,:,1)=uvklt(:,:,1)+ures;
uvklt(:,:,2)=uvklt(:,:,2)+vres;
%Smoothing optical flow
for l=1:1
uvklt(:,:,1) = medfilt2(uvklt(:,:,1), median_filter_size, 'symmetric');
uvklt(:,:,2) = medfilt2(uvklt(:,:,2), median_filter_size, 'symmetric');
end
end
end
end