Substract background from image(with restoring alpha)
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
Substract background from image(with restoring alpha)
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+BG:
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.
so...how i can do that?
blend mode was Plus.(at least same method)
i need to restore that image, with alpha channel.
Background:
Image+BG:
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.
so...how i can do that?
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
Re: Substract background from image(with restoring alpha)
or may with help of the same image on another background?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
If a + X = b, how do we find X? We rearrange, and get X = b - a.
If the original command was ...
... then we can find X.png like this:
We can check the result:
The result is zero, so the X.png is correct.
If the original command was ...
Code: Select all
convert white.png X.png -compose Plus -composite res4.png
Code: Select all
convert res4.png white.png -compose MinusSrc -composite X.png
Code: Select all
convert white.png X.png -compose Plus -composite res4_new.png
compae -metric RMSE res4.png res4_new.png NULL:
snibgo's IM pages: im.snibgo.com
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
Re: Substract background from image(with restoring alpha)
the result is following:
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
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:
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.
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
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.
Okay. Do you know what the alpha was?phoenixcorp13 wrote:image had alpha, ...
snibgo's IM pages: im.snibgo.com
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
Re: Substract background from image(with restoring alpha)
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?
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?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
I don't know what your goal is.
snibgo's IM pages: im.snibgo.com
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
Re: Substract background from image(with restoring alpha)
my goal is to recieve that yellow thingy separately with alpha channel.
or at least find out that it is not possible.
or at least find out that it is not possible.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
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
-
- Posts: 6
- Joined: 2016-04-05T05:54:32-07:00
- Authentication code: 1151
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
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.
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
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.
That is the alpha channel.
Now we have guessed the alpha values, we can calculate the colour values:
Those are the colours.
We need to combine the colour and alpha:
This is the extracted "yellow blob".
As before, we can check the result:
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.
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
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
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
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
Code: Select all
%IM%compare -metric RMSE res_X2.png res4.png NULL:
I stress, this is just one of a virtually infinite number of possible solutions.
snibgo's IM pages: im.snibgo.com
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
Just for fun, let's suppose the method is "Over".phoenixcorp13 wrote:i think i was wrong with Plus method.
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
Those are the colours.
Code: Select all
%IM%convert ^
Xt2c_ov.png ^
res_alpha.png ^
-compose CopyOpacity -composite ^
Xt2ca_ov.png
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Substract background from image(with restoring alpha)
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.
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.
This is the yellow blob, a constant colour #ffc512.
We test the result:
This looks similar to the desired res4.png.
How close is it?
It is 2.7% away from an exact match, which isn't bad.
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
Code: Select all
%IM%convert ^
res4.png -fill #ffc512 -colorize 100 ^
ma.png ^
-compose CopyOpacity -composite ^
res_mac.png
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
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)
snibgo's IM pages: im.snibgo.com