How can I reduce the space between images?

6 views (last 30 days)
The following source code has three problems:
  1. it can't show only one image.
  2. it doesn't preserve the original aspect ration of images
  3. space between images is too wide.
How can I fix these three issues?
draw_multiple_images.m
function draw_multiple_images(image_list)
d = size(image_list);
l = length(d);
figure;
hold all
colormap(gray(256));
% vector or cell-array
if(l==2)
N = length(image_list);
[m, n] = factor_out(N);
% images may be of differenet dimensions
if(iscell(image_list))
for k=1:N
h = subplot(m,n,k);
image(image_list{k},'Parent',h);
set(gca,'xtick',[],'ytick',[])
end
% must be of same dimension
elseif(isvector(image_list))
for k=1:N
h = subplot(m,n,k);
image(image_list(k),'Parent',h);
set(gca,'xtick',[],'ytick',[])
end
end
% 3D matrix of images (!!!)
elseif(l==3)
N = d(3) ;
[m, n] = factor_out(N);
for k=1:N
I = image_list(:,:,k);
subplot(m,n,k);
imshow(I);
set(gca,'xtick',[],'ytick',[])
end
end
hold off
function [m, n] = factor_out(input_number)
sqrtt = ceil(sqrt(input_number));
m = sqrtt;
n = sqrtt;
  2 Comments
Adam
Adam on 18 Jul 2017
You are better off creating and positioning your own axes than using subplot for images. subplot leaves a lot of space to allow for things like titles, axes labels, ticks etc, most or all of which you don't want for an image so you just get blank space instead.
doc axes properties
will guide you to settings regarding aspect ratio - DataAspectRatio and PlotBoxAspectRatio in particular.
I don't understand your first point - 'can't only show one image'?
Ba Ba Black Sheep!
Ba Ba Black Sheep! on 18 Jul 2017
image_name = 'lena.png';
I = gray_imread(image_name);
draw_multiple_images(I);
Doesn't show anything.

Sign in to comment.

Answers (1)

Jan
Jan on 18 Jul 2017
Use axes image to use the original dimensions.
You can find a lot of tools for adjusting the space between subplots in the FileExchange: FEX: Serach "subplot"
Beside searching in the FileExchange, you can find these solutions by using an internet search engine also.
Please explain "can't show only one image": What input are you using and what do you observe?
  4 Comments
Ba Ba Black Sheep!
Ba Ba Black Sheep! on 28 Jul 2017
function img = gray_imread( image_name )
I = imread(image_name);
if(is_rgb(I))
img = rgb2gray(I);
elseif (is_gray(I))
img = I;
end
function ret = is_gray( yourImage )
[~, ~, numberOfColorChannels] = size(yourImage);
if(numberOfColorChannels == 1)
ret = 1;
else
ret = 0;
end
function ret = is_rgb( a )
if size(a,3)==3
ret = 1;
else
ret = 0;
end
Image Analyst
Image Analyst on 28 Jul 2017
Also attach annotated screenshots indicating what you want to be positioned and sized where.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!