反色是与原色叠加可以变为白色的颜色,即用白色(RGB:1.0,1.0,1.0)减去原色的颜色。比如说红色(RGB:1.0,0,0)的反色是青色(0,1.0,1.0)。在OPENGL ES中为1.
通过导入GPUImage库的GPUImageColorInvertFilter来实现iOS的图像反色处理
1 (
2 varying highp vec2 textureCoordinate;
3
4 uniform sampler2D inputImageTexture;
5
6 void main()
7 {
8 lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
9
10 gl_FragColor = vec4((1.0 - textureColor.rgb), textureColor.w);
11 }
12 );
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
我们首先使用texture2D函数来获取图像的颜色RGBA空间
gl_FragColor = vec4((1.0 - textureColor.rgb), textureColor.w);
使用1-对应的RGB值来得到新的RGB值。生成新的颜色,然后着色。
+ (UIImage *)applyColorInvertFilter:(UIImage *)image
{
GPUImageColorInvertFilter *filter = [[GPUImageColorInvertFilter alloc] init];
[filter forceProcessingAtSize:image.size];
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
[pic addTarget:filter];
[pic processImage];
[filter useNextFrameForImageCapture];
return [filter imageFromCurrentFramebuffer];
}
效果图
灰色的RGB值是(0.5,0.5,0.5) 使用GPUImageSepiaFilter对象来实现图像的灰色处理
+ (UIImage *)applySepiaFilter:(UIImage *)image
{
GPUImageSepiaFilter *filter = [[GPUImageSepiaFilter alloc] init];
[filter forceProcessingAtSize:image.size];
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
[pic addTarget:filter];
[pic processImage];
[filter useNextFrameForImageCapture];
return [filter imageFromCurrentFramebuffer];
}
色彩直方图
色彩直方图是在许多图像检索系统中被广泛采用的颜色特征。它所描述的是不同色彩在整幅图像中所占的比例,而并不关心每种色彩所处的空间位置,即无法描述图像中的对象或物体。颜色直方图特别适于描述那些难以进行自动分割的图像。
GPUImageHistogramGenerator对象生成图像的色彩直方图
+ (UIImage *)applyHistogramFilter:(UIImage *)image
{
GPUImageHistogramGenerator *filter = [[GPUImageHistogramGenerator alloc] init];
[filter forceProcessingAtSize:image.size];
GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
[pic addTarget:filter];
[pic processImage];
[filter useNextFrameForImageCapture];
return [filter imageFromCurrentFramebuffer];
}