MATLIB 多个子文件夹批量创造测试文件

本文介绍了一种使用MATLAB进行批量图像处理的方法,包括旋转、缩放、侵蚀、膨胀及添加高斯噪声等操作,并展示了如何自动处理指定路径下的所有.bmp格式图像文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

### 使用 MATLAB 批量处理文件夹中的文件 为了实现批量处理文件夹中的文件,在 MATLAB 中可以利用 `dir` 函数来获取指定路径下的所有子目录以及这些目录内的文件列表。通过循环遍历每一个找到的文件执行所需的操作,比如读取数据、修改内容或是保存新版本的数据。 下面是一个简单的脚本例子展示如何访问并操作级嵌套文件夹里的.mat 文件: ```matlab function batchProcessFolders(rootFolderPath) % 获取根文件夹下所有的子文件夹名 d = dir(fullfile(rootFolderPath, '*')); d = {d([d.isdir]).name}'; for i = 1:length(d) subfolderPath = fullfile(rootFolderPath, d{i}); % 跳过当前和上级两个特殊项 if ~strcmp(d{i}, '.') && ~strcmp(d{i}, '..') disp(['Processing folder: ', subfolderPath]); % 对于每个子文件夹再次调用函数本身递归查找更深层的内容 batchProcessFiles(subfolderPath); end end end function batchProcessFiles(folderPath) % 查找该文件夹内所有 .mat 结尾的文件 files = dir(fullfile(folderPath, '*.mat')); for k = 1:length(files) filePath = fullfile(folderPath, files(k).name); data = load(filePath); % 加载 mat 文件 % 这里放置具体要做的处理逻辑... processedData = doSomethingWith(data); saveProcessedFile(processedData, filePath); end end function result = doSomethingWith(inputData) % 定义具体的转换规则或计算方法 result = inputData; % 假设不做任何改变直接返回原输入作为示例 end function saveProcessedFile(dataToSave, originalFilePath) [~, name, ext] = fileparts(originalFilePath); newFileName = [name '_processed' ext]; save(fullfile(fileparts(originalFilePath), newFileName), 'dataToSave'); end ``` 此代码片段展示了怎样构建一个能够深入到任意深度层次结构中去定位目标类型的文件,并对其进行统一加工的方式[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值