Transparency related calculation I can't figure out

  • 5 Replies
  • 3466 Views
*

Offline Nifflas

  • 1532
  • 61
    • View Profile
Transparency related calculation I can't figure out
« on: March 07, 2009, 02:27:03 »
Hi! Is anyone here really good with maths? Basically, I have:

* This image:


* ...and this image:


They're the same image, but one has a black background, and the other one has a white one. Assuming I wanted to replace the background with an alpha channel, I can compare the difference between the first and second image. This will produce this image:


...so now I know what to put in the alpha channel. The problem is to figure out what to put in the RGB channels, it's not as easy as it sounds, because I need the colour of every pixel, unaffected by the black or white background. I understand that it's probably impossible to calculate the exact colour, but a close enough approximisation would work.

The image I'm after would have to look like this:


If I could figure that out (which I have no idea how to do) I'd have enough information to successfully create an image with an alpha channel out of the two source images that doesn't have one.

Something like this:
« Last Edit: March 07, 2009, 02:39:43 by Nifflas »

*

Offline Joozey

  • 122
  • 0
  • It's a clockwork universe.
    • View Profile
Re: Transparency related calculation I can't figure out
« Reply #1 on: March 07, 2009, 02:58:43 »
The resulting background color of a transparant image should be:
result_image_color = (alpha * original_image_color) + ((1 - alpha) * background_color)

Where:
alpha = 0..1
original_image_color = your last image (the value you want)
background_color = white, or black
result_image_color = the result of background + original image (your first or second image)



Thus, if you know the background color, say it's FFFFFF, and the resulting image color is AABBCC:
(alpha * original_image_color) + ((1 - alpha) * FFFFFF) = AABBCC

You know the alpha, that's the normalized color value from your third image. Say it's 0.3
(0.3 * original_image_color) + ((1 - 0.3) * FFFFFF) = AABBCC

Then, uhh:
original_image_color = (AABBCC - ((1 - 0.3) * FFFFFF)) / 0.3

That should be it, I guess.

« Last Edit: March 07, 2009, 03:02:21 by Joozey »
Gears grinding on maximum power, belts slipping to high speed.<br />In name of Her Majesty Victoria, FULL THRUST INTO ARMADA!

*

Offline Evil

  • 1112
  • 1
  • 1723
    • View Profile
Re: Transparency related calculation I can't figure out
« Reply #2 on: March 07, 2009, 10:02:12 »
layer masks.....?

*

Offline LPChip

  • You can only truly help other people by allowing them to fail.
  • 3510
  • 138
  • Excel at the thing you're the best at!
    • View Profile
    • LPChip Interactive
Re: Transparency related calculation I can't figure out
« Reply #3 on: March 07, 2009, 10:35:33 »
layer masks.....?
That would work in an image editor, but I think Nifflas wants to program this feature.

Normally you would proceed like this:
You have 2 steps.

First you draw the light image with a multiply mask, then you draw the dark image with a lighten mask to compensate for too much darkness caused by the earlier mask.

A multiply mask compares the pixel below with the new pixel. It does an average of both pixels (Rvalue + Bvalue + Gvalue)/3 and sees which one of both is higher. The pixel with the lower average will be used. If both averages are the same, the pixel from the new image will be used.

A lighten mas compares the pixel below with the new pixel. The pixel below does include the previous mask. We will only correct the dark pixels here. It also does an average of both pixels and sees which one of both is higher. The pixel with the higher average will be used. If both averages are the same, then use the average of the older image.

I hope that helps. :)


« Last Edit: March 07, 2009, 10:55:04 by LPChip »
on the left, above my avatar.

MODPlug Central Forum
"If I tried to kill you, I'd end up with a big fat hole through my laptop." - Chironex

*

Offline Nifflas

  • 1532
  • 61
    • View Profile
Re: Transparency related calculation I can't figure out
« Reply #4 on: March 07, 2009, 12:05:42 »
Thanks a lot for this help! I've tried these solutions, but either I must have done something wrong, or they didn't produce the result I was after.

I figured out a working solution though, in Photoshop I used "Difference" on image #1 and #2 to produce the alpha chanel, and then layered that above the second image with black background. I used the "color dodge" blending. That produced the correct image (as I expected, the colour of the nearly transparent pixels could only be approximated and didn't turn out perfect).

Now I only need to google for "color dodge", to find it's maths, that won't be a problem! :)

LPChip, I'm not sure I can agree with your "multiply mask" description. Doesn't it just do (a*b)/255, hence the name "Multiply"?
« Last Edit: March 07, 2009, 23:59:10 by Nifflas »

*

Offline Looki

  • 166
  • 7
    • View Profile
Re: Transparency related calculation I can't figure out
« Reply #5 on: March 07, 2009, 13:44:53 »
http://www.nathanm.com/photoshop-blending-math/
#define ChannelBlend_ColorDodge(B,L)  ((uint8)((B == 255) ? B:min(255, ((L << 8 ) / (255 - B)))))

:) Great that you found a solution!