Metal每日分享,颜色转换滤镜效果

简介: 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

1.png

1.png

1.png

gbra grba rbga

1.png

1.png

1.png


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地址

喜欢的老板们可以点个星🌟,谢谢各位老板!!✌️.

相关文章
|
11月前
|
存储 机器学习/深度学习 编解码
阿里云服务器计算型c9i实例测评:实例性能、适用场景与最新收费标准和活动价格参考
阿里云计算型c9i实例作为第九代云服务器代表,采用CIPU架构与英特尔至强6代处理器,计算性能提升20%,存储I/O及网络能力显著增强。适配机器学习推理、数据分析、视频编码等计算密集型场景,提供2核4G至192核384G的丰富规格,支持按量付费与包年包月模式。2026年新用户可享年付6.4折优惠,叠加优惠券后成本进一步降低。本文详述其技术架构、适用场景、规格参数、收费标准及选购策略,以供参考。
1868 0
|
9月前
|
存储 弹性计算 人工智能
阿里云服务器通用型g9i实例怎么样?性能、收费标准与2核8G、4核16G、8核32G最新活动价格
阿里云第九代企业级ECS通用型g9i实例,凭借阿里云全新CIPU架构与英特尔至强6处理器,实现算力显著提升,提供稳定可预期的高性能输出,适用于多场景。g9i实例提供2核8G到192核768G的广泛配置,支持高性能存储与网络I/O,满足企业核心业务需求。其计费模式灵活,包括按量付费与包年包月,后者提供长期订阅优惠。相比其他实例,g9i在性能、安全性及生态兼容性上优势显著,是企业核心生产系统的理想算力底座。
|
9月前
|
人工智能 自然语言处理 监控
【养龙虾指南】OpenClaw是什么?能做什么?怎么部署?
“养龙虾”是开发者对开源AI智能体框架OpenClaw的趣味称呼——因其Logo为红小龙虾,“Claw”喻指“动手能力”。它非聊天机器人,而是为大模型(如Qwen、DeepSeek)赋予操作电脑能力的本地优先执行网关,支持文件管理、办公自动化、代码辅助等场景。阿里云提供一键部署,安全可控,助你轻松拥有专属“赛博龙虾”。
2149 2
|
9月前
|
安全 数据挖掘 API
如何在不依赖在线API的情况下,批量、快速地查询海量IP的归属地?
面对百万至千万级IP查询需求,依赖在线API易致延迟、不稳定与数据泄露。本地离线方案将IP库部署于内网,毫秒级批量解析,零调用费、高安全、强扩展,支持Python/Java/Go多语言,适配风控、画像、审计等核心场景。
478 1
|
9月前
|
Java 双11 Nacos
《淘宝双11同款:基于 Sentinel 的微服务流量防卫兵实战》
本文深度解析阿里双11同款微服务防护利器——Sentinel,涵盖流量控制、熔断降级、系统自适应保护及Nacos动态规则等核心实战,助你构建高并发、高可用的韧性架构。(239字)
|
11月前
|
监控 数据可视化 前端开发
紧急Bug处理:流程、四阶段控制法及工具方法
本文系统阐述了紧急Bug处理的核心原则、四级分类标准(P0-P3)及四阶段标准化流程(响应、诊断、执行、复盘),强调“控制影响优先于完美修复”。配套工具链涵盖告警聚合、协作指挥、诊断分析与知识沉淀,并提供自动化脚本示例,提升团队应急响应效率与系统稳定性。
|
人工智能 自动驾驶 5G
5G频谱规划:资源优化与共享策略
【10月更文挑战第27天】
1249 4
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
1209 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
机器学习/深度学习 自然语言处理 监控
CNN的应用场景
【10月更文挑战第23天】CNN的应用场景
1568 3