MATLAB Answers

for loop is not iterating through images

2 views (last 30 days)
Ean Hendrickson
Ean Hendrickson on 10 May 2019
Edited: KSSV on 10 May 2019
I have a folder with a bunch of .png images. I then have a for loop where I want to read in all of .png images, store them in a variable, in this case y, perform a wavelet decomposition, extract the coefficients, then do some basic math to calculate the percentage of energy then finally add the calculated energy to a matrix. The problem is that the for loop is not iterating through the images in the folder, it gets stuck only reading in the first image then performing the calculations and storing the same number in the matrix. So I end up with the same number throughout my matrix. If anyone can help me fix this that would be greatly appreciated.
n = numel(flist); % find size of folder rund "flist = dir(fullfile(pwd, 'healthy', '*.png'));" outside of folder first in command window. 'healthy' is name of the folder containg .png images
energy = zeros(n,1); %create empty matrix
for i = 1:(n)
y = imread(sprintf('Layer_%d.png',n)); %read in file
y2 = imresize(y, [256 256]); %resize file
[C,S] = wavedec2(y2,7,'db4'); %get wavelet coefficients
[H4,V4,D4] = detcoef2('all',C,S,7); %reasigning horizontal, veritcal and diagonal coefficents
A4 = appcoef2(C,S,'db4',7); %reasigning approximation coefficients
A4_abs = abs(A4); H4_abs = abs(H4); V4_abs = abs(V4); D4_abs = abs(D4); %absolute values
A4_sq = A4_abs.^2; H4_sq = H4_abs.^2;V4_sq = V4_abs.^2; D4_sq = D4_abs.^2; %square values
A4_sum = sum(A4_sq(:)); H4_sum = sum(H4_sq(:)); %sum all values & store new variable
V4_sum = sum(V4_sq(:)); D4_sum = sum(D4_sq(:)); %sum all values & store new variable
energy(i) = 100*(A4_sum/(H4_sum+V4_sum+D4_sum+A4_sum)); %calculate energy & store in matrix
end

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 10 May 2019
Edited: KSSV on 10 May 2019
img = dir('*png') ;
N = length(img) ; % edited
for i = 1:N
I = imread(img(i).name)
% do what you want
end

More Answers (0)


Translated by