OpenGL-----Image Green Screening

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 0255 
Output HSV colors : h on scale 0360, s and v on scale 01/
#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 01 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 01/
                        /∗ hue doesn’t matter if saturation is 0/
                        /∗ otherwise , determine hue on scale 0360/
} 
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;
   } 
 }
}
}

Original image
After masking:
Alphamask

2.Composite

Alpha Blending

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.
After Composite
composite

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值