MATLIB 多個子文件夾批量創造測試文件

maindir='Z:\\ ';
writepath='Z:\\ ';
%fp = fopen(maindir,'wt');
subdir =dir( maindir );   % 先確定子文件夾
for i = 3 : length( subdir )
    if( isequal( subdir( i ).name, '.' ) || ...
        isequal( subdir( i ).name, '..' ) || ...
        ~subdir( i ).isdir )   % 如果不是目錄跳過
        continue;
    end
    subdirpath = fullfile( maindir, subdir( i ).name, '*.bmp' );
    images = dir( subdirpath );   % 在這個子文件夾下找後綴爲jpg的文件
    % 遍歷每張圖片
    for j = 3 :length( images )
        imagepath = fullfile( maindir, subdir( i ).name, images( j ).name  );
        img = imread( imagepath ); 
        subplot(3,3,1)
        imshow(img)
        title('original');




        b=imrotate(img,3,'nearest');
        b = imresize(b, [32 18]);
        subplot(3,3,2)
        imshow(b)
        title('rotate 5');


        c=imrotate(img,-3,'nearest');
        c = imresize(c, [32 18]);
        subplot(3,3,3)
        imshow(c)
        title('rotate -5');




       SE1=strel('ball',2,2);
       erode=imerode(c,SE1,'same'); %erode
       erode = imresize(erode, [32 18]);
       subplot(3,3,4)
       imshow(erode);
       title('erode');


       SE2 = strel('disk', 1);
       dilate=imdilate(c,SE2,'same'); %dilate
       dilate = imresize(dilate, [32 18]);
       subplot(3,3,5)
       imshow(dilate)
       title('dilate');


       V=0.008;
       Noisy=imnoise(c,'gaussian',0,V);
       subplot(3,3,6)
       imshow(Noisy)
       title('gaussian noise');


       salt=imnoise(c,'salt & pepper',0.02); %加入椒鹽躁聲
       subplot(3,3,7)
       imshow(salt)
       title('salt & pepper noise');


      % fprintf(fp, '%d \n', dex);
   nameimg= [images( j ).name(1:end-4),'_0.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(b,imagepath);
   
   nameimg= [images( j ).name(1:end-4),'_1.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(c,imagepath);
   
     nameimg= [images( j ).name(1:end-4),'_2.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(erode,imagepath);
   
     nameimg= [images( j ).name(1:end-4),'_3.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(dilate,imagepath);
   
     nameimg= [images( j ).name(1:end-4),'_4.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(Noisy,imagepath);
   
     nameimg= [images( j ).name(1:end-4),'_5.bmp'];
   imagepath = fullfile( writepath, subdir( i ).name,nameimg);
   imwrite(salt,imagepath);
   
    end
end
fclose(fp);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章