error in processing multiple images in a loop code

1 visualización (últimos 30 días)
Neo
Neo el 6 de Jul. de 2022
Comentada: Neo el 6 de Jul. de 2022
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 comentarios
Neo
Neo el 6 de Jul. de 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!!

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jul. de 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 comentarios
Image Analyst
Image Analyst el 6 de Jul. de 2022
Editada: Image Analyst el 6 de Jul. de 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 el 6 de Jul. de 2022
Hallejuah!
It works!!! Thank you Image Analyst!

Iniciar sesión para comentar.

Más respuestas (1)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Computer Vision with Simulink en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by