Processing and Saving multiple images to the same file

10 visualizaciones (últimos 30 días)
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;
end
end
% 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

Respuesta aceptada

Raunak Gupta
Raunak Gupta el 6 de Ag. de 2020
Hi Elliza,
From the code I see that you have used the for loop to traverse through the images. Since the first for loop is for traversing, I don’t see the necessity of second for loop. Indeed, what it is doing is changing the value of k to numberofImages and hence when you come back to outer for loop the condition of k<= numberofImages is already is fullfilled and thus your code is only outputting same images numberofImages times. You may change the for loop with something like below.
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.
imwrite(Output,sprintf('MASK_0%d.tif',k)); % will create mask1, mask2,...
end
This will make sure every image is read and mask is calculated.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by