Hi, I'm having trouble with processing several images and saving it to the same file. I have 120 images of the image attached (.tif format and 16bit) and I applied a mask on the white semicircles using a code. My problem is that I'm not sure how to apply the mask to each image and save them in the same file. When I used the following code, it only applied the mask to one image and produced 120 copies of it. Is there a way use the for loop to apply the mask for each processed image and then save each image?
% Specify the folder where the files are
myFolder = 'C:\Users\ellizafeisol\Desktop\DATA\UNF2501';% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder) errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder); uiwait(warndlg(errorMessage)); myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0 % User clicked Cancel
return; endend% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern required.
theFiles = dir(filePattern);numberofImages = length(theFiles);for k = 1 : numberofImages baseFileName = theFiles(k).name; fullFileName = fullfile(theFiles(k).folder, baseFileName); % Process all images in the file
imageArray = imread(fullFileName); %Gray Image
g = double(imageArray); grayImages = mat2gray(g); % Threshold Values
imagesToThreshold = g; startingLowThreshold = 0; startingHighThreshold = 34500; % Binarize the image.
binaryImage = (imagesToThreshold > startingLowThreshold) & (imagesToThreshold < startingHighThreshold); binaryImage = imfill(binaryImage, 'holes'); mask = bwareafilt(binaryImage, 4); %Mask the images
Output=grayImages.*mask; drawnow; % Force display to update immediately.
for k= 1 : numberofImages %k be numbers of images in the folder
h = Output; imwrite(h,sprintf('MASK_0%d.tif',k)); % will create mask1, mask2,...
end end
Best Answer