Cant change directory between loops

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2022-3-14
编辑:Walter Roberson 2022-3-15
img = fullfile(dataset(i).folder,dataset(i).name);

更多回答(2 个)

Jon
Jon 2022-3-14
编辑:Jon 2022-3-14
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 2022-3-15
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 个评论
Walter Roberson
Walter Roberson 2022-3-15
编辑:Walter Roberson 2022-3-15
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by