[Gimp-developer] Proposal for erasing background from an image



Hi all,


  I'm guessing this is frowned on but I've written this up on my blog here:

https://johnflux.com/2017/03/02/erasing-background-from-an-image/

  It's mostly images and latex equations, so it's much better to read it
there.  I've pasted it below anyway.

Is anyone interested in this?


I have two opaque images -  one with an object and a background, and
another with just the background.

I want to subtract the background from the image so that the alpha blended
result is visually identical, but the foreground is as transparent as
possible.

I'm sure that this must have been, but I couldn't find a single correct way
of doing this!

I asked a developer from the image editor gimp team, and they replied that
the standard way is to create an alpha mask on the front image from the
difference between the two images.  i.e. for each pixel in both layers,
subtract the rgb values, average that difference between the three
channels, and then use that as an alpha.

But this is clearly not correct.  Imagine the foreground has a green piece
of semi-transparent glass against a red background.  Just using an alpha
mask is clearly not going to subtract the background because you need to
actually modify the rgb values in the top layer image to remove all the red.

So what is the correct solution?  Let's do the calculations.

If we have a solution, the for a solid background with a semi-transparent
foreground layer that is alpha blended on top, the final visual color is:

$latex out_{rgb} = src_{rgb} * src_{alpha} + dst_{rgb} \cdot
(1-src_{alpha})$

We want the visual result to be the same, so we know the value of $latex
out_{rgb}$ - that's our original foreground+background image.  And we know
$latex dst_{rgb}$ - that's our background image.  We want to now create a
new foreground image, $latex src_{rgb}$, with the maximum value of $latex
src_{alpha}$.

So to restate this again - I want to know how to change the top layer
$latex src$ so that I can have the maximum possible alpha without changing
the final visual image at all.  I.e. remove as much of the background as
possible from our foreground+background image.

Note that we also have the constraint that for each color channel, that
$latex src_{rgb} \le 1$ since each rgb pixel value is between 0 and 1.  So:

$latex src_{alpha} \le (out_{rgb} - dst_{rgb})/(1-dst_{rgb})$

So:

$latex
src_{alpha} = Min((out_r - dst_r)/(1-dst_r), out_g - dst_g)/(1-dst_g),
out_b - dst_b)/(1-dst_b))\\
src_{rgb} = (dst_{rgb} \cdot (1-src_{alpha}) - out_{rgb})/src_{alpha}
$

Proposal

Add an option for the gimp eraser tool to 'remove layers underneath', which
grabs the rgb value of the layer underneath and applies the formula using
the alpha in the brush as a normal erasure would, but bounding the alpha to
be no more than the equation above, and modifying the rgb values
accordingly.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]