フィルターのクリア

error in processing multiple images in a loop code

1 回表示 (過去 30 日間)
Neo
Neo 2022 年 7 月 6 日
コメント済み: Neo 2022 年 7 月 6 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 7 月 6 日
dinfo = dir('*.jpg');
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)
Neo
Neo 2022 年 7 月 6 日
Hallejuah!
It works!!! Thank you Image Analyst!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeComputer Vision with Simulink についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by