필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Yunwei
Yunwei 2023년 2월 15일
댓글: Image Analyst 2023년 2월 17일
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

채택된 답변

Image Analyst
Image Analyst 2023년 2월 15일
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
Yunwei
Yunwei 2023년 2월 17일
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.
Image Analyst
Image Analyst 2023년 2월 17일
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. 🙂

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by