Metal每日分享,虛假顏色混合濾鏡效果

本案例的目的是理解如何用Metal實現虛假顏色效果濾鏡,使用圖像的亮度在兩種用戶指定的顏色之間進行混合;


Demo

實操代碼

// 混合顏色
let filter = C7FalseColor.init(fristColor: .blue, secondColor: .green)

// 方案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: "C7FalseColor")

對外開放參數

  • fristColor: 第一和第二種顏色分別指定了哪些顏色取代了圖像的深色和淺色區域;
  • secondColor: 第二種顏色;
/// 使用圖像的亮度在兩種用戶指定的顏色之間進行混合
/// Uses the luminance of the image to mix between two user-specified colors
public struct C7FalseColor: C7FilterProtocol, ComputeFiltering {
    
    /// The first and second colors specify what colors replace the dark and light areas of the image, respectively.
    public var fristColor: C7Color = .zero
    
    public var secondColor: C7Color = .zero
    
    public var modifier: Modifier {
        return .compute(kernel: "C7FalseColor")
    }
    
    public func setupSpecialFactors(for encoder: MTLCommandEncoder, index: Int) {
        guard let computeEncoder = encoder as? MTLComputeCommandEncoder else { return }
        var fristFactor = Vector3.init(color: fristColor).to_factor()
        computeEncoder.setBytes(&fristFactor, length: Vector3.size, index: index + 1)
        var secondFactor = Vector3(color: secondColor).to_factor()
        computeEncoder.setBytes(&secondFactor, length: Vector3.size, index: index + 2)
    }
    
    public init(fristColor: C7Color = .zero, secondColor: C7Color = .zero) {
        self.fristColor = fristColor
        self.secondColor = secondColor
    }
}
  • 着色器

計算出輸入像素dot(inColor.rgb, luminanceWeighting)亮度點積,然後再兩種輸入顏色當中混合mix(color1.rgb, color2.rgb, luminance)獲取到rgb;

kernel void C7FalseColor(texture2d<half, access::write> outputTexture [[texture(0)]],
                         texture2d<half, access::read> inputTexture [[texture(1)]],
                         constant float3 *firstVector [[buffer(0)]],
                         constant float3 *secondVector [[buffer(1)]],
                         uint2 grid [[thread_position_in_grid]]) {
    const half4 inColor = inputTexture.read(grid);
    
    const half3 luminanceWeighting = half3(0.2125, 0.7154, 0.0721);
    const half luminance = dot(inColor.rgb, luminanceWeighting);
    const half3 color1 = half3(*firstVector);
    const half3 color2 = half3(*secondVector);
    const half4 outColor = half4(mix(half3(color1.rgb), half3(color2.rgb), half3(luminance)), inColor.a);
    
    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地址
  • 喜歡的老闆們可以點個星🌟,謝謝各位老闆!!!

✌️.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章