Commit 5934cd22 authored by Daniel Bourgault's avatar Daniel Bourgault

g_stabilize rewritten and simplified

parent 972d1377
function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,precision,varargin) function g_stabilize(img_ref_fname,varargin)
% Function that calls the stabilization algorithm. The stabilized files % This function and its sub-functions stabilize all images found in the
% working folder against a reference image. The stabilized files
% contain the datetime values stored in the 'DateTime' or the 'Comment' % contain the datetime values stored in the 'DateTime' or the 'Comment'
% field of the unstabilized images. % field of the unstabilized images.
% %
% Input: % Input:
% root_img_fname: The root name of the images before the id number. % img_ref_fname: The complete image name, with extension (and path if
% ext: The image extension (.jpg .png etc), is case % necessary), of the reference image in the current
% insensitive % directory.
% 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).
% Parameters, Name, Value % Parameters, Name, Value
% roiFile, [Defaults: roi.mat] Name of the file with roi % roiFile, [Defaults: roi.mat] Name of the file with roi
% information. % information.
...@@ -23,14 +18,34 @@ function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,preci ...@@ -23,14 +18,34 @@ function g_stabilize(root_img_name,ext,id_img_ref,id_img_first,id_img_last,preci
% stabilized images. % stabilized images.
% %
% Output: % Output:
% All stabilized images are rewritten as image files. % All stabilized images are rewritten as image files in the subfolder
% /stable.
% %
% Example: % Example:
% g_stabilize('IMG_', '.JPG', 1, 2, 20, 4); % g_stabilize('IMG_4018.jpg');
% %
% Will run the stabilization reference to IMG_0001.JPG, on
% IMG_0002.JPG to IMG_0020.JPG
%% Some parameters
% Pyramid level for the stabilization.
% See documentation in the g_stabilize folder.
% L = 4 worked well on initial application but may require adjustments for
% particular application.
L = 4;
% Number of iterative improvement. Probably ok just 1 iteration.
niter = 1;
% Parameters for equalization. See help clahs for details.
% This may not be needed fpr most application but may help other cases.
% Need to play with it if problems.
equalize = false;
nry = 4;
nrx = 4;
% Extract the path and extension of the reference image
[pathstr,name,ext] = fileparts(img_ref_fname);
% Check for optional options
fname_suffix = '_stable'; fname_suffix = '_stable';
auto = 0; auto = 0;
roifile = 'roi.mat'; roifile = 'roi.mat';
...@@ -47,64 +62,55 @@ if length(varargin) > 1 ...@@ -47,64 +62,55 @@ if length(varargin) > 1
end end
hw = waitbar(0,'Preparing images'); hw = waitbar(0,'Preparing images');
% Region of interest
display(' Loading the roi.mat file containing the region of interest (roi)');
load(roifile);
ndigit = ['%',num2str(precision),'.',num2str(precision),'i'] %% Load the Region of interest (roi) file
display(' Loading the roi.mat file containing the region of interest (roi)');
if length(pathstr) == 0 % Images are in the current folder
load(roifile);
else
load([pathstr,'/',roifile]);
end
% Read the reference image % Read the reference image
try im2 = imread(img_ref_fname);
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
% Convert into gray scale if not already a B/W image
if size(im2,3) ~= 1 if size(im2,3) ~= 1
im2 = uint8(g_rgb2gray(im2)*255); im2 = g_rgb2gray(im2);
end end
% May equalize with this equalizer. Careful this doesn't always work well
% and may compromise the stabilization. Use with care.
if equalize
im2 = clahs(im2,nry,nrx);
end
% 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); im2 = double(im2);
% Remove mean and divide by the standard deviation. % Remove mean and divide by the standard deviation.
inorm = find(roi > 0); % This generally help the algorithm to compare images
inorm = find(roi > 0);
im2_norm = (im2 - nanmean(im2(inorm)))./nanstd(im2(inorm)); im2_norm = (im2 - nanmean(im2(inorm)))./nanstd(im2(inorm));
% Take the norm of the gradient of the image. This helps the % Take the norm of the gradient of the image. This helps the
% stabilization algorithm % stabilization algorithm, nut again you may want to explore
% without it
[im2x, im2y] = gradient(im2_norm); [im2x, im2y] = gradient(im2_norm);
grad_im2 = sqrt(im2x.^2 + im2y.^2); grad_im2 = sqrt(im2x.^2 + im2y.^2);
% This is the referecne frame for the stabilization algorithm. % The second frame is the reference frame for the stabilization algorithm.
frames(2).im = grad_im2; frames(2).im = grad_im2;
%% Some figures to make sure everything seems ok
figure(1); figure(1);
imagesc(im2); imagesc(im2);
colormap(gray); colormap(gray);
title('Reference image normalized intensity'); title('Reference image. Intensity normalized over ROI');
figure(2); figure(2);
imagesc(grad_im2); imagesc(log10(grad_im2+eps));
colormap(gray); colormap(gray);
title('Gradient of the referemce image'); title('Gradient of the reference image on a log scale');
figure(3); figure(3);
imagesc(roi); imagesc(roi);
...@@ -116,45 +122,84 @@ if ~auto ...@@ -116,45 +122,84 @@ if ~auto
return return
end end
end end
num_img = id_img_last-id_img_first;
%% Create the folder where to write the stable images
if length(pathstr) == 0 % Images are in the current folder
[status,message,messageid] = mkdir('stable');
else
[status,message,messageid] = mkdir([pathstr,'/stable']);
end
% Check for images already stablilized that would be found in the
% stable folder. This is not yet fully implemeted. The goal would
% be not to perform the stabilization to images already stabilized
%if strcmp(message,'Directory already exists.')
% if length(pathstr) == 0
% img_done = dir(['stable/*',ext]);
% else
% img_done = dir([pathstr,'/stable/','*',ext]);
% end
%end
%% Read all filemanes in the current directory with the same extension as the ref img
if length(pathstr) == 0 % Images are in the current folder
all_img = dir(['*',ext]);
else
all_img = dir([pathstr,'/','*',ext]);
end
% Number of images to proceed
N = length(all_img);
time_per_img = 0; time_per_img = 0;
for i = id_img_first:id_img_last
time_rem = (id_img_last-i+1)*time_per_img; for i = 1:N
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));
time_rem = (N-i+1)*time_per_img;
if i > 1
waitbar((i-1)/N,hw,sprintf('Stabilizing image #%d. %.0fh%02.2fmn remaining',...
i,fix(time_rem/3600),rem(time_rem,3600)/60));
else
waitbar((i-1)/N,hw,sprintf('Stabilizing image #%d.',...
i,fix(time_rem/3600),rem(time_rem,3600)/60));
end
total = tic; total = tic;
%Reading current image;
try if length(pathstr) == 0
img_to_stabilize = [root_img_name,num2str(i,ndigit),lower(ext)]; im1 = imread(all_img(i).name);
im1 = imread(img_to_stabilize); im_info = imfinfo(all_img(i).name);
else
catch im1 = imread([pathstr,'/',all_img(i).name]);
img_to_stabilize = [root_img_name,num2str(i,ndigit),upper(ext)]; im_info = imfinfo([pathstr,'/',all_img(i).name]);
im1 = imread(img_to_stabilize);
end end
im_info = imfinfo(img_to_stabilize);
% Convert into gray scale if not already a B/W image
if size(im1,3) ~= 1 if size(im1,3) ~= 1
im1 = uint8(g_rgb2gray(double(im1)/255)*255); im1 = g_rgb2gray(im1);
end
% May equalize with this equalizer.
if equalize
im1 = clahs(im1,nry,nrx);
end end
im1 = clahs(im1,nry,nrx);
im1 = double(im1); im1 = double(im1);
im1_norm = (im1 - nanmean(im1(inorm)))./nanstd(im1(inorm)); 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); grad_im1 = sqrt(im1x.^2 + im1y.^2);
frames(1).im = grad_im1; frames(1).im = grad_im1;
for iter = 1:niter for iter = 1:niter
[motion,stable] = g_videostabilize(frames,roi,L); [motion,stable] = g_videostabilize(frames,roi,L);
frames(1).im = stable(1).im; frames(1).im = stable(1).im;
%motion.A
%motion.T
end end
im_stable = g_warp(im1,motion.A,motion.T); im_stable = g_warp(im1,motion.A,motion.T);
im_stable = uint8(im_stable); im_stable = uint8(im_stable);
%frames(2).im = stable(1).im;
if isfield(im_info,'DateTime') if isfield(im_info,'DateTime')
datetime = im_info.DateTime; datetime = im_info.DateTime;
elseif isfield(im_info,'Comment') elseif isfield(im_info,'Comment')
...@@ -162,8 +207,17 @@ for i = id_img_first:id_img_last ...@@ -162,8 +207,17 @@ for i = id_img_first:id_img_last
else else
datetime = ''; datetime = '';
end end
imwrite(im_stable,[img_to_stabilize(1:end-4) fname_suffix '.jpg'],'Quality',100,'Comment',datetime);
if length(pathstr) == 0
stable_img_fname = ['stable/',all_img(i).name(1:end-4)]
else
stable_img_fname = [pathstr,'/stable/',all_img(i).name(1:end-4)]
end
imwrite(im_stable,[stable_img_fname fname_suffix ext],...
'Quality',100,'Comment',datetime);
time_per_img = toc(total); time_per_img = toc(total);
end end
close(hw) 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