Metal每日分享,圖像處理色彩丟失和模糊效果

本案例的目的是理解如何用Metal實現圖像包裝效果濾鏡,用於圖像處理色彩丟失和模糊效果;


效果圖

Demo

實操代碼

// 色彩丟失和模糊效果
let filter = C7ColorPacking.init(horizontalTexel: 2.5, verticalTexel: 5)

// 方案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: "C7ColorPacking"),參數因子[horizontalTexel, verticalTexel]

對外開放參數

  • horizontalTexel: 橫向偏移,越大綠色輪廓虛影向右偏移越多;
  • verticalTexel: 縱向偏移,越大藍色輪廓虛影向下偏移越多;
/// 色彩丟失/模糊效果
public struct C7ColorPacking: C7FilterProtocol {
    
    /// The larger the transverse offset, the more the green contour shadow offset to the right.
    public var horizontalTexel: Float
    /// The larger the vertical offset, the more the blue contour shadow offset downward.
    public var verticalTexel: Float
    
    public var modifier: Modifier {
        return .compute(kernel: "C7ColorPacking")
    }
    
    public var factors: [Float] {
        return [horizontalTexel, verticalTexel]
    }
    
    public init(horizontalTexel: Float = 0, verticalTexel: Float = 0) {
        self.horizontalTexel = horizontalTexel
        self.verticalTexel = verticalTexel
    }
}
  • 着色器

紋理座標和偏移均歸一化處理,然後獲取到上下左右4個紋理偏移座標,取出對應的紋理紅色值,最後得到像素值;

kernel void C7ColorPacking(texture2d<half, access::write> outputTexture [[texture(0)]],
                           texture2d<half, access::sample> inputTexture [[texture(1)]],
                           device float *texelWidthPointer [[buffer(0)]],
                           device float *texelHeightPointer [[buffer(1)]],
                           uint2 grid [[thread_position_in_grid]]) {
    constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
    const float width  = outputTexture.get_width();
    const float height = outputTexture.get_height();
    const float texelWidth  = float(*texelWidthPointer / width);
    const float texelHeight = float(*texelHeightPointer / height);
    
    const float2 textureCoordinate = float2(float(grid.x) / width, float(grid.y) / height);
    const float2 upperLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, -texelHeight);
    const float2 upperRightTextureCoordinate = textureCoordinate + float2(texelWidth, -texelHeight);
    const float2 lowerLeftTextureCoordinate = textureCoordinate + float2(-texelWidth, texelHeight);
    const float2 lowerRightTextureCoordinate = textureCoordinate + float2(texelWidth, texelHeight);
    
    half upperLeftIntensity = inputTexture.sample(quadSampler, upperLeftTextureCoordinate).r;
    half upperRightIntensity = inputTexture.sample(quadSampler, upperRightTextureCoordinate).r;
    half lowerLeftIntensity = inputTexture.sample(quadSampler, lowerLeftTextureCoordinate).r;
    half lowerRightIntensity = inputTexture.sample(quadSampler, lowerRightTextureCoordinate).r;
    
    const half4 outColor = half4(upperLeftIntensity, upperRightIntensity, lowerLeftIntensity, lowerRightIntensity);
    
    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地址
  • 喜歡的老闆們可以點個星🌟,謝謝各位老闆!!!

✌️.

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