Commit 972d137730e7df5382f4272392bb99c98a4598c6

Authored by Pascal Bourgault
1 parent f83fc5a2
Exists in master

Voir commit precedent.

Showing 2 changed files with 150 additions and 40 deletions   Show diff stats
src/g_rect/g_viz_field.m
1   -function g_viz_field(imgFname,outputFname);
  1 +function [h_figure,h_pcolor,h_datetext] = g_viz_field(imgFname,rectFile,varargin)
  2 +%G_VIZ_FIELD Generates a map with a georectified image
  3 +% G_VIZ_FIELD Generates a map with the georectified image in imgFname
  4 +% Converts in grayscale if needed.
  5 +% Inputs:
  6 +% imgFname, filename of the image to georectify
  7 +% rectFile, .mat file created by g_rect
  8 +% Parameters (name, value)
  9 +% showTime, Default 0, if 1, displays the timestamp of the
  10 +% image in the figure's title
  11 +% showLand, Default 0, if 'f' or 'h', displays the land contour
  12 +% with m_gshhs_f or m_gshhs_f
  13 +% landcolor, Default [241 235 144]/255, Color of the land on the
  14 +% map
  15 +%
  16 +% Outputs:
  17 +% h_figure, h_pcolor and h_datetext are the handles to the
  18 +% corresponding objects in the figure, for use in g_viz_anim
  19 +%
  20 +
  21 +show_time = 0;
  22 +show_land = 0;
  23 +land_color = [241 235 144]/255;
  24 +if length(varargin) > 1
  25 + for i=1:2:length(varargin)
  26 + switch lower(varargin{i})
  27 + case 'showtime'
  28 + show_time = varargin{i+1};
  29 + case 'showland'
  30 + show_land = varargin{i+1};
  31 + case 'landcolor'
  32 + land_color = varargin{i+1};
  33 + end
  34 + end
  35 +end
  36 +
2 37  
3 38 % Set some plotting parameters
4 39 ms = 10; % Marker Size
5 40 fs = 10; % Font size
6 41 lw = 2; % Line width
7 42  
8   -load(outputFname);
  43 +load(rectFile);
9 44  
10   -[m n p] = size(LON);
  45 +%p = size(LON,3);
11 46  
12 47 lon_min = min(lon_gcp);
13 48 lon_max = max(lon_gcp);
... ... @@ -26,9 +61,9 @@ lat_max= lat_max + fac*abs(lat_max-lat_min);
26 61  
27 62 rgb0 = double(imread(imgFname))/255;
28 63  
29   -[mm nn pp] = size(rgb0);
  64 +pp = size(rgb0,3);
30 65 if pp == 3
31   - int = (rgb0(:,:,1)+rgb0(:,:,2)+rgb0(:,:,3))/3;
  66 + int = g_rgb2gray(rgb0);
32 67 else
33 68 int = rgb0;
34 69 end
... ... @@ -37,22 +72,38 @@ int = int';
37 72  
38 73 int = int - nanmean(nanmean(int));
39 74  
40   -land_color = [241 235 144]/255;
  75 +
41 76  
42 77 m_proj('mercator','longitudes',[lon_min lon_max],'latitudes',[lat_min lat_max]);
43 78 hold on;
44 79  
45 80 [X,Y] = m_ll2xy(LON,LAT);
46   -
47   -colormap(gray);
  81 +cmap = contrast(int,256);
  82 +colormap(cmap);
48 83 h = pcolor(X,Y,int);
49 84 shading('interp');
50 85  
51 86  
52 87 % Uncomment one of these lines if you want the coastline to be pltoted.
53   -%m_gshhs_f('patch',land_color)
54   -%m_gshhs_h('patch',land_color)
  88 +if show_land
  89 + if strcmpi(show_land,'f')
  90 + m_gshhs_f('patch',land_color)
  91 + else
  92 + m_gshhs_h('patch',land_color)
  93 + end
  94 +end
55 95  
  96 +if show_time
  97 + info = imfinfo(imgFname);
  98 + if isField(info,'DateTime')
  99 + date = info.DateTime;
  100 + elseif isField(info,'Comment')
  101 + date = info.Comment;
  102 + else
  103 + date = '';
  104 + end
  105 + ht = title(date);
  106 +end
56 107 m_plot(LON0,LAT0,'kx','markersize',ms,'linewidth',lw); % Camera location
57 108  
58 109 %% Plot GCPs and ICPs.
... ... @@ -66,4 +117,12 @@ end
66 117  
67 118 clear LON LAT X Y
68 119  
69   -m_grid('box','fancy','fontsize',fs);
70 120 \ No newline at end of file
  121 +m_grid('box','fancy','fontsize',fs);
  122 +
  123 +if nargout > 1
  124 + h_figure = gcf;
  125 + h_pcolor = h;
  126 + if nargout == 3
  127 + h_datetext = ht;
  128 + end
  129 +end
... ...
src/g_stabilize/g_stabilize.m
1   -function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision);
2   -% Function that calls the stabilization algorithm.
  1 +function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision,varargin)
  2 +% Function that calls the stabilization algorithm. The stabilized files
  3 +% contain the datetime values stored in the 'DateTime' or the 'Comment'
  4 +% field of the unstabilized images.
3 5 %
4 6 % Input:
5 7 % root_img_fname: The root name of the images before the id number.
6   -% ext: The image extension (.jpg .png etc)
  8 +% ext: The image extension (.jpg .png etc), is case
  9 +% insensitive
7 10 % id_img_ref: The id number of the reference image
8 11 % id_img_first: The id number of the first image of the sequence to
9 12 % be stabilized
... ... @@ -11,6 +14,13 @@ function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,preci
11 14 % be stabilized
12 15 % precision: The number of digit for the file name
13 16 % (e.g.: IMG_0400.JPG would be 4).
  17 +% Parameters, Name, Value
  18 +% roiFile, [Defaults: roi.mat] Name of the file with roi
  19 +% information.
  20 +% auto, [Defaults: 0 (false)] If true, overrides any
  21 +% interaction with the user (for use in scripts)
  22 +% suffix, [Defaults: '_stable'] Suffix to add to the
  23 +% stabilized images.
14 24 %
15 25 % Output:
16 26 % All stabilized images are rewritten as image files.
... ... @@ -22,18 +32,40 @@ function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,preci
22 32 % IMG_0002.JPG to IMG_0020.JPG
23 33  
24 34 fname_suffix = '_stable';
  35 +auto = 0;
  36 +roifile = 'roi.mat';
  37 +if length(varargin) > 1
  38 + for i=1:2:length(varargin)
  39 + if strcmpi(varargin{i},'roiFile')
  40 + roifile = varargin{i+1};
  41 + elseif strcmpi(varargin{i},'auto')
  42 + auto = varargin{i+1};
  43 + elseif strcmpi(varargin{i},'suffix')
  44 + fname_suffix = varargin{i+1};
  45 + end
  46 + end
  47 +end
25 48  
  49 +hw = waitbar(0,'Preparing images');
26 50 % Region of interest
27   -display(' Load the roi.mat file containing the region of interest (roi)');
28   -load roi.mat;
  51 +display(' Loading the roi.mat file containing the region of interest (roi)');
  52 +load(roifile);
29 53  
30 54  
31   -ndigit = ['%',num2str(precision),'.',num2str(precision),'i'];
  55 +ndigit = ['%',num2str(precision),'.',num2str(precision),'i']
32 56  
33 57 % Read the reference image
34   -img_ref = [root_img_name,num2str(id_img_ref,ndigit),ext];
35   -im2 = imread(img_ref);
36   -im2 = im2(:,:,1);
  58 +try
  59 + img_ref = [root_img_name,num2str(id_img_ref,ndigit),lower(ext)];
  60 + im2 = double(imread(img_ref))/255;
  61 +catch
  62 + img_ref = [root_img_name,num2str(id_img_ref,ndigit),upper(ext)];
  63 + im2 = double(imread(img_ref))/255;
  64 +end
  65 +
  66 +if size(im2,3) ~= 1
  67 + im2 = uint8(g_rgb2gray(im2)*255);
  68 +end
37 69  
38 70  
39 71 % Pyramid level for the stabilization.
... ... @@ -56,7 +88,7 @@ im2_norm = (im2 - nanmean(im2(inorm)))./nanstd(im2(inorm));
56 88  
57 89 % Take the norm of the gradient of the image. This helps the
58 90 % stabilization algorithm
59   -[im2x im2y] = gradient(im2_norm);
  91 +[im2x, im2y] = gradient(im2_norm);
60 92 grad_im2 = sqrt(im2x.^2 + im2y.^2);
61 93  
62 94 % This is the referecne frame for the stabilization algorithm.
... ... @@ -78,41 +110,60 @@ figure(3);
78 110 imagesc(roi);
79 111 title('roi');
80 112  
81   -answer = input('Happy with roi (y/n)? ','s');
82   -if answer == 'n'
83   - return
  113 +if ~auto
  114 + answer = input('Happy with roi (y/n)? ','s');
  115 + if answer == 'n'
  116 + return
  117 + end
84 118 end
85   -
86   -
  119 +num_img = id_img_last-id_img_first;
  120 +time_per_img = 0;
87 121 for i = id_img_first:id_img_last
88   -
89   - i
90   -
91   - %img_to_stabilize = [root_img_name,num2str(i+1),ext];
92   - img_to_stabilize = [root_img_name,num2str(i,ndigit),ext];
93   - im1 = imread(img_to_stabilize);
94   - im1 = im1(:,:,1);
  122 + time_rem = (id_img_last-i+1)*time_per_img;
  123 + waitbar((i-id_img_first)/num_img,hw,sprintf('Stabilizing image #%d. %.0fh%02.2fmn remaining',i,fix(time_rem/3600),rem(time_rem,3600)/60));
  124 + total = tic;
  125 + %Reading current image;
  126 + try
  127 + img_to_stabilize = [root_img_name,num2str(i,ndigit),lower(ext)];
  128 + im1 = imread(img_to_stabilize);
  129 +
  130 + catch
  131 + img_to_stabilize = [root_img_name,num2str(i,ndigit),upper(ext)];
  132 + im1 = imread(img_to_stabilize);
  133 + end
  134 + im_info = imfinfo(img_to_stabilize);
  135 + if size(im1,3) ~= 1
  136 + im1 = uint8(g_rgb2gray(double(im1)/255)*255);
  137 + end
95 138  
96 139 im1 = clahs(im1,nry,nrx);
97   -
98 140 im1 = double(im1);
  141 +
99 142 im1_norm = (im1 - nanmean(im1(inorm)))./nanstd(im1(inorm));
100   - [im1x im1y] = gradient(im1_norm);
  143 + [im1x, im1y] = gradient(im1_norm);
101 144 grad_im1 = sqrt(im1x.^2 + im1y.^2);
102 145  
103 146 frames(1).im = grad_im1;
104   -
105 147 for iter = 1:niter
106 148 [motion,stable] = g_videostabilize(frames,roi,L);
107 149 frames(1).im = stable(1).im;
108 150 %motion.A
109 151 %motion.T
110 152 end
  153 +
111 154 im_stable = g_warp(im1,motion.A,motion.T);
112 155 im_stable = uint8(im_stable);
113 156 %frames(2).im = stable(1).im;
114   -
115   - %imwrite(im_stable,[img_to_stabilize(1:end-4),fname_suffix,ext],'Quality',100);
116   - imwrite(im_stable,[img_to_stabilize(1:end-4),fname_suffix,'.jpg'],'Quality',100);
117   -
  157 +
  158 + if isfield(im_info,'DateTime')
  159 + datetime = im_info.DateTime;
  160 + elseif isfield(im_info,'Comment')
  161 + datetime = im_info.Comment;
  162 + else
  163 + datetime = '';
  164 + end
  165 + imwrite(im_stable,[img_to_stabilize(1:end-4) fname_suffix '.jpg'],'Quality',100,'Comment',datetime);
  166 +
  167 + time_per_img = toc(total);
118 168 end
  169 +close(hw)
119 170 \ No newline at end of file
... ...