Problem in looping. contour command is not working on all the images, only the values from last image is printing. I want the C values from all the images

1 view (last 30 days)
hi,
I am using the code below to read the images from the folder, and in work space I can see it is reading all the images but then I want to apply the contour command as shown below to all the images and extract C from all the images but the code is only extracting the information from the last image. Can you guys guide me what needs to be done in the code below.
Code:
srcFile=dir('D:\ImageAnalysis\NewAnalysis\Images\*.png')
for i=1:length(srcFile)
filename=strcat('D:\ImageAnalysis\NewAnalysis\Images\',srcFile(i).name);
I=imread(filename);
[C,h] = imcontour(I,2);
end

Accepted Answer

Ameer Hamza
Ameer Hamza on 17 Nov 2020
Edited: Ameer Hamza on 17 Nov 2020
Because you are overwriting these variable in each iteration. Create a cell array
srcFile=dir('D:\ImageAnalysis\NewAnalysis\Images\*.png')
C = cell(1,numel(srcFile));
h = cell(1,numel(srcFile));
for i=1:length(srcFile)
filename=strcat('D:\ImageAnalysis\NewAnalysis\Images\',srcFile(i).name);
I=imread(filename);
[C{i},h{i}] = imcontour(I,2);
end
Read about cell arrays here: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html. You can access values in cell arrays using brace indexing
C{1}
C{2}
..
C{end}

More Answers (0)

Community Treasure Hunt

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

Start Hunting!