I am a beginner-level programmer. Kindly help me to count the number of images from multiple folders at once

조회 수: 6 (최근 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.
  댓글 수: 4
KSSV
KSSV 2022년 1월 11일
Do all the folders have only images? Or some other files too are present?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
Akhil Narayanan
Akhil Narayanan 2022년 1월 11일
First of all , many thanks. But I am getting the error
Reference to non-existent field 'folder'.
I am using MATLAB 2013a.
Walter Roberson
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
results = 11×2 cell array
{'Halide.rights'} {[ 0]} {'builtins' } {[ 0]} {'colorspaces' } {[ 0]} {'deep' } {[ 0]} {'icons' } {[ 0]} {'images' } {[ 0]} {'imdata' } {[38]} {'imdemos' } {[ 0]} {'imuitools' } {[ 0]} {'iptformats' } {[ 0]} {'iptutils' } {[ 0]}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
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.

카테고리

Help CenterFile Exchange에서 Marine and Underwater Vehicles에 대해 자세히 알아보기

태그

제품


릴리스

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by