matlab 显示文件夹所有图片

时间:2022-01-07 05:19:02

最近写实验报告,想多插入几个图,因为涉及到很多图片,为了介绍这个小数据集,想将所有图片放在一起列出来,所以自己写了一个标准化的小函数,如有帮助那就最好,也希望大家多多指正。

效果:

matlab 显示文件夹所有图片

function Result = listimage(path, picsize, row_num)
%show all the images
%3-channel images
%Input:
% path:the path of the image
% picsize:the picsize of image to be shown in the result default = [100 100]
% row_num:the num of each row default = 10
%Output:
% Result:the result image
%Author:
% 卍KID
%Time:
% 2014.3.16(SZU)

if nargin == 1
picsize = [100 ,100];
row_num = 10;
end

if nargin == 2
row_num = 10;
end

ext = {'*.jpeg','*.jpg','*.png','*.pgm','*.bmp'};
files = [];

for i = 1:length(ext)
files = [files dir([path ext{i}])];
end

l = fix(length(files)/row_num);

Result = zeros((l+1)*picsize(1) ,row_num*picsize(2) ,3);

for i = 1:length(files)

Img = imread([path files(i).name]);
size_img = size(Img);
if size_img(3) ~= 3
error('this is not a 3 channel image');
end
Img = im2double(Img);
newImg = imresize(Img ,picsize ,'bicubic');

n = mod(i-1 ,row_num);
newn = picsize(2)*n;

m = fix((i-1)/row_num);
newm = picsize(1)*m;

Result(newm+1 : newm+picsize(1) ,newn+1 : newn+picsize(2) ,1) = newImg(:,:,1);
Result(newm+1 : newm+picsize(1) ,newn+1 : newn+picsize(2) ,2) = newImg(:,:,2);
Result(newm+1 : newm+picsize(1) ,newn+1 : newn+picsize(2) ,3) = newImg(:,:,3);

end
imshow(Result);