Browse a database of images matlab

조회 수: 1 (최근 30일)
zakaria siad
zakaria siad 2018년 3월 15일
답변: Abhishek 2025년 4월 3일
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