I am trying to use background subtraction on a set of images in a folder

49 visualizaciones (últimos 30 días)
I am new to the image processing side of MATLAB. The images I am trying to subtract the background from are a set of frames from a video. I have successfully subtracted the background from one using a different photo I constructed in the paint app (the area of interest is highlighted in white, leaving just that area after the subtraction (this is quite important). Since the camera is stationary, I need to use the same singular photo to carry out background subtraction on all 95 frames. I managed to somewhat do this using the code below, however the images were put into a cell, therefore I could not save the images, see them individually, or watch them as a video. Is this possible to do automatically, subtracting the background from 95 different images using the same one image to subtract from, since repeating this process in paint 95 times would take a long time? Or is there a way to remove the images from the cell where I can save them, see them individually and watch them together as a video? Thanks
  1 comentario
Andrew Palmer
Andrew Palmer el 9 de Ag. de 2022
Sorry, the code that gave me the subtracted images in a cell is below since I forgot to attach.
filePattern=fullfile('C:\', 'folder1', 'folder2','*.jpg');
images1=dir(filePattern);
image2=imread('subtract.jpg');
for k = 1:95
baseFileName=images1(k).name;
fullFileName=fullfile(images1(k).folder, baseFileName);
fprintf(1, 'now reading %s/n', fullFileName);
end
imageArray=imread(fullFileName);
imshow(imageArray)
for k = 2:95
subtracted{k-1}=image2-imageArray;
end

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 9 de Ag. de 2022
Try this:
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!'));
  3 comentarios
Andrew Palmer
Andrew Palmer el 10 de Ag. de 2022
Hi. Turns out I do need to save them all individually, since I need to then go on to use imfindcircles to identify pillars in the sequence of images. I put in the suggested code above, 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. 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 again
Image Analyst
Image Analyst el 10 de Ag. de 2022
You can save the images one at a time with imwrite() or else you can save the whole cell array into a .mat file with save() and then recall it with load().

Iniciar sesión para comentar.

Más respuestas (0)

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