前言:在网上看了一些修改图片名称的代码,发现很多都没有达到预期的结果,所以在这里把我的代码贴出来,仅供大家参考,
也希望帮到大家少走一些弯路。
file_path1='G:\VOC2012dataset\imageDataset\tvmonitor\';
file_path2='G:\VOC2012dataset\train\tvmonitor\';
img_path_list = dir(strcat(file_path1,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name_old = img_path_list(j).name;% 图像名
if j<10
image_name_new = strcat('000',num2str(j),'.jpg');
image = imread(strcat(file_path1,image_name_old));
imwrite(image,strcat(file_path2,image_name_new))
elseif j>=10 && j<100
image_name_new = strcat('00',num2str(j),'.jpg');
image = imread(strcat(file_path1,image_name_old));
imwrite(image,strcat(file_path2,image_name_new))
elseif j>=100
image_name_new = strcat('0',num2str(j),'.jpg');
image = imread(strcat(file_path1,image_name_old));
imwrite(image,strcat(file_path2,image_name_new))
end
end
end
图片以0001,0002,…xxxx格式存储。