error in processing multiple images in a loop code

2 views (last 30 days)
hello,
a reknowned responder (walter robinson) suggested the following code to process multiple images and count blobs and then sum them together:
dinfo = dir('.jpg');
filenames = {dinfo.name};
numfiles = length(filenames);
count = zeros(numfiles, 1);
for K = 1 : numfiles
MyRGBImage = filenames{K};
imageData = imread(MyRGBImage);
bw = im2bw(imageData);
bw = bwareafilt(bw, [10 inf]);
[~, numcircles] = bwlabel(bw);
circle_count(K) = numcircles;
end
[~, filename, ~] = fileinfo(filenames);
info_table = table(filename, circle_count);
finalcount = sum(circle_count);
disp(info_table)
But i keep getting an error:
Unrecognized function or variable 'fileinfo'.
Error in loopcount (line 13)
[~, filename, ~] = fileinfo(filenames);
I changed it to filepart or imfinfo but then I got:
Error using imfinfo
Too many output arguments.
Error in loopcount (line 13)
[~, filename, ~] = imfinfo(filenames);
so I changed it to fileparts and I got:
Error using table
All table variables must have the same number of rows.
Error in loopcount (line 14)
info_table = table(filename, circle_count);
Can someone please help me figure out what is going on here?
Thank you!!
  3 Comments
Neo
Neo on 6 Jul 2022
Hey Walter!!
Thanks that helped but after I uploaded my two test images (just to try it out), I got the following:
filename circle_count
___________ ____________
{1×29 cell} 1×29 double
It should say the number of blobs in each cell as a sum but it seems to put out the structure of the array instead?
Thanks!!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 6 Jul 2022
Try this. It works fine:
directoryInfo = dir('*.jpg');
allFileNames = {directoryInfo.name};
numfiles = length(allFileNames)
circle_count = zeros(numfiles, 1);
for k = 1 : numfiles
fprintf('Processing file #%d of %d : "%s".\n', k, numfiles, allFileNames{k});
MyRGBImage = allFileNames{k};
imageData = imread(MyRGBImage);
bw = im2bw(imageData); % Binarize the image.
bw = bwareafilt(bw, [10 inf]); % Extract only blobs 10 pixels and larger.
% Count the number of blobs.
[~, numCircles] = bwlabel(bw);
% Save the number of blobs.
circle_count(k) = numCircles;
end
% Stuff arrays into a table variable.
info_table = table(allFileNames(:), circle_count, 'VariableNames', {'FileName', 'CircleCount'});
finalcount = sum(circle_count)
disp(info_table)
  3 Comments
Image Analyst
Image Analyst on 6 Jul 2022
Edited: Image Analyst on 6 Jul 2022
Yeah, because test1.jpg does not exist in your current folder. Why would you change it to a single file when you wanted to do all the files? Put it back to *.jpg or *.jp* if you have both .jpg and .jpeg files.
Try this if you want sequential:
numFiles = 2;
circle_count = zeros(numfiles, 1);
for k = 1 : numfiles
thisFileName = sprintf('Test%d.jpg', k);
if isfile(thisFileName)
fprintf('Processing file #%d of %d : "%s".\n', k, numfiles, thisFileName);
imageData = imread(thisFileName);
bw = im2bw(imageData); % Binarize the image.
bw = bwareafilt(bw, [10 inf]); % Extract only blobs 10 pixels and larger.
% Count the number of blobs.
[~, numCircles] = bwlabel(bw);
% Save the number of blobs.
circle_count(k) = numCircles;
else
warningMessage = sprintf('File not found:\n%s', thisFileName);
fprintf('%s\n', warningMessage);
uiwait(warndlg(warningMessage));
end
end
% Stuff arrays into a table variable.
info_table = table(allFileNames(:), circle_count, 'VariableNames', {'FileName', 'CircleCount'});
finalcount = sum(circle_count)
disp(info_table)
Neo
Neo on 6 Jul 2022
Hallejuah!
It works!!! Thank you Image Analyst!

Sign in to comment.

More Answers (1)

Jan
Jan on 6 Jul 2022
I assume this was meant:
[~, filename, ~] = fileparts(filenames);
% ^^^^^ instead of fileinfo
  1 Comment
Neo
Neo on 6 Jul 2022
yes, so i changed it to:
dinfo = dir('.jpg');
filenames = {dinfo.name};
numfiles = length(filenames);
count = zeros(numfiles, 1);
for K = 1 : numfiles
MyRGBImage = filenames{K};
imageData = imread(MyRGBImage);
bw = im2bw(imageData);
bw = bwareafilt(bw, [10 inf]);
[~, numcircles] = bwlabel(bw);
circle_count(K) = numcircles;
end
[~, filename, ~] = fileparts(filenames);
info_table = table(filename, circle_count);
finalcount = sum(circle_count);
disp(info_table)
I still get an error

Sign in to comment.

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!