如何输出级联图像或堆叠图像

时间:2018-12-24 02:28:48

标签: image matlab drawing matlab-figure

enter image description here

请参见上图,如果有24张图片,如何使用MATLAB实现此输出效果。这种图经常出现在论文中。我定义了一个函数,但是只有一行代码无法实现。

function h = op(file_path, pad,m,n)
img_path_list = dir(strcat(file_path,'*.jpg'));
num = length(img_path_list);%
% [m,n]=size(image);
fw=n+(num-1)*pad;
fh=m+(num-1)*pad;
h=figure('position',[0,0,fw+pad,fh+pad]);
for j = 1:num
   image_name = img_path_list(j).name;
   image =  imread(strcat(file_path,image_name));
   hold on
   pd=(j-1)*pad;
   rpl=fw-n-pd;
   rpb=fh-m-pd;
%How to specify the location of the output on the image canvas
%  set('Position',[rpl rpb n m]);
%  axes('position',[rpl rpb n m]);
   imshow (image);
end
%  h=gcf;

1 个答案:

答案 0 :(得分:3)

您可以在imshow()函数中使用 Xdata Ydata 来设置每张图像的轴位置,以将它们显示在同一轴上,将它们堆叠在一起并彼此移位。固定为每个图像的单位。

下面给出了说明该过程的代码。

close all
% read the images in metrices
i1 = imread('onion.png');
i2 = imread('cameraman.tif');
i3 = imread('peppers.png');
i4 = imread('moon.tif');
i5 = imread('trees.tif');
i6 = imread('greens.jpg');
% create a cell array of the images
imgs = {i1, i2, i3, i4, i5, i6};

% variable to shift the position of each image
shift = 0;

% looping from 1 to length of the cell arrays
for i = 1:numel(imgs)

% display image, shifting the position to 2 units
% for each image on the same axis
imshow(imgs{i}, 'XData', [1+shift 10+shift], ...
    'YData', [1+shift 10+shift],'InitialMagnification', 400)
% hold on the axis
hold on
% increment the shift value
shift = shift + 2;
end

% set the axis limits
xlim([1 10+shift])
ylim([1 10+shift])
% hide the axis lines
axis off

示例输出

enter image description here