how to finish first part then second part in for loop?

Hello all,
I have a for loop, and there are two parts. I want the part to complete then excute the second part in one for loop.
Basically I get all images files in one folder (many subfolders). I want to delete the images which the pixel values are less than 40 as well as all the images in the subfolder.
The script I wrote cannot delete all images which were in the same subfolder.
Thanks for your input!
S = dir(fullfile(path,'*.'));
N = setdiff({S([S.isdir]).name},{});
for ii = 1:numel(N)
T = dir(fullfile(path,N{ii},'*.tif'));
C = {T(~[T.isdir]).name};
for jj = 1:numel(C)
F = fullfile(path,N{ii},C{jj});
%first part
I=imread(F);
imwrite(I,['Z:\Cell_' num2str(ii) '_' num2str(jj) '.TIF']);
pixelvalue=size(I);
k=nnz(pixelvalue>=40);
%second part
if k==1 || k==0
delete(['Z:\Cell_' num2str(ii) '*.TIF']);
end
end
end

 Risposta accettata

Try this:
% Process a sequence of files.
filePattern = fullfile(pwd, '*.tif');
imds = imageDatastore(filePattern) % Create an image Datastore
% Get all filenames into one cell array. Filenames have the complete path (folder prepended).
allFileNames = imds.Files;
numFiles = numel(imds.Files);
recycle on % Deleted files go to recycle bin.
for k = 1 : numFiles
% Get this file name.
fullFileName = allFileNames{k};
if ~isfile(fullFileName)
% File no longer exists. We deleted it on a prior iteration.
continue;
end
% If it gets to here the file still exists.
fprintf('Processing #%d of %d : "%s"\n', k, numFiles, fullFileName);
% Now do something with fullFileName, such as passing it to imread.
grayImage = imread(fullFileName);
if size(grayImage, 3) == 3
grayImage = rgb2gray(grayImage);
end
% See if any pixels are less than 40.
if any(grayImage(:) < 40)
% Yes, this image has at least one pixel darker than 40 gray levels.
% Get the folder it's in.
[subFolder, baseFileName, ext] = fileparts(fullFileName);
filePattern = fullfile(subFolder, '*.tif');
imdsSubFolder = imageDatastore(filePattern); % Create an image Datastore
allFileNamesSub = imds.Files;
numFilesSub = numel(imds.Files);
% Delete ALL the TIFF image files in this folder.
for k2 = 1 : numFilesSub
thisFileName = allFileNamesSub{k2};
% Ask user for confirmation before deleting the image.
promptMessage = sprintf('Do you want to delete "%s"?', thisFileName);
titleBarCaption = 'Delete?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes, Delete it', 'No', 'Quit', 'Yes, Delete it');
if contains(buttonText, 'Yes', 'IgnoreCase', true)
% They said to delete it.
fprintf(' Deleting %s\n', thisFileName);
delete(allFileNamesSub{k2});
elseif contains(buttonText, 'Quit', 'IgnoreCase', true)
% Exit program.
return; % or break; if you want to exit the loop over this folder.
end
end
end
end

7 Commenti

Hi, many thanks for your input.
The sprict you attached could not working.
There are only subfolders in my folder (images all in the subfloders), which results in the error "Folder does not have any files or is empty." Is there a way I can firstly access to all subfolder?
On the other hand, if I get all images in one folder, which will result in delete all images in this folder.
Sorry. That's weird. I didn't know imageDatastore would throw an error if there were not any files in the top level folder. So a workaround is to use dir. I tested this with a folder that had no TIF image files in the top level folder but had 4 subfolders each with a tiff image in it, and it worked.
% Process a sequence of files.
% Define the top level folder:
folder = 'C:\Users\YunWei\Matlab\work\Tests\tests';
% Get a list of all TIFF files in that folder and subfolders of it.
filePattern = fullfile(folder, '**\*.tif');
fileList = dir(filePattern) % Create an image Datastore
% Get all filenames into one cell array. Filenames have the complete path (folder prepended).
allFileNames = fullfile({fileList.folder}, {fileList.name});
numFiles = numel(allFileNames)
recycle on % Deleted files go to recycle bin.
for k = 1 : numFiles
% Get this file name.
fullFileName = allFileNames{k};
if ~isfile(fullFileName)
% File no longer exists. We deleted it on a prior iteration.
continue;
end
% If it gets to here the file still exists.
fprintf('Processing #%d of %d : "%s"\n', k, numFiles, fullFileName);
% Now do something with fullFileName, such as passing it to imread.
grayImage = imread(fullFileName);
if size(grayImage, 3) == 3
grayImage = rgb2gray(grayImage);
end
% See if any pixels are less than 40.
if any(grayImage(:) < 40)
% Yes, this image has at least one pixel darker than 40 gray levels.
% Get the folder it's in.
[subFolder, baseFileName, ext] = fileparts(fullFileName);
filePattern = fullfile(subFolder, '*.tif');
filesInSubFolder = dir(filePattern);
numFilesSub = numel(filesInSubFolder);
% Delete ALL the TIFF image files in this folder.
for k2 = 1 : numFilesSub
thisFileName = fullfile(filesInSubFolder(k2).folder, filesInSubFolder(k2).name);
% Ask user for confirmation before deleting the image.
promptMessage = sprintf('Do you want to delete "%s"?', thisFileName);
titleBarCaption = 'Delete?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Yes, Delete it', 'No', 'Quit', 'Yes, Delete it');
if contains(buttonText, 'Yes', 'IgnoreCase', true)
% They said to delete it.
fprintf(' Deleting %s\n', thisFileName);
% delete(allFileNamesSub{k2});
elseif contains(buttonText, 'Quit', 'IgnoreCase', true)
% Exit program.
return; % or break; if you want to exit the loop over this folder.
end
end
end
end
Hi, thanks a lot.
Could you also update the 'delete(allFileNamesSub{k2})'?
Because there is not acutally deleting in the script.
Sorry for asking this. I am still new to this area.
Your original script had a line deleting files. If you don't actually need to delete files, just remove the lines that do so. Removing those lines of code should be trivial, why don't you give it a go?
Yes because when I was testing it for you I didn't want to actually delete any of my files so I commented out the delete line
% delete(allFileNamesSub{k2});
So why don't you uncomment it? Then it will recycle your files.
I tried to uncomment it.
Just that you changed some structure in the second script so there was no allFlieNamesSub anymore, and that's why l was asking.
Now I have found out what to replace it. The script works pretty well.
Thanks a lot.
Yeah, it's quite possible a number of things were changed in the second script, that's why I gave the full script (since I wasn't changing just a line or two). I'm glad it's working and thanks for accepting it. 🙂

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by