11
$\begingroup$

Is it possible to remove static objects/pixels from a sequence of images?

Lets say I have a black square in the middle of my white picture (white background) and a black circle is orbiting around it. The result would be that my images will only contain the moving black circle from frame to frame and that the static black square would be colored white and though "removed".

My example would be something like this:

Manipulate[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}]

Which gives this image sequence

enter image description here

My first thoughts were going to something like that "put a circle around a cell"-thing with which one could identify not moving cells by looking at the static positions of the targeted cells positions. But I don't have enough expertise in that to manage this problem by myself right now.

Another thing that I came up now was the possibility to create a table with all images of my sequence, turn the images into tables with ImageData and calculate the Mean of each pixels RGB value which then gives me the information if the pixel was black or white all time or if it wasn't. But that explodes my RAM. Maybe there is a better solution?

$\endgroup$
  • 2
    $\begingroup$ Sounds like [en.wikipedia.org/wiki/Background_subtraction], but I don't think MMA has a built-in function for that. $\endgroup$ – Niki Estner Jul 10 '15 at 10:52
  • $\begingroup$ What should be displayed instead of the static objects? $\endgroup$ – shrx Jul 10 '15 at 11:09
  • $\begingroup$ This would be easier if you would post a few images... $\endgroup$ – Niki Estner Jul 10 '15 at 11:33
  • $\begingroup$ Can you guarantee that the objects will be non-overlapping frame to frame ? $\endgroup$ – image_doctor Jul 10 '15 at 12:04
  • $\begingroup$ No, unfortunately they can overlap. $\endgroup$ – pH13 - Yet another Philipp Jul 10 '15 at 12:06
6
$\begingroup$

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.

$\endgroup$
9
$\begingroup$

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]]

enter image description here

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]]

enter image description here

You can then simply use ImageSubtract to subtract the background:

GraphicsRow[
 MapThread[Function[{pixel, bg}, If[pixel == bg, White, pixel]], 
    ImageData /@ {#, background}, 2] & /@ frames]

enter image description here

$\endgroup$
3
$\begingroup$

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]]]}];

removed background

For more difficult scenes you might want to use some thresholding, smoothing, noise-reducing or other image processing functions in the creation of masks.

$\endgroup$
3
$\begingroup$

If your background is constant then this will suffice:

With[{bg = ImageAdd[ImageSubtract[frames[[1]], frames[[2]]], frames[[2]]]}, 
 ColorNegate@ImageSubtract[bg, #] & /@ frames]
$\endgroup$
3
$\begingroup$

With

imgs = Table[
   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/10}];

simply

ListAnimate@(tab = 
   Table[ColorNegate@
     ImageSubtract[Binarize@ColorNegate@imgs[[i]], 
      Binarize@ColorNegate@imgs[[i + 1]]], {i, 1, Length@imgs - 1}])

enter image description here

Head@tab[[i]]==Image; if one wants Graphics, then Graphics @@@ tab.

$\endgroup$
2
$\begingroup$

With the frames being

frames = Binarize /@ (Graphics[{White, Rectangle[{-2, -2}, {2, 2}], 
        Black, Rectangle[{-0.5, -0.5}], Disk[#, 0.1]}, 
       Background -> White] &) /@ CirclePoints[1.5, 150][[;; 10]];

ImageAssemble@frames

In

One can just add up all frames, as White (1) + Black (0) will become White (1) and White (1) + White (1) will also become White (1).

staticCN = ColorNegate @ ImageAdd[frames];

ImageAssemble[ImageAdd[#, staticCN] & /@ frames]

Out

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.