「这是我参加2022初次更文应战的第4天,活动概况检查:2022初次更文应战」
本案例的意图是了解如何用Metal实现像素色彩转化滤镜,通过对像素色彩的不同读取方式获取到相应像素色彩,灰度图移除场景中除了黑白灰以外一切的色彩,让整个图画灰度化;
Demo
- HarbethDemo地址
- iDay每日共享文档地址
实操代码
// 转成灰度图滤镜
let filter = C7ColorConvert(with: .gray)
// 计划1:
let dest = BoxxIO.init(element: originImage, filter: filter)
ImageView.image = try? dest.output()
dest.filters.forEach {
NSLog("%@", "\($0.parameterDescription)")
}
// 计划2:
ImageView.image = try? originImage.make(filter: filter)
// 计划3:
ImageView.image = originImage ->> filter
实现原理
- 过滤器
这款滤镜采用并行计算编码器规划.compute(kernel: type.rawValue)
/// 色彩通道`RGB`位置转化
public struct C7ColorConvert: C7FilterProtocol {
public enum ColorType: String, CaseIterable {
case invert = "C7ColorInvert"
case gray = "C7Color2Gray"
case bgra = "C7Color2BGRA"
case brga = "C7Color2BRGA"
case gbra = "C7Color2GBRA"
case grba = "C7Color2GRBA"
case rbga = "C7Color2RBGA"
}
private let type: ColorType
public var modifier: Modifier {
return .compute(kernel: type.rawValue)
}
public init(with type: ColorType) {
self.type = type
}
}
- 着色器
取出像素rgb值,然后根据对应像素色彩;灰度图则是取一切的色彩重量,将它们加权或均匀;
// 色彩反转,1 - rgb
kernel void C7ColorInvert(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(1.0h - inColor.rgb, inColor.a);
outputTexture.write(outColor, grid);
}
// 转灰度图
kernel void C7Color2Gray(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half3 kRec709Luma = half3(0.2126, 0.7152, 0.0722);
const half gray = dot(inColor.rgb, kRec709Luma);
const half4 outColor = half4(half3(gray), 1.0h);
outputTexture.write(outColor, grid);
}
kernel void C7Color2BGRA(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(inColor.bgr, inColor.a);
outputTexture.write(outColor, grid);
}
kernel void C7Color2BRGA(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(inColor.brg, inColor.a);
outputTexture.write(outColor, grid);
}
kernel void C7Color2GBRA(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(inColor.gbr, inColor.a);
outputTexture.write(outColor, grid);
}
kernel void C7Color2GRBA(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(inColor.grb, inColor.a);
outputTexture.write(outColor, grid);
}
kernel void C7Color2RBGA(texture2d<half, access::write> outputTexture [[texture(0)]],
texture2d<half, access::read> inputTexture [[texture(1)]],
uint2 grid [[thread_position_in_grid]]) {
const half4 inColor = inputTexture.read(grid);
const half4 outColor(inColor.rbg, inColor.a);
outputTexture.write(outColor, grid);
}
- 权值法
const half3 kRec709Luma = half3(0.2126, 0.7152, 0.0722);
const half gray = dot(inColor.rgb, kRec709Luma);
const half4 outColor = half4(half3(gray), 1.0h);
- 均匀值法
const float color = (inColor.r + inColor.g + inColor.b) / 3.0;
const half4 outColor = half4(color, color, color, 1.0h);
总结:
一般因为人眼对不同色彩的敏感度不一样,所以三种色彩值的权重不一样,一般来说绿色最高,赤色其次,蓝色最低,最合理的取值分别为Wr = 30%,Wg = 59%,Wb = 11%,所以权值法相对作用更好一点。
对照图
invert | bgra | brga |
---|---|---|
gbra | grba | rbga |
Harbeth功用清单
- 支撑ios体系和macOS体系
- 支撑运算符函数式操作
- 支撑多种形式数据源 UIImage, CIImage, CGImage, CMSampleBuffer, CVPixelBuffer.
- 支撑快速规划滤镜
- 支撑合并多种滤镜作用
- 支撑输出源的快速扩展
- 支撑相机采集特效
- 支撑视频增加滤镜特效
- 支撑矩阵卷积
- 支撑运用体系 MetalPerformanceShaders.
- 支撑兼容 CoreImage.
- 滤镜部分大致分为以下几个模块:
- Blend:图画融合技术
- Blur:模糊作用
- Pixel:图画的基本像素色彩处理
- Effect:作用处理
- Lookup:查找表过滤器
- Matrix: 矩阵卷积滤波器
- Shape:图画形状大小相关
- Visual: 视觉动态特效
- MPS: 体系 MetalPerformanceShaders.
最后
- 关于色彩转化滤镜介绍与规划到此为止吧。
- 慢慢再弥补其他相关滤镜,喜欢就给我点个星吧。
-
滤镜Demo地址,现在包含
100+
种滤镜,同时也支撑CoreImage混合运用。 - 再附上一个开发加速库KJCategoriesDemo地址
- 再附上一个网络根底库RxNetworksDemo地址
- 喜欢的老板们可以点个星,谢谢各位老板!!!
✌️.