参考:http://blog.csdn.net/guoxiaojie_415/article/details/21317323;http://blog.csdn.net/qikaihuting/article/details/70244416
本篇博文仅针对于循环读取指定文件夹下的图片文件,不涉及深度和层度遍历。
Function函数
function [subdirpath] = RangTraversal(strPath) maindir = strPath; //strPath是string类型的字符串 subdir = dir(maindir); // subdirpath = cell(1, length(subdir)); for j = 1 : length( subdir ) if (~( isequal( subdir(j).name, ‘.‘ )|| isequal( subdir(j).name, ‘..‘))) %|| ~subdir(j).isdir subdirpath{j} = fullfile(maindir, subdir(j).name); end end
调用RangTraversal函数
subdirpath = RangTraversal( ‘../sampleImages‘ ); filename = cell(1, length(subdirpath)); path_ab = cell(1, length(subdirpath)); suffix = {‘.jpg‘, ‘.bmp‘, ‘.tiff‘, ‘.png‘}; for i = 1 : length(subdirpath) if(subdirpath{i} ~= 0) [path_ab{i}, filename{i}] = fileparts(subdirpath{i}); rgb = imread(subdirpath{i}); gray = rgb2gray(rgb); for j = 1 : length(suffix) path_wr = strcat(‘../results/savedimages/‘, [filename{i}, suffix{j}]); imwrite(gray, path_wr); end end end
这段代码的作用是循环读取文件夹内的图片,然后将其灰度化,将灰度化后的图片存入新的文件夹。
时间: 2024-11-14 17:37:23