I am a beginner-level programmer. Kindly help me to count the number of images from multiple folders at once
3 ビュー (過去 30 日間)
古いコメントを表示
All the folders in the below path contains images. I want to count number of images in each folder and need to get the output in .csv format.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/859260/image.png)
4 件のコメント
採用された回答
Walter Roberson
2022 年 1 月 11 日
dinfo = dir(fullfile('*', '*.jpg'));
folders = {dinfo.folder};
[~, basefolder] = fileparts(folders);
[G, folder_name] = findgroups(basefolder);
image_count = accumarray(G(:), 1);
results = table(folder_name, image_count);
writetable(results, 'OutputFileName.csv');
2 件のコメント
Walter Roberson
2022 年 1 月 11 日
Unfortunately that makes the code more difficult. You should consider upgrading.
if isunix()
%for demonstration purposes only, position us to some images
projectdir = fullfile(matlabroot, 'toolbox', 'images');
cd(projectdir);
end
dinfo = dir('*');
dinfo(ismember({dinfo.name}, {'.', '..'})) = []; %remove . and ..
folder_name = {dinfo.name}.';
num_folder = length(folder_name);
image_count = cell(num_folder, 1);
for K = 1 : num_folder
finfo = dir( fullfile(folder_name{K}, '*.jpg') );
image_count{K} = length(finfo);
end
results = [folder_name, image_count];
results
その他の回答 (1 件)
Image Analyst
2022 年 1 月 11 日
編集済み: Image Analyst
2022 年 1 月 11 日
Do you want the count broken down by how many are in each folder? Or do you just want to count the numbre in each folder and give a grand total, like this:
topLevelFolder = pwd; % or 'C:\my folder' or whatever you want.
filePattern = fullfile(topLevelFolder, '**\*.jpg');
% Get list of all JPG files in the top level folder and all subfolders below that.
fileList = dir(filePattern)
% Count them and put the count into the numFiles variable.
numFiles = length(fileList)
** may not work in your antique version though. Not sure when the ** was introduced.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Convert Image Type についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!