matlab连续自动读取文件夹里面的照片代码
时间: 2024-10-12 08:15:16 浏览: 39
在MATLAB中,你可以使用`dir`函数获取文件夹中的所有文件信息,然后遍历文件列表,对图片文件(通常扩展名为`.jpg`, `.png`等)进行读取。下面是一个简单的示例代码片段:
```matlab
% 设置文件夹路径
folder_path = 'your_folder_directory'; % 替换为你要读取的文件夹路径
% 获取文件夹下的所有文件信息
files = dir(fullfile(folder_path, '*.*')); % '*'匹配任意文件名,'*.*'匹配所有文件
% 遍历文件,只处理.jpg和.png格式的照片
images = {};
for i = 1:length(files)
if strcmp(files(i).name(end-3:end), {'jpg', 'png'})
% 使用imread函数读取图片
img = imread(fullfile(folder_path, files(i).name));
% 将图片添加到cell数组中
images{i} = img;
end
end
% 查看第一个图片
imshow(images{1});
% 如果你想将所有图片保存到一个新的文件夹,可以创建该文件夹并使用saveas
new_folder_path = 'new_images_folder';
mkdir(new_folder_path);
for i = 1:length(images)
saveas(fullfile(new_folder_path, ['image_' num2str(i) '.jpg']), images{i});
end
阅读全文
相关推荐


















