Metal每日分享,图像阀值素描滤镜效果
本案例的目的是理解如何用Metal实现图像阀值素描滤镜,用于图像阀值素描,形成有噪点的素描;
Demo
- HarbethDemo地址
实操代码
// 用于图像阀值素描,形成有噪点的素描
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 = 1public 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 = edgeStrengthself.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.
最后
- 慢慢再补充其他相关滤镜,喜欢就给我点个星
Metal每日分享,图像阀值素描滤镜效果相关推荐
- Metal每日分享,纯色图片滤镜效果
本案例的目的是理解如何用Metal实现纯色图片效果滤镜,主要就是生成纯色图片: Demo HarbethDemo地址 实操代码 // 纯色滤镜 ImageView.image = C7Color.pu ...
- Metal每日分享,颜色转换滤镜效果
本案例的目的是理解如何用Metal实现像素颜色转换滤镜,通过对像素颜色的不同读取方式获取到相应像素颜色,灰度图移除场景中除了黑白灰以外所有的颜色,让整个图像灰度化: Demo HarbethDemo地 ...
- Metal每日分享,虚假颜色混合滤镜效果
本案例的目的是理解如何用Metal实现虚假颜色效果滤镜,使用图像的亮度在两种用户指定的颜色之间进行混合: Demo HarbethDemo地址 实操代码 // 混合颜色 let filter = C7 ...
- Metal每日分享,均值模糊滤镜效果
本案例的目的是理解如何用Metal实现均值模糊效果滤镜,均值模糊原理其实很简单通过多个纹理叠加,每个纹理偏移量设置不同达到一点重影效果来实现模糊; Demo HarbethDemo地址 实操代码 // ...
- Metal每日分享,海报画滤镜效果
本案例的目的是理解如何用Metal实现海报画效果滤镜,主要就是改变颜色级别数量从而获取到新的像素颜色: Demo HarbethDemo地址 实操代码 // 海报画滤镜 let filter = C7 ...
- Metal每日分享,调节亮度滤镜效果
本案例的目的是理解如何用Metal实现调节亮度滤镜,简单讲就是通过对像素rgb+亮度值来改变图片亮度; Demo HarbethDemo地址 实操代码 // 调节亮度滤镜 let filter = C ...
- Metal每日分享,调整对比度滤镜效果
本案例的目的是理解如何用Metal实现调整对比度效果滤镜,调整对比度就是在保证平均亮度不变的情况下,扩大或缩小亮的点和暗的点之间的差异: Demo HarbethDemo地址 实操代码 // 对比度 ...
- IOS OpenGL ES GPUImage 图像阀值素描,形成有噪点的素描 GPUImageThresholdSketchFilter
目录 一.简介 二.效果演示 三.源码下载 四.猜你喜欢 零基础 OpenGL (ES) 学习路线推荐 : OpenGL (ES) 学习目录 >> OpenGL ES 基础 零基础 Ope ...
- Metal每日分享,自然饱和度滤镜效果
本案例的目的是理解如何用Metal实现自然饱和度效果滤镜,简单讲就是调整图像整体的明亮程度,如调节到较高数值,图像会产生色彩过饱和从而引起图像失真: Demo HarbethDemo地址 iDay每日 ...
最新文章
- 少年五年升阿里P8,他如何从低谷登上“光明顶”?
- Asp.net导出Excel
- 微软正在帮助政府构建AI灾难响应工具
- mysql php 新手卡生成_PHPMaker(基于MYSQL数据库自动生成PHP 脚本的软件)
- SpringMVC注解开发(基础)---SpringMVC学习笔记(七)
- php csrf攻击 xss区别,XSS与CSRF攻击及防御方法
- Django(二) 路由和视图
- 解决sodu echo写入文件是权限不足-bash: test.txt: Permission denied
- Windows和Mac下获取(当前)进程内存占用
- 学做网站(1):环境搭建
- mysql - InnoDB存储引擎 死锁问题( Deadlock found when trying to get lock; try restarting transaction )...
- java学习资料整理(开发必备)
- 科睿唯安官网更新SCI期刊列表,慎投2月已被剔除期刊
- Vue使用Emoji表情
- UESTC - 59 数据大搜索
- 6款免费网络延迟测试工具
- Qt for winrt结合Winrt API开发
- 微信浏览器中进行支付宝支付
- mac系统设置alias
- 云控是什么?有什么用处?
热门文章
- Metal每日分享,纯色图片滤镜效果