Cant change directory between loops

7 vues (au cours des 30 derniers jours)
Joe Beard
Joe Beard le 14 Mar 2022
I am trying to change files between loops and read in the images from each, however only the images from the first file are ever read in. Apologies if this has been asked before, I couldnt find anything similar when looking for a solution.
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset=dsn(~ismember({dsn.name},{'.','..'}));
for i = 1:length(dataset)
img = dataset(i).name;
imgr = imread(img);
imshow(imgr)
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities(i, j) = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Mar 2022
Modifié(e) : Walter Roberson le 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
  2 commentaires
Stephen23
Stephen23 le 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
^^^
Walter Roberson
Walter Roberson le 15 Mar 2022
@Stephen You are right, I fixed it now.

Connectez-vous pour commenter.

Plus de réponses (2)

Jon
Jon le 14 Mar 2022
Modifié(e) : Jon le 14 Mar 2022
Looking at your loops the logic seems basically OK but I can't run it to see exactly what happens as I don't have your image directories.
I would suspect that you are not really getting back a full list of all of the files in each directory you are looping through. So in particular you should check that the results of
dsn = dir(ds);
are what you expect. If not look one line earlier and see whether the string ds is really what you want. It looks like it evaluates to something like, for example with j = 3
ds = "J:\Images\BCS 3"
Is that the name of a folder full of images?
Note when providing code you can use the code button in the Answers toolbar to format it nicely.
Also when you do your directory search you can use wildcard characters, like J:\Images\myImages\*.jpg to just get jpegs or whatever.Then you don't have to get rid of the . and .. in your results.

yanqi liu
yanqi liu le 15 Mar 2022
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset=dsn(~ismember({dsn.name},{'.','..'}));
for i = 1:length(dataset)
if dataset(i).isdir == 1
continue;
end
img = fullfile(dataset(i).folder,dataset(i).name);
imgr = imread(img);
figure(1); clf; imshow(imgr, [])
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities{i, j} = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end
  2 commentaires
Walter Roberson
Walter Roberson le 15 Mar 2022
Modifié(e) : Walter Roberson le 15 Mar 2022
for j = 1:5
ds = sprintf("J:\\Images\\BCS %d", j);
dsn = dir(ds);
dataset = dsn(~[dsn.isfolder]); %also deals with . and ..
for i = 1:length(dataset)
img = fullfile(dataset(i).folder,dataset(i).name);
imgr = imread(img);
figure(1); clf; imshow(imgr, [])
stats = regionprops(imgr, 'all');
allPerimeters = [stats.Perimeter];
allAreas = [stats.Area];
allCircularities{i, j} = allPerimeters .^ 2 ./ (4 * pi* allAreas);
end
end
Stephen23
Stephen23 le 15 Mar 2022
dataset = dsn(~[dsn.isfolder]);
% ^^^

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by