Metal每日分享,图像阀值素描滤镜效果

简介: Metal每日分享,图像阀值素描滤镜效果

Demo


HarbethDemo地址

iDay每日分享文档地址


实操代码


// 用于图像阀值素描,形成有噪点的素描
let filter = C7ThresholdSketch.init(edgeStrength: 2.5, threshold: 0.25)
// 方案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: "C7ThresholdSketch"),参数因子[edgeStrength, threshold]

对外开放参数

threshold: 任何高于这个阈值的边缘都是黑色的,低于白色的任何东西都是黑色的。

edgeStrength: 调整滤波器的动态范围。更高的值会导致更强的边缘,但可以饱和强度的色彩空间。

public struct C7ThresholdSketch: C7FilterProtocol {
    public static let range: ParameterRange<Float, Self> = .init(min: 0.0, max: 1.0, value: 0.25)
    /// Any edge above this threshold will be black, and anything below white. Ranges from 0.0 to 1.0
    @ZeroOneRange public var threshold: Float = range.value
    /// Adjusts the dynamic range of the filter.
    /// Higher values lead to stronger edges, but can saturate the intensity colorspace.
    public var edgeStrength: Float = 1
    public var modifier: Modifier {
        return .compute(kernel: "C7ThresholdSketch")
    }
    public var factors: [Float] {
        return [edgeStrength, threshold]
    }
    public init(edgeStrength: Float = 1, threshold: Float = range.value) {
        self.edgeStrength = edgeStrength
        self.threshold = threshold
    }
}

着色器

取出周边1像素对应点的归一化坐标,获取到这些点对应的红色值;

水平方向取顶部和底部红色值做个整合处理,竖直方向取左边和右边做个整合处理,分别得到(h, v);

length计算出范围值,step计算出阈值对应的颜色,最后得到黑色或白色像素颜色即可;

kernel void C7ThresholdSketch(texture2d<half, access::write> outputTexture [[texture(0)]],
                              texture2d<half, access::sample> inputTexture [[texture(1)]],
                              constant float *edgeStrength [[buffer(0)]],
                              constant float *threshold [[buffer(1)]],
                              uint2 grid [[thread_position_in_grid]]) {
    constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
    const float x = float(grid.x);
    const float y = float(grid.y);
    const float width = float(inputTexture.get_width());
    const float height = float(inputTexture.get_height());
    const float2 leftCoordinate = float2((x - 1) / width, y / height);
    const float2 rightCoordinate = float2((x + 1) / width, y / height);
    const float2 topCoordinate = float2(x / width, (y - 1) / height);
    const float2 bottomCoordinate = float2(x / width, (y + 1) / height);
    const float2 topLeftCoordinate = float2((x - 1) / width, (y - 1) / height);
    const float2 topRightCoordinate = float2((x + 1) / width, (y - 1) / height);
    const float2 bottomLeftCoordinate = float2((x - 1) / width, (y + 1) / height);
    const float2 bottomRightCoordinate = float2((x + 1) / width, (y + 1) / height);
    const half leftIntensity = inputTexture.sample(quadSampler, leftCoordinate).r;
    const half rightIntensity = inputTexture.sample(quadSampler, rightCoordinate).r;
    const half topIntensity = inputTexture.sample(quadSampler, topCoordinate).r;
    const half bottomIntensity = inputTexture.sample(quadSampler, bottomCoordinate).r;
    const half topLeftIntensity = inputTexture.sample(quadSampler, topLeftCoordinate).r;
    const half topRightIntensity = inputTexture.sample(quadSampler, topRightCoordinate).r;
    const half bottomLeftIntensity = inputTexture.sample(quadSampler, bottomLeftCoordinate).r;
    const half bottomRightIntensity = inputTexture.sample(quadSampler, bottomRightCoordinate).r;
    half h = -topLeftIntensity - 2.0h * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0h * bottomIntensity + bottomRightIntensity;
    h = max(0.0h, h);
    half v = -bottomLeftIntensity - 2.0h * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0h * rightIntensity + topRightIntensity;
    v = max(0.0h, v);
    half mag = length(half2(h, v)) * half(*edgeStrength);
    mag = 1.0h - step(half(*threshold), mag);
    const half4 outColor = half4(half3(mag), 1.0h);
    outputTexture.write(outColor, grid);
}


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

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

相关文章
|
IDE Shell 网络安全
使用ESC服务器配置code-server
使用ESC服务器来配置code-server服务(在线VSCode编辑器)
2288 1
使用ESC服务器配置code-server
|
3月前
|
人工智能 算法 机器人
3组实测数据拆解DeepSeek引用机制:从平台推荐到证据链构建
本文基于2026年初实测与普林斯顿AI搜索研究,揭示DeepSeek引用机制:内容需先被知乎、CSDN等平台推荐,再进入其语料库。实证表明,含引用来源、统计数据、直接引语的内容被引用率分别提升34.4%、32.1%、29.7%。文章提供平台适配、证据块拆解、深水区监测等可复现路径。
860 3
|
9月前
|
JSON API 开发者
实战指南:使用API高效获取纳斯达克股票数据
本文为开发者提供纳斯达克股票数据API实战指南:支持实时行情、历史K线(1分钟至日线)、公司基本面及IPO日历等全维度数据;仅需`countryId=5`即可接入,统一JSON返回,附Python示例与WebSocket低延迟方案。(239字)
|
数据采集 人工智能 供应链
2025年适合汽车行业与互联网企业的BI产品选型指南
2025年,数字化转型加速,BI工具成企业决策核心。本文对比瓴羊Quick BI、Power BI、Tableau、永洪科技、Domo五大主流产品,从能力、行业适配、案例等维度解析,重点推荐阿里云旗下瓴羊Quick BI,其在汽车与互联网行业表现突出,兼具AI分析、高性能计算与信创合规优势,助力企业实现数据价值最大化。
|
9月前
|
自然语言处理 搜索推荐 BI
企业级 Agent 解决方案
瓴羊企业级Agent解决方案以AgentOne为智能中枢,打通数据孤岛、工具链断裂与业务闭环缺失三大痛点,构建“感知-决策-执行-反馈”全链路数智闭环。集成Quick Service、智能小Q、Quick Audience及Dataphin四大专业智能体,实现服务、分析、营销与数据治理的全域协同。(239字)
|
9月前
|
人工智能 自然语言处理 前端开发
准时上线:当AI建站邂逅“Just in Time”哲学
本文提出“Just in Time AI建站”新范式,打破传统AI一次性生成的局限,以渐进式共创替代“生成-返工”循环。云指AI在用户需要时(雏形、调优、内容、获客各阶段)提供精准、局部、即时支持,让建站从技术任务回归创作本质——适时、适度、适配,做用户全程陪伴的智能伙伴。
421 5
|
11月前
|
存储 人工智能 弹性计算
玄晶引擎AI数字化转型技术方案:基于阿里云生态的服务业民企降本增效实践
玄晶引擎深度融合阿里云生态,针对服务业民企“轻资产、重运营”痛点,构建以知识库为底座、AI智能力体为核心的云原生数字化转型方案,实现精准获客、智能运营与盈利重构,助力企业降本增效、拓展业务边界。
698 14
|
11月前
|
存储 弹性计算 分布式计算
2026年阿里云 ECS 本地盘解析:NVMe SSD 与 SATA HDD 特性及选型指南
在阿里云 ECS 实例的存储方案中,本地盘凭借 “物理机直连” 的特性,提供低延迟、高 IOPS 的存储体验,成为高性能与海量存储场景的重要选择。目前阿里云本地盘主要分为 NVMe SSD 与 SATA HDD 两类,二者在性能、适用场景、实例兼容性上差异显著,需结合业务需求精准匹配。本文从技术特性、性能参数、适配实例及场景选型四方面展开解析,为用户提供客观参考。
2026年阿里云 ECS 本地盘解析:NVMe SSD 与 SATA HDD 特性及选型指南
|
12月前
|
Java 关系型数据库 MySQL
Spring Boot事务配置管理
Spring Boot中事务管理确保数据操作的可靠性,通过@Transactional注解实现异常回滚。但需注意:仅运行时异常默认回滚,非运行时异常需指定rollbackFor;try-catch吞掉异常会导致回滚失效;事务范围大于synchronized锁范围时可能引发并发问题。合理配置方可避免数据不一致。