Metal每日分享,图像处理色彩丢失和模糊效果

简介: Metal每日分享,图像处理色彩丢失和模糊效果

Demo


HarbethDemo地址

iDay每日分享文档地址


实操代码


// 色彩丢失和模糊效果
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);
}


效果图


横行纵向偏移 横向偏移 纵向偏移

1.png

1.png

1.png


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

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

目录
打赏
0
0
0
0
5
分享
相关文章
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
Halcon边缘检测和线条检测(1),文章含自适应/动态二值化等算子
1760 0
图像处理之调整亮度与对比度
图像处理之调整亮度与对比度
129 6
图像处理之基于像素的图像混合
图像处理之基于像素的图像混合
62 1
图像处理之积分图应用一(半径无关的快速模糊算法)
图像处理之积分图应用一(半径无关的快速模糊算法)
71 0
图像处理之调整亮度与饱和度
图像处理之调整亮度与饱和度
98 0
Baumer工业相机堡盟相机如何使用JPEG图像压缩功能(LXT.JP系列相机图像压缩功能的使用和优点以及行业应用)(C#)
Baumer工业相机堡盟相机如何使用JPEG图像压缩功能(LXT.JP系列相机图像压缩功能的使用和优点以及行业应用)(C#)
118 0
Baumer万兆网工业相机堡盟相机LXT.JP图像压缩相机在图像压缩模式下不同情况下的压缩比
Baumer万兆网工业相机堡盟相机LXT.JP图像压缩相机在图像压缩模式下不同情况下的压缩比
103 0
Baumer工业相机堡盟相机如何使用JPEG图像压缩功能(LXT.JP系列相机图像压缩功能的使用和优点以及行业应用)(C++)
Baumer工业相机堡盟相机如何使用JPEG图像压缩功能(LXT.JP系列相机图像压缩功能的使用和优点以及行业应用)(C++)
130 0
Baumer工业相机堡盟工业相机如何联合BGAPISDK和OpenCV实现图像的伽马变换校正算法增强(C++)
Baumer工业相机堡盟工业相机如何联合BGAPISDK和OpenCV实现图像的伽马变换校正算法增强(C++)
293 0
Baumer工业相机堡盟相机VLXT-90M.I如何做平场校正阴影校正:消除图像明暗不均匀现象
Baumer工业相机堡盟相机VLXT-90M.I如何做平场校正阴影校正:消除图像明暗不均匀现象
204 0