Metal每日分享,高斯双边模糊滤镜效果

简介: Metal每日分享,高斯双边模糊滤镜效果

Demo


HarbethDemo地址

iDay每日分享文档地址


实操代码


// 双边模糊滤镜
let filter = C7BilateralBlur.init(radius: 10)
// 方案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


效果对比图


不同参数下效果

radius: 10 radius: 2 radius: 0.5

1.png

1.png

1.png


实现原理


过滤器

这款滤镜采用并行计算编码器设计.compute(kernel: "C7BilateralBlur")

/// 双边模糊
public struct C7BilateralBlur: C7FilterProtocol {
    public var radius: Float = 1
    public var offect: C7Point2D = C7Point2D.center
    public var modifier: Modifier {
        return .compute(kernel: "C7BilateralBlur")
    }
    public var factors: [Float] {
        return [radius, offect.x, offect.y]
    }
    public init(radius: Float = 1) {
        self.radius = radius
    }
}

着色器

将高斯滤波中通过各个点到中心点的空间临近度计算的各个权值进行优化,将其优化为空间临近度(四周8个点)计算的权值和像素值相似度计算的权值的乘积,优化后的权值再与图像作卷积运算;

kernel void C7BilateralBlur(texture2d<half, access::write> outputTexture [[texture(0)]],
                            texture2d<half, access::sample> inputTexture [[texture(1)]],
                            constant float *blurRadius [[buffer(0)]],
                            constant float *stepOffsetX [[buffer(1)]],
                            constant float *stepOffsetY [[buffer(2)]],
                            uint2 grid [[thread_position_in_grid]]) {
    const int GAUSSIAN_SAMPLES = 9;
    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 inCoordinate(x / width, y / height);
    int multiplier = 0;
    float2 blurStep;
    float2 singleStepOffset(float(*stepOffsetX * 10) / width, float(*stepOffsetY * 10) / height);
    float2 blurCoordinates[GAUSSIAN_SAMPLES];
    for (int i = 0; i < GAUSSIAN_SAMPLES; i++) {
        multiplier = (i - ((GAUSSIAN_SAMPLES - 1) / 2));
        blurStep = float(multiplier) * singleStepOffset;
        blurCoordinates[i] = inCoordinate + blurStep;
    }
    half4 centralColor;
    half gaussianWeightTotal;
    half4 sum;
    half4 sampleColor;
    half distanceFromCentralColor;
    half gaussianWeight;
    constexpr sampler quadSampler(mag_filter::linear, min_filter::linear);
    const float distanceNormalizationFactor = float(abs(1 - *blurRadius));
    centralColor = inputTexture.sample(quadSampler, blurCoordinates[4]);
    gaussianWeightTotal = 0.18;
    sum = centralColor * 0.18;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[0]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[1]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[2]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[3]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[5]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.15 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[6]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.12 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[7]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.09 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    sampleColor = inputTexture.sample(quadSampler, blurCoordinates[8]);
    distanceFromCentralColor = min(distance(centralColor, sampleColor) * distanceNormalizationFactor, 1.0);
    gaussianWeight = 0.05 * (1.0 - distanceFromCentralColor);
    gaussianWeightTotal += gaussianWeight;
    sum += sampleColor * gaussianWeight;
    const half4 outColor = sum / gaussianWeightTotal;
    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地址

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

相关文章
|
6月前
|
算法 BI 计算机视觉
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
47 0
|
7月前
|
算法
[贴装专题] 基于halcon的拟合矩形边缘验证贴装精度
[贴装专题] 基于halcon的拟合矩形边缘验证贴装精度
83 0
|
前端开发 JavaScript
神奇的滤镜!巧妙实现内凹的平滑圆角
神奇的滤镜!巧妙实现内凹的平滑圆角
288 0
神奇的滤镜!巧妙实现内凹的平滑圆角
|
小程序
小程序实现全屏幕高斯模糊背景图
小程序实现全屏幕高斯模糊背景图
198 0
|
并行计算 iOS开发 计算机视觉
Metal每日分享,均值模糊滤镜效果
Metal每日分享,均值模糊滤镜效果
Metal每日分享,均值模糊滤镜效果
|
并行计算 iOS开发 MacOS
Metal每日分享,自然饱和度滤镜效果
Metal每日分享,自然饱和度滤镜效果
Metal每日分享,自然饱和度滤镜效果
|
并行计算 iOS开发 MacOS
Metal每日分享,图像阀值素描滤镜效果
Metal每日分享,图像阀值素描滤镜效果
Metal每日分享,图像阀值素描滤镜效果
|
并行计算 iOS开发 MacOS
Metal每日分享,3x3矩阵卷积滤镜效果
Metal每日分享,3x3矩阵卷积滤镜效果
Metal每日分享,3x3矩阵卷积滤镜效果
|
编解码 人工智能 算法
最新代码开源!TartanCalib:自适应亚像素细化的广角镜头标定
作者测试了三种利用中间相机模型的关键方法:(1)将图像分解为虚拟针孔相机,(2)将目标重新投影到图像帧中,以及(3)自适应亚像素细化。将自适应子像素细化和特征重投影相结合,可将重投影误差显著提高26.59%,帮助检测到最多42.01%的特征,并提高密集深度映射下游任务的性能。最后,TartanCalib是开源的,并在一个易于使用的标定工具箱中实现。作者还提供了一个translation 层和其它最先进的工作,允许使用数千个参数回归通用模型或使用更稳健的求解器。为此,TartanCalib是广角标定的首选工具!
最新代码开源!TartanCalib:自适应亚像素细化的广角镜头标定
|
并行计算 计算机视觉 iOS开发
Metal每日分享,图像处理色彩丢失和模糊效果
Metal每日分享,图像处理色彩丢失和模糊效果
Metal每日分享,图像处理色彩丢失和模糊效果