Metal每日分享,基于色温调整白平衡滤镜效果

2022-12-19  本文已影响0人  弹吉他的少年

本案例的目的是理解如何用Metal实现基于色温调整白平衡效果滤镜,主要就是消除或减轻日光下偏蓝和白炽灯下偏黄,简单讲把应该是白色的调成白色或接近白色,不使其严重偏色;


Demo

实操代码

// 白平衡滤镜
let filter = C7WhiteBalance.init(temperature: 4000, tint: -200)

// 方案1:
ImageView.image = try? BoxxIO(element: originImage, filters: [filter, filter2, filter3]).output()

// 方案2:
ImageView.image = originImage.filtering(filter, filter2, filter3)

// 方案3:
ImageView.image = originImage ->> filter ->> filter2 ->> filter3

效果对比图

temperature: 4000, tint: -200 temperature: 4000, tint: 0 temperature: 4000, tint: 200
temperature: 7000, tint: -200 temperature: 7000, tint: 0 temperature: 7000, tint: 100

实现原理

这款滤镜采用并行计算编码器设计.compute(kernel: "C7WhiteBalance"),参数因子[temperature < 5000 ? 0.0004 * (temperature - 5000) : 0.00006 * (temperature - 5000), tint / 100]

对外开放参数

/// 白平衡
public struct C7WhiteBalance: C7FilterProtocol {
    
    public static let range: ParameterRange<Float, Self> = .init(min: 4000, max: 7000, value: 5000)
    
    /// The tint to adjust the image by. A value of -200 is very green and 200 is very pink.
    public var tint: Float = 0
    /// The temperature to adjust the image by, in ºK. A value of 4000 is very cool and 7000 very warm.
    /// Note that the scale between 4000 and 5000 is nearly as visually significant as that between 5000 and 7000.
    public var temperature: Float = range.value
    
    public var modifier: Modifier {
        return .compute(kernel: "C7WhiteBalance")
    }
    
    public var factors: [Float] {
        return [
            temperature < 5000 ? 0.0004 * (temperature - 5000) : 0.00006 * (temperature - 5000),
            tint / 100
        ]
    }
    
    public init(temperature: Float = range.value, tint: Float = 0) {
        self.temperature = temperature
        self.tint = tint
    }
}

将图像从RGB空间转换到YIQ空间的rgb值,控制蓝色值处于(-0.5226...0.5226)区间之间,再将YIQ空间颜色转换为RGB空间颜色rgb,对比暖色常量warm获取到新的rgb,最后混色原色和新的rgb和图像色温得到最终的像素rgb;

kernel void C7WhiteBalance(texture2d<half, access::write> outputTexture [[texture(0)]],
                           texture2d<half, access::read> inputTexture [[texture(1)]],
                           constant float *temperature [[buffer(0)]],
                           constant float *tint [[buffer(1)]],
                           uint2 grid [[thread_position_in_grid]]) {
    const half4 inColor = inputTexture.read(grid);
    
    const half3x3 RGBtoYIQ = half3x3({0.299, 0.587, 0.114}, {0.596, -0.274, -0.322}, {0.212, -0.523, 0.311});
    const half3x3 YIQtoRGB = half3x3({1.000, 0.956, 0.621}, {1.000, -0.272, -0.647}, {1.000, -1.105, 1.702});
    
    half3 yiq = RGBtoYIQ * inColor.rgb;
    yiq.b = clamp(yiq.b + half(*tint) * 0.5226 * 0.1, -0.5226, 0.5226);
    const half3 rgb = YIQtoRGB * yiq;
    
    const half3 warm = half3(0.93, 0.54, 0.0);
    const half r = rgb.r < 0.5 ? (2.0 * rgb.r * warm.r) : (1.0 - 2.0 * (1.0 - rgb.r) * (1.0 - warm.r));
    const half g = rgb.g < 0.5 ? (2.0 * rgb.g * warm.g) : (1.0 - 2.0 * (1.0 - rgb.g) * (1.0 - warm.g));
    const half b = rgb.b < 0.5 ? (2.0 * rgb.b * warm.b) : (1.0 - 2.0 * (1.0 - rgb.b) * (1.0 - warm.b));
    
    const half4 outColor = half4(mix(rgb, half3(r, g, b), half(*temperature)), inColor.a);
    outputTexture.write(outColor, grid);
}

色温曲线

先看看在不同光源下呈现出来的颜色如下图:


流程原理:

Harbeth功能清单

最后

✌️.

上一篇 下一篇

猜你喜欢

热点阅读