简体   繁体   English

将棕褐色,RGB,灰度效果应用于UIImage

[英]Applying Sepia, RGB, GrayScale effect to UIImage

I want to apply filter effects to image in my app. 我想将滤镜效果应用到我的应用程序中的图像。 I am new in Open GL & want to apply Sepia, RGB, GrayScale effect to image in my app. 我是Open GL的新手,想在我的应用程序中将棕褐色,RGB,GrayScale效果应用于图像 I have implemented Brightness, Contrast, Saturation effects ,but was unable to found any solution on Grayscale, RGB & sepia effect. 我已经实现了亮度,对比度,饱和度效果 ,但是无法找到任何关于灰度,RGB和棕褐色效果的解决方案。

Thanks in advance. 提前致谢。

You don't specify whether you want to do this in OpenGL ES 1.1 or 2.0, so I'm going to assume that you are referring to the newer 2.0. 您没有指定是要在OpenGL ES 1.1还是2.0中执行此操作,因此我假设您是指较新的2.0。 If 1.1 is indeed your target, you'll need to play with blend modes like Apple does in their GLImageProcessing example . 如果1.1确实是您的目标,则需要像Apple的GLImageProcessing示例中那样使用混合模式。

For OpenGL ES 2.0, you can use fragment shaders to achieve each of these effects. 对于OpenGL ES 2.0,您可以使用片段着色器来实现所有这些效果。 For a grayscale version of an image, you can extract just the luminance using the following fragment shader: 对于图像的灰度版本,可以使用以下片段着色器仅提取亮度:

 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);
 }

For a sepia tone, you can employ the color matrix manipulation shader I demonstrate in this answer : 对于棕褐色调,您可以使用我在此答案中演示的颜色矩阵操作着色器:

 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);
 }

with a matrix of 的矩阵

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},
    };

I have no idea what you mean by "RGB effect." 我不知道您所说的“ RGB效果”是什么意思。 Perhaps you mean color matrix manipulation, in which case the above will work for you in that case as well. 也许您指的是颜色矩阵操作,在这种情况下,上面的内容同样适用于您。

All of these are built-in filters within my open source GPUImage framework (see the GPUImageBrightnessFilter, GPUImageContrastFilter, GPUImageSaturationFilter, GPUImageSepiaFilter, and GPUImageColorMatrixFilter). 所有这些都是我的开源GPUImage框架中的内置过滤器(请参阅GPUImageBrightnessFilter,GPUImageContrastFilter,GPUImageSaturationFilter,GPUImageSepiaFilter和GPUImageColorMatrixFilter)。 If you're truly a newcomer to OpenGL ES, it will take you quite a bit of code to set up your scene, pull in your UIImage as a texture, run a vertex and fragment shader against it, retrieve that image, and save it back out as a UIImage. 如果您确实是OpenGL ES的新手,它将花费您很多代码来设置场景,将UIImage用作纹理,对其运行顶点和片段着色器,检索该图像并保存它作为UIImage退出。 GPUImage will do all of that for you with a few lines of Objective-C code. GPUImage将通过几行Objective-C代码为您完成所有这些工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM