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

8 ビュー (過去 30 日間)
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 件のコメント
Andrew Palmer
Andrew Palmer 2022 年 8 月 9 日
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');
for k = 1:95
fullFileName=fullfile(images1(k).folder, baseFileName);
fprintf(1, 'now reading %s/n', fullFileName);
for k = 2:95



Image Analyst
Image Analyst 2022 年 8 月 9 日
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);
backgroundImage = imread(subtractionFileName);
for k = 1:numberOfFiles
baseFileName = fileList(k).name;
if contains(baseFileName, 'subtract', 'IgnoreCase',true)
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.
  3 件のコメント
Image Analyst
Image Analyst 2022 年 8 月 10 日
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().


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by