opengl进行绿色屏幕抠图

在进行一个AR项目,需要进行抠图,所以就用了一些抠图的方法,先展示一下抠图效果,如下:

抠图之前
抠图之后

蓝色背景
RGB值 : 0, 71, 187
CMYK : 90, 68, 0, 0

绿色背景

RGB值: 0, 177, 64

CMYK : 81, 0, 92, 0

RGB值转换成(0-1)之间,需要除以255

我们直接在fragment shader中进行抠图。

抠图算法方案一:

keying_color = vec3(0.0, 0.6941177, 0.2509804) 是需要扣掉的rgb值,计算2个像素的色差值,色差值在一定阈值智能进行抠图:

#extension GL_OES_EGL_image_external: require
precision mediump  float;
varying vec2 varTexCoordAlpha;
uniform samplerExternalOES texture;
void main() {
    vec4 texColor = texture2D(texture, vec2(varTexCoordAlpha.x,1.0-varTexCoordAlpha.y));
    vec3 keying_color = vec3(0.0, 0.6941177, 0.2509804);
    float thresh = 0.6; // [0, 1.732]
    float slope = 0.50; // [0, 1]
    vec3 input_color = texColor.rgb;
    float d = abs(length(abs(keying_color.rgb - input_color.rgb)));
    float edge0 = thresh * (1.0 - slope);
    float alpha = smoothstep(edge0, thresh, d);
    gl_FragColor = vec4(input_color, alpha);
}

抠图算法方案二:

这个算法是查询到的,具体的可以看一下参考文档进行研究一下。

#extension GL_OES_EGL_image_external: require
precision mediump  float;
varying vec2 varTexCoordAlpha;
uniform samplerExternalOES texture;
void main() {
    vec4 texColor = texture2D(texture, vec2(varTexCoordAlpha.x,1.0-varTexCoordAlpha.y));
    float rbAverage = texColor.r * 0.5 + texColor.b * 0.5;
    float gDelta = texColor.g - rbAverage;
    texColor.a = 1.0 - smoothstep(0.0, 0.15, gDelta);
    texColor.a = texColor.a * texColor.a * texColor.a;
    gl_FragColor = texColor;
}

参考文档:

https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php

https://stackoverflow.com/questions/17444734/opengl-es-green-screen-but-i-want-to-use-black

https://www.programmersought.com/article/83204897429/

https://forum.unity.com/threads/chroma-key-in-unity-5.359119/?_ga=2.221513246.746374513.1602656168-624735101.1598963093

https://github.com/erikbuck/RealTimeGreenScreen

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XR风云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值