Green screening is a process whereby a range of colors in an image (typically highly saturated greens) is used to determine an alpha channel for the image, for purposes of compositing.
Two steps:
First, you are to write code to generate an alpha channel mask for an image based upon image color information. Second, you will complete an image compositing program that uses the alpha channel information of a foreground image and computes the over operator.
1.Masking
first convert the color data to HSV and next select the pixels to mask based on those which have a hue near the value 120 (green on a 360 degree scale),set the a=0;
/∗ Code to convert RGB to HSV
Adapted from Foley, Van Dam, Feiner, and Hughes, pg. 592.
Input RGB color primary values : r, g, and b on scale 0 − 255
Output HSV colors : h on scale 0−360, s and v on scale 0−1
∗/
#define maximum(x, y, z) ((x) > (y)? ((x) > (z)? (x) : (z)) : ((y) > (z)? (y) : (z)))
#define minimum(x, y, z) ((x) < (y)? ((x) < (z)? (x) : (z)) : ((y) < (z)? (y) : (z))) void RGBtoHSV(int r, int g, int b, double &h, double &s, double &v){
double red, green, blue;
double max, min, delta;
red=r/255.0;
green=g/255.0;
blue=b/255.0; /∗**r,g,b to 0−1 scale**∗/
max = maximum(red, green, blue);
min = minimum(red, green, blue);
v = max; /∗ value is maximum of r, g, b ∗/
if (max == 0) {
/∗ saturation and hue 0 if value is 0 ∗/
s = 0;
h = 0;
} else {
s = (max - min) / max;
delta = max - min;
if (delta == 0)
{
h = 0;
}
else {
if (red == max) {
h = (green - blue) / delta;
/∗ saturation is color purity on scale 0 − 1 ∗/
/∗ hue doesn’t matter if saturation is 0 ∗/
/∗ otherwise , determine hue on scale 0 − 360 ∗/
}
else if (green == max)
{
h = 2.0 + (blue - red) / delta;
}
else { /∗ (blue == max) ∗/
h = 4.0 + (red - green) / delta;
}
h = h * 60.0;
if(h < 0) {
h = h + 360.0;
}
}
}
}
After masking:
2.Composite
Over Operator Defined
Each color channel C treated separated:
CP = aCA+(1-a)CB (Linear interpolation of colors)
if background is transparent
CP = aCA+(1-a)(aB)CB
Before:CP = aACA + (1-aA)aBCB
After: cP = cA + (1-aA)cB
Same for alpha: aP = aA + (1-aA)aB
Interpretation: a premultiplied (r,g,b,a) means that
over with Premultiplied Colors.
the real color is (R,G,B) = (ar, ar, ag)
The correct way to composite is:
**Interpolate foreground and background and then
composite**
PS:the value that you read(read_image OpenImageIO) from image is Premultiplied value.