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地址

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

相关文章
|
算法 数据可视化
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
1652 0
|
4月前
|
计算机视觉
【图像处理】基于灰度矩的亚像素边缘检测方法理论及MATLAB实现
基于灰度矩的亚像素边缘检测方法,包括理论基础和MATLAB实现,通过计算图像的灰度矩来精确定位边缘位置,并提供了详细的MATLAB代码和实验结果图。
127 6
|
6月前
|
算法 BI 计算机视觉
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
50 0
|
7月前
|
算法
halcon系列之直线拟合和圆形拟合算子
halcon系列之直线拟合和圆形拟合算子
801 0
|
7月前
|
算法
[贴装专题] 基于halcon的拟合矩形边缘验证贴装精度
[贴装专题] 基于halcon的拟合矩形边缘验证贴装精度
87 0
|
小程序
小程序实现全屏幕高斯模糊背景图
小程序实现全屏幕高斯模糊背景图
207 0
|
并行计算 iOS开发 计算机视觉
Metal每日分享,均值模糊滤镜效果
Metal每日分享,均值模糊滤镜效果
Metal每日分享,均值模糊滤镜效果
|
并行计算 iOS开发 MacOS
Metal每日分享,图像阀值素描滤镜效果
Metal每日分享,图像阀值素描滤镜效果
Metal每日分享,图像阀值素描滤镜效果
|
并行计算 iOS开发 MacOS
Metal每日分享,自然饱和度滤镜效果
Metal每日分享,自然饱和度滤镜效果
Metal每日分享,自然饱和度滤镜效果
|
算法 数据安全/隐私保护
点云地面点滤波-progressive TIN densification(PTD)算法介绍
点云地面点滤波-progressive TIN densification(PTD)算法介绍
804 0
点云地面点滤波-progressive TIN densification(PTD)算法介绍