I'm trying to save a sequence of images generated in MATLAB individually

3 visualizaciones (últimos 30 días)
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!'));

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Ag. de 2022
"Firstly, is there a different way to save them individually, despite the fact they're in a cell?"
No. Some of the image writing calls support writing 3 or 4 dimensional arrays of data, but I cannot think of any that support writing a cell array within a single call. Oh yes, one of movie writing calls supports writing a struct array... but not a cell array.
"Or is there another way to save them not in a cell?"
The calls that support writing multiple images in the same call all write the multiple images into the same file. Animated gifs, movie files, dicom images, some calls that end up writing tiff images, multiband images.
"Or is there a way to put the cell direclty into another code in order to apply imfindicircles to the sequence."
No, imfindcircles works strictly one image at a time.
"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."
writing one image at a time in a loop (or equivalent such as cellfun()) is a completely valid approach. But if you do not have permission to write into the folder that is going to affect any file saving method. You need to either write to somewhere you have write access to, or you need to get granted permission to write to that folder.
"Or is there a way to put the cell direclty into another code in order to apply imfindicircles to the sequence."
That suggests that you might not have a hard requirement to use different files. In that case, you can save() the cell as a mat file if you need to save a file at all, and when it comes time to process, load the cell and loop over the entries.
for k = 1:numel(SubtractionImage)
thisImage = SubtractionImage{k};
result = imfindcircles(thisImage);
end

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by