g_stabilize.m 2.99 KB
Newer Older
Daniel Bourgault's avatar
Daniel Bourgault committed
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 g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision);
% Function that calls the stabilization algorithm.
% 
% Input: 
%   root_img_fname: The root name of the images before the id number.
%   ext:            The image extension (.jpg .png etc)
%   id_img_ref:     The id number of the reference image
%   id_img_first:   The id number of the first image of the sequence to 
%                   be stabilized
%   id_img_last:    The id number of the last image of the sequence to 
%                   be stabilized
%   precision:      The number of digit for the file name 
%                   (e.g.: IMG_0400.JPG would be 4).
%
% Output:
%   All stabilized images are rewritten as imaghe files.
%

fname_suffix = '_stable';

% Region of interest
display(' Load the roi.mat file containing the region of interest (roi)');
load roi.mat;


ndigit = ['%',num2str(precision),'.',num2str(precision),'i'];

% Read the reference image
img_ref = [root_img_name,num2str(id_img_ref,ndigit),ext];
im2     = imread(img_ref);
im2     = im2(:,:,1);


% Pyramid level for the stabilization.
% See documentation in the g_stabilize folder.
% L = 4 worked well on initial application but may require adjustments.
L = 4;

% Number of iterative improvement. Probably ok just 1 iteration.
niter = 1;

% Parameters for equalization. See help clahs for details.
nry = 4;
nrx = 4;
im2 = clahs(im2,nry,nrx);
im2 = double(im2);

% Remove mean and divide by the standard deviation.
inorm = find(roi > 0);
im2_norm = (im2 - nanmean(im2(inorm)))./nanstd(im2(inorm));

% Take the norm of the gradient of the image. This helps the
% stabilization algorithm
[im2x im2y] = gradient(im2_norm);
grad_im2    = sqrt(im2x.^2 + im2y.^2);

% This is the referecne frame for the stabilization algorithm.
frames(2).im = grad_im2;


figure(1);
imagesc(im2);
colormap(gray);
title('Reference image normalized intensity');

figure(2);
imagesc(grad_im2);
colormap(gray);
title('Gradient of the referemce image');


figure(3);
imagesc(roi);
title('roi');

answer = input('Happy with roi (y/n)? ','s');
if answer == 'n'
    return
end


for i = id_img_first:id_img_last
    
    i
    
    %img_to_stabilize = [root_img_name,num2str(i+1),ext];
    img_to_stabilize = [root_img_name,num2str(i,ndigit),ext];
    im1 = imread(img_to_stabilize);
    im1 = im1(:,:,1);
    
    im1 = clahs(im1,nry,nrx);
    
    im1 = double(im1);
    im1_norm = (im1 - nanmean(im1(inorm)))./nanstd(im1(inorm));
    [im1x im1y] = gradient(im1_norm);
    grad_im1 = sqrt(im1x.^2 + im1y.^2);
    
    frames(1).im = grad_im1;
    
    for iter = 1:niter
        [motion,stable] = g_videostabilize(frames,roi,L);
        frames(1).im = stable(1).im;
        %motion.A
        %motion.T
    end
    im_stable = g_warp(im1,motion.A,motion.T);
    im_stable = uint8(im_stable);
    %frames(2).im = stable(1).im;
        
    %imwrite(im_stable,[img_to_stabilize(1:end-4),fname_suffix,ext],'Quality',100);
    imwrite(im_stable,[img_to_stabilize(1:end-4),fname_suffix,'.jpg'],'Quality',100);
    
end