MATLAB【八】———— matlab 读取单个(多个)文件夹中所有图像

0.matlab 移动(复制)文件到另一个文件夹

sourcePath = '.\Square_train';  
targetPath = '.\Square_test';  
 
fileList = dir(sourcePath);  
  
for k = 3 :5: length(fileList)  
    movefile([sourcePath,'\',fileList(k).name],targetPath);
end 
 
%copyfile([sourcePath,'\',fileList(k).name],targetPath)

1.选择文件夹路径:

[filename filepath]=uigetfile('*.*','请选择文件');%filename为文件名,filepath为文件路径
image =  imread(strcat(file_path,image_name));%读取图片文件

2. 指定路径下单个文件夹data中所有图像

注,下述的代码只能读取data文件夹中的图像,假设data中包含子文件夹,不能读取子文件夹中的图像。

file_path =  '.\data\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
        for j = 1:img_num %逐一读取图像
            image_name = img_path_list(j).name;% 图像名
            image =  imread(strcat(file_path,image_name));
            fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
            %图像处理过程 省略
        end
end

3. 指定路径下 多个文件夹中所有图像,该代码可以读取文件夹data中及data的所有子文件夹中的图像。

p = genpath('.\data');% 获得文件夹data下所有子文件的路径,这些路径存在字符串p中,以';'分割
length_p = size(p,2);%字符串p的长度
path = {};%建立一个单元数组,数组的每个单元中包含一个目录
temp = [];
for i = 1:length_p %寻找分割符';',一旦找到,则将路径temp写入path数组中
    if p(i) ~= ';'
        temp = [temp p(i)];
    else 
        temp = [temp '\']; %在路径的最后加入 '\'
        path = [path ; temp];
        temp = [];
    end
end  
clear p length_p temp;
%至此获得data文件夹及其所有子文件夹(及子文件夹的子文件夹)的路径,存于数组path中。
%下面是逐一文件夹中读取图像
file_num = size(path,1);% 子文件夹的个数
for i = 1:file_num
    file_path =  path{i}; % 图像文件夹路径
    img_path_list = dir(strcat(file_path,'*.jpg'));
    img_num = length(img_path_list); %该文件夹中图像数量
    if img_num > 0
        for j = 1:img_num
            image_name = img_path_list(j).name;% 图像名
            image =  imread(strcat(file_path,image_name));
            fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的路径和图像名
            %图像处理过程 省略
        end
    end
end

4.逐一读取某文件夹中的图片:

 
img_path_list = dir(strcat(file_path,'*.bmp'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
        for j = 1:img_num %逐一读取图像
            image_name = img_path_list(j).name;% 图像名
            image =  imread(strcat(file_path,image_name));%读取图像
 
            %对单幅图像进行操作
 
       end
 

 5.批量适配图片信息与excel/txt等文档信息,批量移动拷贝图片,批量存图片中点和框方 https://blog.csdn.net/Darlingqiang/article/details/107731523

6.脚本实现批量处理图片

https://blog.csdn.net/Darlingqiang/article/details/107734320

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大江东去浪淘尽千古风流人物

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值