error in processing multiple images in a loop code

2 次查看(过去 30 天)
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 个评论
Neo
Neo 2022-7-6
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!!

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2022-7-6
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 个评论
Image Analyst
Image Analyst 2022-7-6
编辑:Image Analyst 2022-7-6
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)

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2022-7-6
I assume this was meant:
[~, filename, ~] = fileparts(filenames);
% ^^^^^ instead of fileinfo
  1 个评论
Neo
Neo 2022-7-6
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by