Metal每日分享,颜色转换滤镜效果
本案例的目的是理解如何用Metal实现像素颜色转换滤镜,通过对像素颜色的不同读取方式获取到相应像素颜色,灰度图移除场景中除了黑白灰以外所有的颜色,让整个图像灰度化;
Demo
- HarbethDemo地址
实操代码
// 转成灰度图滤镜
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: ColorTypepublic 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.
最后
- 关于颜色转换滤镜介绍与设计到此为止吧。
- 慢慢再补充其他相关滤镜,喜欢就给我点个星
Metal每日分享,颜色转换滤镜效果相关推荐
- Metal每日分享,自然饱和度滤镜效果
本案例的目的是理解如何用Metal实现自然饱和度效果滤镜,简单讲就是调整图像整体的明亮程度,如调节到较高数值,图像会产生色彩过饱和从而引起图像失真: Demo HarbethDemo地址 iDay每日 ...
- Metal每日分享,虚假颜色混合滤镜效果
本案例的目的是理解如何用Metal实现虚假颜色效果滤镜,使用图像的亮度在两种用户指定的颜色之间进行混合: Demo HarbethDemo地址 实操代码 // 混合颜色 let filter = C7 ...
- Metal每日分享,调整胶片颗粒感滤镜效果
本案例的目的是理解如何用Metal实现调节胶片颗粒感滤镜,通过调整颗粒参数来调整晶粒尺寸来达到颗粒感效果: Demo HarbethDemo地址 实操代码 // 调节胶片颗粒感滤镜 let filte ...
- Metal每日分享,图像阀值素描滤镜效果
本案例的目的是理解如何用Metal实现图像阀值素描滤镜,用于图像阀值素描,形成有噪点的素描: Demo HarbethDemo地址 实操代码 // 用于图像阀值素描,形成有噪点的素描 let filt ...
- Metal每日分享,纯色图片滤镜效果
本案例的目的是理解如何用Metal实现纯色图片效果滤镜,主要就是生成纯色图片: Demo HarbethDemo地址 实操代码 // 纯色滤镜 ImageView.image = C7Color.pu ...
- Metal每日分享,均值模糊滤镜效果
本案例的目的是理解如何用Metal实现均值模糊效果滤镜,均值模糊原理其实很简单通过多个纹理叠加,每个纹理偏移量设置不同达到一点重影效果来实现模糊; Demo HarbethDemo地址 实操代码 // ...
- Metal每日分享,海报画滤镜效果
本案例的目的是理解如何用Metal实现海报画效果滤镜,主要就是改变颜色级别数量从而获取到新的像素颜色: Demo HarbethDemo地址 实操代码 // 海报画滤镜 let filter = C7 ...
- Metal每日分享,图像单色滤镜效果
本案例的目的是理解如何用Metal实现图像单色效果滤镜,将图像转换为单色版本,根据每个像素的亮度进行着色: Demo HarbethDemo地址 实操代码 // 去雾效果滤镜 let filter = ...
- Metal每日分享,UV去雾滤镜效果
本案例的目的是理解如何用Metal实现去雾效果滤镜,类似于UV过滤器: Demo HarbethDemo地址 实操代码 // 去雾效果滤镜 let filter = C7Haze.init(dista ...
最新文章
- 报名 | 极简大数据决策,培养数据化思维讲座
- 一年后斩获腾讯T3,层层深入
- 初中数学四十二个几何模型_陪伴成长的初中数学几何模型大全
- html中radio,checkbox值的获取、赋值、注册事件
- Sliverlight变换特性
- 编程语言对比 字符串
- ​Azure 助力下,C9 电竞战队所向披靡!​
- @JVM垃圾收集器种类
- 重读模电教材一_童诗白、华成英
- MarkDown下载和安装
- web前端仿网易云官网
- windows 开启安全中心的方法
- KOF97判定框查看方法
- 机器学习篇-船长总结(hsrjdz01)
- TLE(两行轨道数据)卫星行李数据格式解析
- 【IAR Error】IAR MSP430编译报错:error
- Faiss(16):编译时添加对AVX512指令的支持
- mc服务器常用指令_MC服务器 新手指令教程
- 解决Gradle在IDEA中运行Java17的Junit单元测试程序报错:module java.base does not “opens java.lang“ to unnamed module
- python阿里巴巴_阿里图片采集,python selenium 采集阿里巴巴商品图片数据
热门文章
- 如何解决Mysql安装时卡在starting server的方法?(亲测有效)
- QUIC构建1——chromium镜像,clone文件depot_tools的过程+git过程中的问题
- 关于shell脚本的一些理解和基础
- google快速翻译英文网站
- 教你粉笔画的绘画技巧
- 857. 雇佣 K 名工人的最低成本
- ZStack Cube:超融合3.0,场景化的一体机
- 制造业企业招工难,怎么从根本上解决?
- 第22课:打包和发布 Electron 应用
- 【刷leetcode,拿Offer-009】561. Array Partition I(贪心,C++)
- Metal每日分享,自然饱和度滤镜效果