本案例的目的是理解如何用Metal實現圖像閥值素描濾鏡,用於圖像閥值素描,形成有噪點的素描;
效果圖
Demo
實操代碼
// 用於圖像閥值素描,形成有噪點的素描
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.
- 濾鏡部分大致分爲以下幾個模塊:
最後
- 慢慢再補充其他相關濾鏡,喜歡就給我點個星🌟吧。
-
濾鏡Demo地址,目前包含
100+
種濾鏡,同時也支持CoreImage混合使用。 - 再附上一個開發加速庫KJCategoriesDemo地址
- 再附上一個網絡基礎庫RxNetworksDemo地址
- 喜歡的老闆們可以點個星🌟,謝謝各位老闆!!!
✌️.