Browse a database of images matlab

1 次查看(过去 30 天)
i have folder contain 250 images i went to divided to 10 folders each one container 25 image

回答(1 个)

Abhishek
Abhishek 2025-4-3
To efficiently distribute images from a single directory into multiple subfolders, you can use the ‘copyfile’ method inside a loop. The script below copies images from a source directory into multiple destination folders, with each folder containing a specified number of images. I have tried it on MATLAB R2024b:
sourceDir = 'C:\Users\ SampleImages';
destDir = 'C:\Users \Output';
imageFiles = dir(fullfile(sourceDir, '*.png'));
imagesPerFolder = 25;
numFolders = ceil(length(imageFiles) / imagesPerFolder);
for folderIdx = 1:numFolders
folderName = fullfile(destDir, sprintf('Folder_%02d', folderIdx));
if ~exist(folderName, 'dir')
mkdir(folderName);
end
startIdx = (folderIdx - 1) * imagesPerFolder + 1;
endIdx = min(folderIdx * imagesPerFolder, length(imageFiles));
for imgIdx = startIdx:endIdx
sourceFile = fullfile(sourceDir, imageFiles(imgIdx).name);
destFile = fullfile(folderName, imageFiles(imgIdx).name);
copyfile(sourceFile, destFile);
end
end
disp('Images have been copied into folders successfully.');
Here is the step-by-step process:
  • The code first calculates the number of folders to be created and then creates those folders.
  • It then distributes the images among these folders. The variable ‘imagesPerFolder’ defines the number of images to be placed in each destination folder.
  • The ‘copyfile’ method is used to copy the files from the source to the destination folder. Alternatively, you can use ‘movefile’ to move the files instead of copying them.
Please refer to the documentations for more details:
I hope this solves your problem. Thanks!

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by