Cant change directory between loops
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Joe Beard
le 14 Mar 2022
Modifié(e) : Walter Roberson
le 15 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
0 commentaires
Réponse acceptée
Walter Roberson
le 14 Mar 2022
Modifié(e) : Walter Roberson
le 15 Mar 2022
img = fullfile(dataset(i).folder,dataset(i).name);
Plus de réponses (2)
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.
0 commentaires
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
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
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!