阅读 39

Metal每日分享,连环画滤镜和油画滤镜效果

本案例的目的是理解如何用Metal实现连环画滤镜和油画滤镜,将图像处理成连环画和油画效果;


Demo

  • HarbethDemo地址

实操代码

// 连环画效果 let filter = C7ComicStrip.init() // 方案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 复制代码

连环画实现原理

  • 过滤器

这款滤镜采用并行计算编码器设计.compute(kernel: "C7ComicStrip")

/// 连环画滤镜 public struct C7ComicStrip: C7FilterProtocol {          public var modifier: Modifier {         return .compute(kernel: "C7ComicStrip")     }          public init() { } } 复制代码

  • 着色器

获取到红色值abs(g - b + g + r) * r绝对值,绿色abs(b - g + b + r) * r,蓝色abs(b - g + b + r) * g,最后获取到像素颜色;

kernel void C7ComicStrip(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 half r = inColor.r;     const half g = inColor.g;     const half b = inColor.b;          const half R = half(abs(g - b + g + r) * r);     const half G = half(abs(b - g + b + r) * r);     const half B = half(abs(b - g + b + r) * g);          const half4 outColor = half4(R, G, B, inColor.a);          outputTexture.write(outColor, grid); } 复制代码

油画实现原理

  • 过滤器

这款滤镜采用并行计算编码器设计.compute(kernel: "C7OilPainting"),参数因子[radius, Float(pixel)]

对外开放参数

  • radius: 模糊半径;

  • pixel: 像素颗粒度;

/// 油画滤镜 public struct C7OilPainting: C7FilterProtocol {          public var radius: Float = 3.0     public var pixel: Int = 1          public var modifier: Modifier {         return .compute(kernel: "C7OilPainting")     }          public var factors: [Float] {         return [radius, Float(pixel)]     }          public init(radius: Float = 3.0, pixel: Int = 1) {         self.radius = radius         self.pixel = pixel     } } 复制代码

  • 着色器

kernel void C7OilPainting(texture2d<half, access::write> outputTexture [[texture(0)]],                           texture2d<half, access::sample> inputTexture [[texture(1)]],                           constant float *radius [[buffer(0)]],                           constant float *pixel [[buffer(1)]],                           uint2 grid [[thread_position_in_grid]]) {     constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);     const float2 size = float2(*pixel) / float2(outputTexture.get_width(), outputTexture.get_height());     const float2 textureCoordinate = float2(grid) / float2(outputTexture.get_width(), outputTexture.get_height());     const float r = float(*radius);     const float n = float((r + 1.0) * (r + 1.0));          float3 m0 = float3(0.0);     float3 m1 = float3(0.0);     float3 s0 = float3(0.0);     float3 s1 = float3(0.0);     float3 color = float3(0.0);          for (float j = -r; j <= 0.0; ++j)  {         for (float k = -r; k <= 0.0; ++k)  {             color = float3(inputTexture.sample(quadSampler, textureCoordinate + float2(k,j) * size).rgb);             m0 += color;             s0 += color * color;         }     }          for (float j = -r; j <= 0.0; ++j)  {         for (float k = 0.0; k <= r; ++k)  {             color = float3(inputTexture.sample(quadSampler, textureCoordinate + float2(k,j) * size).rgb);             m1 += color;             s1 += color * color;         }     }          half4 outColor = half4(0.0h);     float min_sigma2 = 100.0;     m0 /= n;     s0 = abs(s0 / n - m0 * m0);     float sigma2 = s0.r + s0.g + s0.b;     if (sigma2 < min_sigma2) {         min_sigma2 = sigma2;         outColor = half4(half3(m0), 1.0h);     }          m1 /= n;     s1 = abs(s1 / n - m1 * m1);     sigma2 = s1.r + s1.g + s1.b;     if (sigma2 < min_sigma2) {         min_sigma2 = sigma2;         outColor = half4(half3(m1), 1.0h);     }          outputTexture.write(outColor, grid); } 复制代码

效果图

原图连环画油画
WX20221202-102106.pngWX20221202-095423.pngWX20221202-101038.png

Harbeth功能清单

  • 支持ios系统和macOS系统

  • 支持运算符函数式操作

  • 支持多种模式数据源 UIImage, CIImage, CGImage, CMSampleBuffer, CVPixelBuffer.

  • 支持快速设计滤镜

  • 支持合并多种滤镜效果

  • 支持输出源的快速扩展

  • 支持相机采集特效

  • 支持视频添加滤镜特效

  • 支持矩阵卷积

  • 支持使用系统 MetalPerformanceShaders.

  • 支持兼容 CoreImage.

  • 滤镜部分大致分为以下几个模块:

    • Blend:图像融合技术

    • Blur:模糊效果

    • Pixel:图像的基本像素颜色处理

    • Effect:效果处理

    • Lookup:查找表过滤器

    • Matrix: 矩阵卷积滤波器

    • Shape:图像形状大小相关

    • Visual: 视觉动态特效

    • MPS: 系统 MetalPerformanceShaders.


作者:茶底世界之下
链接:https://juejin.cn/post/7172371363641229326


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐