将Sepia,RGB,GrayScale效果应用于UIImage

nik*_*iks 5 opengl-es uiimage ios

我想在我的应用中将滤镜效果应用于图像.我是Open GL的新手,想在我的应用程序中将Sepia,RGB,GrayScale效果应用于图像.我已经实现了亮度,对比度,饱和度效果,但无法在灰度,RGB和棕褐色效果上找到任何解决方案.

提前致谢.

Bra*_*son 5

您没有指定是否要在OpenGL ES 1.1或2.0中执行此操作,因此我将假设您指的是较新的2.0.如果1.1确实是你的目标,那么你需要在他们的GLImageProcessing示例中使用像Apple那样的混合模式.

对于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);
 }
Run Code Online (Sandbox Code Playgroud)

对于棕褐色调,您可以使用我在此答案中演示的颜色矩阵操作着色器:

 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);
 }
Run Code Online (Sandbox Code Playgroud)

用矩阵

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},
    };
Run Code Online (Sandbox Code Playgroud)

我不明白你的意思是"RGB效果".也许你的意思是颜色矩阵操作,在这种情况下,上述情况也适用于你.

所有这些都是我的开源GPUImage框架中的内置过滤器(请参阅GPUImageBrightnessFilter,GPUImageContrastFilter,GPUImageSaturationFilter,GPUImageSepiaFilter和GPUImageColorMatrixFilter).如果你真的是OpenGL ES的新手,它会花费你很多代码来设置场景,将你的UIImage作为纹理拉入,运行顶点和片段着色器,检索图像,然后保存它作为UIImage退出.GPUImage将使用几行Objective-C代码为您完成所有这些工作.