Remove static objects from an image sequence
One simple way to get a robust background image even if the dots overlap from one frame to the next is to use the per-pixel median:
frames = Table[
Rasterize@
Graphics[{Opacity[0], Rectangle[{-1.1, -1.1}, {1.1, 1.1}],
Opacity[1], Rectangle[{-.5, -.5}],
Disk[{Cos[p], Sin[p]}, .1]}], {p, 0, Pi/5, Pi/50}];
background = Image[Median[ImageData /@ frames]]
unlike the mean, the median filters out the "moving" objects completely as long as the majority of the pixels in the sequence show the background. Compare the mean:
Image[Mean[ImageData /@ frames]]
You can then simply use ImageSubtract
to subtract the background:
GraphicsRow[
MapThread[Function[{pixel, bg}, If[pixel == bg, White, pixel]],
ImageData /@ {#, background}, 2] & /@ frames]
Essentially you want to find all pixels that are black in all images. I turned the color around so that the goal became to find the pixels that are white in all images. Now 0x0=0, 1x0=0 and 1x1=1, so what I would propose is to multiply all images in the animation with each other. Afterwards I use ColorNegate
to get back your configuration with a white background and black foreground.
img[p_] := Graphics[{
White, Rectangle[{-.5, -.5}],
Disk[{Cos[p], Sin[p]}, .1]
},
Background -> Black,
PlotRange -> {{-1.1, 1.1}, {-1.1, 1.1}}
]
ColorNegate@Fold[ImageMultiply[#, img@#2] &, img[0], Range[0.05, 2 Pi, 0.05]]
Now that we have the background we can use ImageSubtract
to remove it. You mentioned problems with memory which is why I generate each frame only when I need it, so only one image is in the memory at a time. You should know however that this is probably a lot slower than to generate all images first; ImageMultiply
can work on an arbitrary number of images simutaneously and will work much faster if you let it. You should at least consider if you can multiply the images in batches.
Here's an approach that works on your example image sequence:
frames = Table[
Rasterize@
Graphics[{Opacity[0], Rectangle[{-1.1, -1.1}, {1.1, 1.1}],
Opacity[1], Rectangle[{-.5, -.5}],
Disk[{Cos[p], Sin[p]}, .1]}], {p, 0, 2 Pi, Pi/2}];
masks = ImageDifference @@@ Partition[frames, 2, 1];
MapThread[
ColorNegate@ImageMultiply[##] &, {ColorNegate@frames,
Append[masks, masks[[-1]]]}];
For more difficult scenes you might want to use some thresholding, smoothing, noise-reducing or other image processing functions in the creation of masks
.