MATLAB【八】———— matlab 读取单个(多个)文件夹中所有图像
生活随笔
收集整理的這篇文章主要介紹了
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_numfile_path = path{i}; % 圖像文件夾路徑img_path_list = dir(strcat(file_path,'*.jpg'));img_num = length(img_path_list); %該文件夾中圖像數量if img_num > 0for j = 1:img_numimage_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));% 顯示正在處理的路徑和圖像名%圖像處理過程 省略endend
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
?
總結
以上是生活随笔為你收集整理的MATLAB【八】———— matlab 读取单个(多个)文件夹中所有图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最好忘了吧是什么歌呢
- 下一篇: ISP【二】————camera ir图