Commit 972d1377 authored by Pascal Bourgault's avatar Pascal Bourgault

Voir commit precedent.

parent f83fc5a2
function g_viz_field(imgFname,outputFname);
function [h_figure,h_pcolor,h_datetext] = g_viz_field(imgFname,rectFile,varargin)
%G_VIZ_FIELD Generates a map with a georectified image
% G_VIZ_FIELD Generates a map with the georectified image in imgFname
% Converts in grayscale if needed.
% Inputs:
% imgFname, filename of the image to georectify
% rectFile, .mat file created by g_rect
% Parameters (name, value)
% showTime, Default 0, if 1, displays the timestamp of the
% image in the figure's title
% showLand, Default 0, if 'f' or 'h', displays the land contour
% with m_gshhs_f or m_gshhs_f
% landcolor, Default [241 235 144]/255, Color of the land on the
% map
%
% Outputs:
% h_figure, h_pcolor and h_datetext are the handles to the
% corresponding objects in the figure, for use in g_viz_anim
%
show_time = 0;
show_land = 0;
land_color = [241 235 144]/255;
if length(varargin) > 1
for i=1:2:length(varargin)
switch lower(varargin{i})
case 'showtime'
show_time = varargin{i+1};
case 'showland'
show_land = varargin{i+1};
case 'landcolor'
land_color = varargin{i+1};
end
end
end
% Set some plotting parameters
ms = 10; % Marker Size
fs = 10; % Font size
lw = 2; % Line width
load(outputFname);
load(rectFile);
[m n p] = size(LON);
%p = size(LON,3);
lon_min = min(lon_gcp);
lon_max = max(lon_gcp);
......@@ -26,9 +61,9 @@ lat_max= lat_max + fac*abs(lat_max-lat_min);
rgb0 = double(imread(imgFname))/255;
[mm nn pp] = size(rgb0);
pp = size(rgb0,3);
if pp == 3
int = (rgb0(:,:,1)+rgb0(:,:,2)+rgb0(:,:,3))/3;
int = g_rgb2gray(rgb0);
else
int = rgb0;
end
......@@ -37,22 +72,38 @@ int = int';
int = int - nanmean(nanmean(int));
land_color = [241 235 144]/255;
m_proj('mercator','longitudes',[lon_min lon_max],'latitudes',[lat_min lat_max]);
hold on;
[X,Y] = m_ll2xy(LON,LAT);
colormap(gray);
cmap = contrast(int,256);
colormap(cmap);
h = pcolor(X,Y,int);
shading('interp');
% Uncomment one of these lines if you want the coastline to be pltoted.
%m_gshhs_f('patch',land_color)
%m_gshhs_h('patch',land_color)
if show_land
if strcmpi(show_land,'f')
m_gshhs_f('patch',land_color)
else
m_gshhs_h('patch',land_color)
end
end
if show_time
info = imfinfo(imgFname);
if isField(info,'DateTime')
date = info.DateTime;
elseif isField(info,'Comment')
date = info.Comment;
else
date = '';
end
ht = title(date);
end
m_plot(LON0,LAT0,'kx','markersize',ms,'linewidth',lw); % Camera location
%% Plot GCPs and ICPs.
......@@ -66,4 +117,12 @@ end
clear LON LAT X Y
m_grid('box','fancy','fontsize',fs);
\ No newline at end of file
m_grid('box','fancy','fontsize',fs);
if nargout > 1
h_figure = gcf;
h_pcolor = h;
if nargout == 3
h_datetext = ht;
end
end
function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision);
% Function that calls the stabilization algorithm.
function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision,varargin)
% Function that calls the stabilization algorithm. The stabilized files
% contain the datetime values stored in the 'DateTime' or the 'Comment'
% field of the unstabilized images.
%
% Input:
% root_img_fname: The root name of the images before the id number.
% ext: The image extension (.jpg .png etc)
% ext: The image extension (.jpg .png etc), is case
% insensitive
% 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
......@@ -11,6 +14,13 @@ function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,preci
% be stabilized
% precision: The number of digit for the file name
% (e.g.: IMG_0400.JPG would be 4).
% Parameters, Name, Value
% roiFile, [Defaults: roi.mat] Name of the file with roi
% information.
% auto, [Defaults: 0 (false)] If true, overrides any
% interaction with the user (for use in scripts)
% suffix, [Defaults: '_stable'] Suffix to add to the
% stabilized images.
%
% Output:
% 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
% IMG_0002.JPG to IMG_0020.JPG
fname_suffix = '_stable';
auto = 0;
roifile = 'roi.mat';
if length(varargin) > 1
for i=1:2:length(varargin)
if strcmpi(varargin{i},'roiFile')
roifile = varargin{i+1};
elseif strcmpi(varargin{i},'auto')
auto = varargin{i+1};
elseif strcmpi(varargin{i},'suffix')
fname_suffix = varargin{i+1};
end
end
end
hw = waitbar(0,'Preparing images');
% Region of interest
display(' Load the roi.mat file containing the region of interest (roi)');
load roi.mat;
display(' Loading the roi.mat file containing the region of interest (roi)');
load(roifile);
ndigit = ['%',num2str(precision),'.',num2str(precision),'i'];
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);
try
img_ref = [root_img_name,num2str(id_img_ref,ndigit),lower(ext)];
im2 = double(imread(img_ref))/255;
catch
img_ref = [root_img_name,num2str(id_img_ref,ndigit),upper(ext)];
im2 = double(imread(img_ref))/255;
end
if size(im2,3) ~= 1
im2 = uint8(g_rgb2gray(im2)*255);
end
% Pyramid level for the stabilization.
......@@ -56,7 +88,7 @@ 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);
[im2x, im2y] = gradient(im2_norm);
grad_im2 = sqrt(im2x.^2 + im2y.^2);
% This is the referecne frame for the stabilization algorithm.
......@@ -78,41 +110,60 @@ figure(3);
imagesc(roi);
title('roi');
answer = input('Happy with roi (y/n)? ','s');
if answer == 'n'
return
if ~auto
answer = input('Happy with roi (y/n)? ','s');
if answer == 'n'
return
end
end
num_img = id_img_last-id_img_first;
time_per_img = 0;
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);
time_rem = (id_img_last-i+1)*time_per_img;
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));
total = tic;
%Reading current image;
try
img_to_stabilize = [root_img_name,num2str(i,ndigit),lower(ext)];
im1 = imread(img_to_stabilize);
catch
img_to_stabilize = [root_img_name,num2str(i,ndigit),upper(ext)];
im1 = imread(img_to_stabilize);
end
im_info = imfinfo(img_to_stabilize);
if size(im1,3) ~= 1
im1 = uint8(g_rgb2gray(double(im1)/255)*255);
end
im1 = clahs(im1,nry,nrx);
im1 = double(im1);
im1_norm = (im1 - nanmean(im1(inorm)))./nanstd(im1(inorm));
[im1x im1y] = gradient(im1_norm);
[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);
if isfield(im_info,'DateTime')
datetime = im_info.DateTime;
elseif isfield(im_info,'Comment')
datetime = im_info.Comment;
else
datetime = '';
end
imwrite(im_stable,[img_to_stabilize(1:end-4) fname_suffix '.jpg'],'Quality',100,'Comment',datetime);
time_per_img = toc(total);
end
close(hw)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment