当前位置:网站首页>Batch renaming of images by MATLAB

Batch renaming of images by MATLAB

2022-06-23 23:52:00 HH~LL

During the experiment , Picture naming is not standardized , It may bring us a lot of trouble in subsequent data processing , Because the general program reads the image from the folder first according to the image name , If it is 1000 Zhang image , from 1 Start naming , namely 1——1000, Read order is not 1 2 3…, It is 1 10 11 12 …100 Read first 1 At the beginning Read again 2 At the beginning , It is equivalent to reading out of order . Therefore, it is necessary to name images in a standard way .
If the total number of pictures is greater than 1000, You can modify the inner layer for The conditions in the loop and 0 The number of

file_path1=' Image folders to be named in batch ';
file_path2=' Name the folder where the pictures are saved ';
img_path_list = dir(strcat(file_path1,'*.png'));% Get all... In this folder jpg Format image   
img_num = length(img_path_list);% Total number of captured images  
if img_num > 0 % There are images that meet the conditions   
    for j = 1:img_num % Read the images one by one   
        image_name_old = img_path_list(j).name;%  Image name 
        if j<10
            image_name_new = strcat('000',num2str(j),'.png');
            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),'.png');
            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),'.png');
            image = imread(strcat(file_path1,image_name_old));
            imwrite(image,strcat(file_path2,image_name_new))
        end
    end 
end 

原网站

版权声明
本文为[HH~LL]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232109118195.html