I'm trying to save a sequence of images generated in MATLAB individually
Show older comments
I asked a question yesterday about using background subtraction on a sequence of images using one frame to subtract from. I got the correct code for this as shown below. It turns out I do need to save all the subtracted frames individually, since I need to then go on to use imfindcircles to identify pillars in the sequence of images. I used the suggested addition to the code below using subtractionImage{k} instead, and tried to use imwrite() to save them to a folder, however the error message said since they were in a cell they could not. I also tried using imwrite() in a loop for each image, however the message said I might not have permission to write into the chosen folder. Firstly, is there a different way to save them individually, despite the fact they're in a cell? Or is there another way to save them not in a cell? Or is there a way to put the cell direclty into another code in order to apply imfindicircles to the sequence. Thanks.
folder = fullfile('C:\folder1\folder2');
filePattern = fullfile(folder, '*.jpg');
fileList = dir(filePattern);
numberOfFiles = numel(fileList);
subtractionFileName = 'subtract.jpg'; % or fullfile(folder, 'subtract.jpg') if it's in the same folder.
if ~isfile(subtractionFileName)
errorMessage = sprintf('Subtraction image not found:\n%s', subtractionFileName);
uiwait(errordlg(errorMessage));
return;
end
backgroundImage = imread(subtractionFileName);
for k = 1:numberOfFiles
baseFileName = fileList(k).name;
if contains(baseFileName, 'subtract', 'IgnoreCase',true)
continue;
end
fullFileName = fullfile(fileList(k).folder, baseFileName);
fprintf(1, 'Now reading %s/n', fullFileName);
thisImage = imread(fullFileName);
% Do the subtraction. Use subtractionImage{k} if you need to save them all for some reason
% and you have enough memory to save them all.
subtractionImage = thisImage - backgroundImage;
% Display the image.
imshow(subtractionImage, []);
caption = sprintf('Image #%d of %d : %s', k, numberOfFiles, baseFileName);
title(caption, 'Interpreter','none');
drawnow; % force it to update the screen immediately.
end
uiwait(helpdlg('Done!'));
Accepted Answer
More Answers (0)
Categories
Find more on Object Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!