我有一个包含大约 1000 张图像的目录。在我的 matlab 代码中,我必须按名称顺序处理这些图像( img1.jpg 、 img2.jpg 等)。有没有办法按顺序从图像目录中读取,以便处理 img1.jpg,然后处理 img2.jpg 等等?先感谢您。
imgFilesDir =dir('pics/*.jpg');
for n=1:length(imgFilesDir)
%read Target image and convert into single
rgb2= im2single(imread(strcat('pics/',imgFilesDir(n).name)));
I2 = rgb2gray(rgb2);
end
使用正则表达式提取数字部分,从字符串转换为数字,按数字排序,然后将该顺序应用于字符串:
imgFilesDirName = {imgFilesDir.name};
[~, ind] = sort(cellfun(@str2double, regexp(imgFilesDirName,'\d+(?=\..*)','match')));
imgFilesDirSorted = imgFilesDir(ind); %// sorted struct
imgFilesDirNameSorted = imgFilesDirName(ind); %// sorted names
假定数字部分是文件扩展名之前的一位或多位数字。
例如,给定
imgFilesDirName = {'imag1.jpg', 'imag10.jpg', 'imag11.jpg', 'img2.jpg', 'img20.jpg'};
you get
imgFilesDirNameSorted =
'imag1.jpg' 'img2.jpg' 'imag10.jpg' 'imag11.jpg' 'img20.jpg'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)