ios - 带有 GLSL 的 CIKernel 白色像素

标签 ios glsl core-image cifilter cikernel

我在 GLSL 中有一个阈值过滤器的 CIKernal,如下所示:

let thresholdKernel = CIColorKernel(string:
    "kernel vec4 thresholdFilter(__sample pixel, float threshold)" +
    "{                           " +
    "   float luma = (pixel.r * 0.2126) +   " +
    "       (pixel.g * 0.7152) +             " +
    "       (pixel.b * 0.0722);          " +
 
    "   return vec4(step(threshold, luma)); " +
    "}                          "
)”

我想检查 像素 是白色的。 GLSL 中是否有一个简单的命令可以在没有额外计算的情况下执行此操作?

更新**
我想摆脱亮度计算。那么有没有办法在不进行上述亮度计算的情况下检查像素是否为白色?

最佳答案

如果每个树颜色 channel 为 >= 1.0,则像素为“白色” .这可以通过测试颜色 channel 的总和是否为 3.0 来检查。当然首先要确保三个颜色 channel 限制在 1.0:

bool is_white = dot(vec3(1.0), clamp(lightCol.rgb, 0.0, 1.0)) > 2.999;

或者
float white = step(2.999, dot(vec3(1.0), clamp(lightCol.rgb, 0.0, 1.0))); 

在这种情况下 min(vec3(1.0), lightCol.rgb)可以用来代替clamp(lightCol.rgb, 0.0, 1.0)也。

如果众所周知,三个颜色 channel 中的每一个都是<= 1.0 ,则表达式可以简化为:
dot(vec3(1.0), lightCol.rgb) > 2.999

注意,在这种情况下 dot 产品计算:
1.0*lightCol.r + 1.0*lightCol.g + 1.0*lightCol.b

luma可以计算如下:
float luma = dot(vec3(0.2126, 0.7152, 0.0722), lightCol.rgb);

关于ios - 带有 GLSL 的 CIKernel 白色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105716/

相关文章:

ios - 如何向 CIImage 添加文本?

iphone - iOS 示例 "GLPaint"不支持方向纵向、纵向颠倒

ios - iPhone 4 英寸屏幕上的屏幕渲染问题

opengl-es - OpenGL ES 2.0 中的剪裁平面

ios - 与核心图像的色彩平衡

ios - initWithCVPixelBuffer 失败,因为 CVPixelBufferRef 不是非 IOSurface 支持的

ios - 从全局函数中显示和隐藏 StatusBar?

ios - Xcode 8 错误 : this class is not key value coding-compliant for the key enemyTitleLabel

opengl - 在 GLSL 中如何判断事件纹理是否为纹理 id 0?

c++ - GLSL/C++ 中的白色(可能未着色)OBJ