Substract background from image(with restoring alpha)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

I have a background image, and image on that background.
blend mode was Plus.(at least same method)
i need to restore that image, with alpha channel.
Background:
Image
Image+BG:
Image

i tryed that:
imconvert res4.png white.png ( -clone 0 -clone 1 -compose difference -composite -threshold 0 ) -delete 1 -compose copy_opacity -composite tmp.png
but got bad result.
Image

so...how i can do that?
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Re: Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

or may with help of the same image on another background?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

If a + X = b, how do we find X? We rearrange, and get X = b - a.

If the original command was ...

Code: Select all

convert white.png X.png -compose Plus -composite res4.png
... then we can find X.png like this:

Code: Select all

convert res4.png white.png -compose MinusSrc -composite X.png
We can check the result:

Code: Select all

convert white.png X.png -compose Plus -composite res4_new.png

compae -metric RMSE res4.png res4_new.png NULL:
The result is zero, so the X.png is correct.
snibgo's IM pages: im.snibgo.com
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Re: Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

the result is following:
Image
no alpha channel.

i think i was wrong with Plus method.
image had alpha, and was painted on background(white.png), target image(res4.png) don't have alpha anymore.
and i need restore X.png, which had alpha.

i only can add new pair of background and target image, if it needed.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

X.png is fully opaque.

res4.png and white.png have no transparency. So X.png has no need for any transparency.

But you can give it transparency, if you want. For example:

Code: Select all

convert X.png -transparent Black Xt.png
All the pixels that were black are now fully transparent. The result may look ugly, but it is accurate.

When X can have transparency, there are many possible images that give the correct result. An almost infinite number.

And if the original operation could have been Plus or Over or anything else, there are many many more possible solutions.
phoenixcorp13 wrote:image had alpha, ...
Okay. Do you know what the alpha was?
snibgo's IM pages: im.snibgo.com
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Re: Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

result will be like last picture in first post, but only black, not green.
can i somehow restore alpha by using two pairs of BG and RES to recieve truly X.png?
or better forget about it and search another way to reach the goal?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

I don't know what your goal is.
snibgo's IM pages: im.snibgo.com
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Re: Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

my goal is to recieve that yellow thingy separately with alpha channel.
or at least find out that it is not possible.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

Please be more specific. I have provided a method. What is wrong with it? How doesn't it satisfy your goal?
snibgo's IM pages: im.snibgo.com
phoenixcorp13
Posts: 6
Joined: 2016-04-05T05:54:32-07:00
Authentication code: 1151

Re: Substract background from image(with restoring alpha)

Post by phoenixcorp13 »

Image
that result with your command.
i need to get rid of any black background, by restoring half transparent pixels.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

There is no black in Xt.png. I turned black transparent.

There are very dark pixels, and others that are not so dark, and some that are exactly yellow.

There is a virtually infinite number of ways of adjusting the alpha of Xt.png, and adjusting the colours correspondingly, so that when it is added we get res4.png.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

We want one of the images X.png that has certain colour and alpha values such that, when it is added to white.png, makes res4.png.

There are a virtually infinite number of solutions. However, if we knew or guessed the colour values of X.png, this determines the alpha values. Alternatively, if we knew or guessed the alpha values, this would determine the colour values.

Let's try the second method. We want the darker pixels to have more transparency. So let's suppose that the alpha values are the auto-levelled brightness of res4.png. (I choose "Brightness" to ensure that any fully-saturated pixels will have 100% opacity.)

All code is Windows BAT syntax.

Code: Select all

%IM%convert res4.png -grayscale Brightness -auto-level res_alpha.png
Image
That is the alpha channel.

Now we have guessed the alpha values, we can calculate the colour values:

Code: Select all

%IM%convert ^
  res4.png white.png ^
  -compose MinusSrc -composite ^
  res_alpha.png ^
  -compose DivideSrc -composite ^
  Xt2c.png
Image
Those are the colours.

We need to combine the colour and alpha:

Code: Select all

%IM%convert ^
  Xt2c.png ^
  res_alpha.png ^
  -compose CopyOpacity -composite ^
  Xt2ca.png
Image
This is the extracted "yellow blob".

As before, we can check the result:

Code: Select all

%IM%convert Xt2ca.png white.png -compose Plus -composite res_X2.png
Image

Code: Select all

%IM%compare -metric RMSE res_X2.png res4.png NULL:
The difference is 0.000456, ie 0.04%, which is almost identical to the required result. The error is probably integer quantization.

I stress, this is just one of a virtually infinite number of possible solutions.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

phoenixcorp13 wrote:i think i was wrong with Plus method.
Just for fun, let's suppose the method is "Over".

We assume the alpha is as in my previous post.

Code: Select all

%IM%convert ^
  res4.png ^
  ( white.png ^
    ( res_alpha.png -negate ) ^
    -compose Multiply -composite ^
  ) ^
  -compose MinusSrc -composite ^
  res_alpha.png ^
  -compose DivideSrc -composite ^
  Xt2c_ov.png
Image
Those are the colours.

Code: Select all

%IM%convert ^
  Xt2c_ov.png ^
  res_alpha.png ^
  -compose CopyOpacity -composite ^
  Xt2ca_ov.png
Image
Those are the colours with alpha. The colour varies; it isn't a constant yellow.

Comparing this "-compose Over" white.png with res4.png, we get the same difference as before, 0.04%, an almost exact match.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Substract background from image(with restoring alpha)

Post by snibgo »

Above, I showed how to find the colours of the extracted "yellow blob" assuming a certain compose method and alpha arrangement. The yellow blob has an average colour #ffc512.

If we work in Q8 integer, each pixel has 256 possible values for alpha. If the image has 200x200 pixels, then we have 256*200*200 = 10,240,000 possible solutions.

We could work in the opposite direction and assume a certain colour for each pixel, then calculate the alpha. However, there are 256*256*256 possible colours at each pixel. Most of these can't be generated from any alpha value. Thus, most colour values cannot create an exact solution. For example, if X.png was entirely blue #00f, then an Over composite on white.png which is entirely #660 cannot create the yellows seen in res4.png, no matter what alpha values are used.

But we can try the second method, assuming a constant colour of #ffc512. We will find the most correct alpha for each pixel.

Code: Select all

%IM%convert ^
  ( res4.png ^
    white.png ^
    -compose MinusSrc -composite ^
  ) ^
  ( res4.png -fill #ffc512 -colorize 100 ^
    white.png ^
    -compose MinusSrc -composite ^
  ) ^
  -compose DivideSrc -composite ^
  +depth ^
  +write m.png ^
  -grayscale Average ^
  ma.png
Where m.png has different values in the colour channels, the solution requires different alphas for the three colour channels. We need a single alpha, so we take the average as a compromise.

Code: Select all

%IM%convert ^
  res4.png -fill #ffc512 -colorize 100 ^
  ma.png ^
  -compose CopyOpacity -composite ^
  res_mac.png
Image
This is the yellow blob, a constant colour #ffc512.

We test the result:

Code: Select all

%IM%convert ^
  res_mac.png ^
  white.png ^
  -compose DstOver -composite ^
  res_macr.png
Image
This looks similar to the desired res4.png.

How close is it?

Code: Select all

%IM%compare -metric RMSE res_macr.png res4.png NULL:

1808.86 (0.0276014)
It is 2.7% away from an exact match, which isn't bad.
snibgo's IM pages: im.snibgo.com
Post Reply