how to finish first part then second part in for loop?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Image Analyst
le 15 Fév 2023
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 commentaires
Image Analyst
le 17 Fév 2023
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. 🙂
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Image Sequences and Batch Processing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!