1. 程式人生 > >深度學習-MATLAB資料增廣

深度學習-MATLAB資料增廣

圖片資料增廣

對資料夾及其子資料夾下的圖片資料進行簡單的增廣,四個方面,翻轉,平移,增加高斯噪聲,對比度增強,儲存在當前資料夾中,命名為圖片原名+ -1、-2、-3、-4,代表四個類。

p=genpath('檔案的路徑');%獲取指定資料夾下所有子檔案的路徑
%這些路徑存在於字串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 %獲得了指定資料夾及其所有子資料夾,接下來就是逐一資料夾讀取影象,利用上面的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));%顯示正在讀取的影象 J1 = flipdim(image,2);%翻轉 filename = strcat(image_name,'-1.jpg'
); im_pathfile=fullfile(file_path,filename); imwrite(J1,im_pathfile); J2 = imadjust(image, [0.3,1], []);%對比度增強 filename = strcat(image_name,'-2.jpg'); im_pathfile=fullfile(file_path,filename); imwrite(J2,im_pathfile); J3 = imnoise(image,'gaussian',0.1,0.02);%對影象加入高斯噪聲 filename = strcat(image_name,'-3.jpg'); im_pathfile=fullfile(file_path,filename); imwrite(J3,im_pathfile); [R C] = size(image);%平移 se = translate(strel(1),[0 round(R/1.5)]); J4 = imdilate(image,se); filename = strcat(image_name,'-4.jpg'); im_pathfile=fullfile(file_path,filename); imwrite(J4,im_pathfile); end end end