필터 지우기
필터 지우기

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
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 CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by