将Sepia、RGB、灰度效果应用于UIImage



我想在我的应用程序中对图像应用过滤效果。我是Open GL&想要将Sepia、RGB、灰度效果应用于我的应用程序中的图像。我已经实现了亮度、对比度、饱和度效果,但无法在灰度、RGB和饱和度上找到任何解决方案;深褐色效果。

提前谢谢。

您没有指定是在OpenGL ES 1.1还是2.0中执行此操作,所以我假设您指的是更新的2.0。如果1.1确实是你的目标,你需要像苹果在GLImageProcessing示例中那样使用混合模式。

对于OpenGL ES 2.0,可以使用片段着色器来实现每种效果。对于图像的灰度版本,可以使用以下片段着色器仅提取亮度:

 precision highp float;
 varying vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);
 void main()
 {
     float luminance = dot(texture2D(inputImageTexture, textureCoordinate).rgb, W);
     gl_FragColor = vec4(vec3(luminance), 1.0);
 }

对于深褐色色调,你可以使用我在这个答案中演示的颜色矩阵操纵着色器:

 varying highp vec2 textureCoordinate;
 uniform sampler2D inputImageTexture;
 uniform lowp mat4 colorMatrix;
 uniform lowp float intensity;
 void main()
 {
     lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
     lowp vec4 outputColor = textureColor * colorMatrix;
     gl_FragColor = (intensity * outputColor) + ((1.0 - intensity) * textureColor);
 }

带有矩阵

self.colorMatrix = (GPUMatrix4x4){
        {0.3588, 0.7044, 0.1368, 0},
        {0.2990, 0.5870, 0.1140, 0},
        {0.2392, 0.4696, 0.0912 ,0},
        {0,0,0,0},
    };

我不知道你说的"RGB效果"是什么意思。也许你指的是颜色矩阵操作,在这种情况下,上面的内容也适用于你。

所有这些都是我的开源GPUImage框架中的内置过滤器(请参阅GPUImageBrightnessFilter、GPUImageContrastFilter、GPUIImageSaturationFilter、GPUImageSepiaFilter和GPUImageColorMatrixFilter)。如果你真的是OpenGL ES的新手,那么你需要大量的代码来设置场景,将UIImage作为纹理引入,对其运行顶点和碎片着色器,检索该图像,并将其保存为UIImage。GPUImage将通过几行Objective-C代码为您完成所有这些。

最新更新