How can I create a background subtraction loop?

2 views (last 30 days)
Hello, I am very new to programming and Matlab, and I've been facing an issue. I guess it's pretty simple, but I can not get out of the first steps.
I have a folder with a sequence of .tif images. I want to rename them numerically (from 1 to XXXXX). That I could do, and it seems that it's working properly with the code below:
a ='C:\Users\\Test Script Matlab';
A =dir( fullfile(a, '*.tif') );
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( '%05d.tif', iFile ) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end
The next step would be a background substraction, in which the background would be the previous image of the sequence.
For example:
At the beginning, the image number 1 (image00001) is the background image. The output image would be image00002-image00001.
The second output would be image00003-image00002.
The third output image would be image 00004-image00003...and so on...
Then, at the end, I want to save all image outputs in a different folder.
I wonder if you can help me with that?
Thank you.
  2 Comments
Matt J
Matt J on 19 Aug 2022
How many images? How big are they? Are they grayscale images? If not, what should be the output format?
DGM
DGM on 20 Aug 2022
Are the images actually registered well enough, or will further registration be required?
Are you sure that arithmetic subtraction is actually the correct method for background feature removal in your case? It could be, but it's often not. Without knowing what the images look like, I can't know if it is.
If I might try reading between the lines, I might make some assumptions based on the problem presentation. Assuming that arithmetic subtraction is appropriate, that means that the image progression only adds positive (light) image features between frames (a light-on-dark polarity). Similarly, the idea that the subsequent frame is always the "background" seems to imply that (light) features are being progressively added between frames.
Either way, there are various ways to do background removal, but I can't know what's appropriate here. I have a rudimentary background extraction tool that works for videos and image sequences, but it takes some setup effort and it doesn't necessarily work well on everything. It might be overkill if the problem is truly as simple as it sounds.
For either myself or others to provide informed assistance, an example of the imageset would be very helpful. If the imageset is too large to attach, a reduced-resolution subset of the frames should work. If they're TIFF files, just stick em in a zip file so that the editor will let you attach them. If you wish to host them offsite as a workaround for the filesize limit, feel free.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 20 Aug 2022
See my attached video processing demo where I subtract prior frames from the current frame.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!