Skip to content
Snippets Groups Projects
Commit f68bc9dd authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Figures: added function for making figures from images

parent de7802ac
No related branches found
No related tags found
No related merge requests found
function gtMakeFigureFromPicture(img, mask, varargin)
conf = struct( ...
'filename', '', ...
'lims_u', [], ...
'lims_v', [], ...
'flip_ud', false, ...
'flip_lr', false, ...
'label', [], ...
'clims', [], ...
'pixel_to_cm', 0.05, ...
'borders', true, ...
'colorbar', true );
conf = parse_pv_pairs(conf, varargin);
use_mask = ~isempty(mask);
if (~use_mask)
mask = true(size(img, 1), size(img, 2));
end
if (~isempty(conf.lims_u))
mask([1:conf.lims_u(1)-1, conf.lims_u(2)+1:end], :) = 0;
end
if (~isempty(conf.lims_v))
mask(:, [1:conf.lims_v(1)-1, conf.lims_v(2)+1:end]) = 0;
end
% cuting the images
horz_profile = sum(mask, 1);
vert_profile = sum(mask, 2);
horz_lims = [find(horz_profile, 1, 'first'), find(horz_profile, 1, 'last')];
vert_lims = [find(vert_profile, 1, 'first'), find(vert_profile, 1, 'last')];
img = img(vert_lims(1):vert_lims(2), horz_lims(1):horz_lims(2), :);
mask = mask(vert_lims(1):vert_lims(2), horz_lims(1):horz_lims(2));
% Note the inversion between lr/up because of matlab's transpose problem
if (conf.flip_ud)
img = fliplr(img);
mask = fliplr(mask);
end
if (conf.flip_lr)
img = flipud(img);
mask = flipud(mask);
end
if (isempty(conf.clims))
if (size(img, 3) == 3)
clims_img = [0 1];
else
clims_img = [min(img(:)), max(img(:))];
end
else
clims_img = [conf.clims(1), min(conf.clims(2), max(img(:)))];
end
f = figure();
ax = axes('parent', f);
if (use_mask)
imagesc(permute(img, [2 1 3]), 'parent', ax, ...
'AlphaDataMapping', 'scaled', 'AlphaData', mask', ...
clims_img );
else
imagesc(permute(img, [2 1 3]), 'parent', ax, ...
clims_img );
end
colormap(ax, jet)
set_size(f, ax, conf, mask);
end
function set_size(f, ax, conf, img)
set(f, 'Units', 'centimeters')
img_size = size(img) * conf.pixel_to_cm;
if (~conf.borders)
figure_position = [0, 0, img_size];
set(f, 'Position', figure_position)
set(f, 'Paperposition', figure_position)
set(ax, 'Units', 'normalized')
set(ax, 'Position', [0, 0, 1, 1])
fprintf('Axes size: [%g, %g] cm\n', figure_position(3:4))
else
if (~isempty(conf.label))
upper_padding = 2;
lower_offset = 1.75;
else
upper_padding = 1.5;
lower_offset = 1;
end
set(ax, 'Units', 'centimeters')
if (~conf.colorbar)
figure_position = [0, 0, img_size] + [0, 0, 2, upper_padding];
set(f, 'Position', figure_position)
set(f, 'Paperposition', figure_position)
else
figure_position = [0, 0, img_size] + [0, 0, 3, upper_padding];
set(f, 'Position', figure_position)
set(f, 'Paperposition', figure_position)
cb = colorbar('peer', ax, 'location', 'EastOutside');
set(cb, 'Units', 'centimeters')
position = [img_size(1)+1.35, lower_offset, 0.65, img_size(2)];
set(cb, 'Position', position)
end
axes_position = [1, lower_offset, img_size];
set(ax, 'Position', axes_position)
if (~isempty(conf.label))
t = uicontrol('Style', 'Text', 'parent', f, ...
'String', conf.label, 'FontWeight', 'bold');
set(t, 'Units', 'centimeters')
position = [1, 0.25, img_size(1), 0.75];
set(t, 'Position', position)
end
fprintf('Axes size: [%g, %g] cm\n', axes_position(3:4))
end
box(ax, 'Off')
fprintf('Figure size: [%g, %g] cm\n', figure_position(3:4))
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment