Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best Core Image filter to produce black and white effects?

I am using Core Image and would like to produce a black and white effect on the chosen image.

Ideally I would like to have access to the same sort of options that are available on Photoshop i.e. Reds, Cyan, Greens, Blues and Magenta. The goal being to create different types of the black and white effect.

Does anyone know what filter would be best to manipulate these sort of options? If not does anyone know of a good approach to creating the black and white effect using other filters?

Thanks

Oliver

like image 881
ORStudios Avatar asked Apr 05 '12 14:04

ORStudios


People also ask

What is CIContext?

Overview. The CIContext class provides an evaluation context for Core Image processing with Quartz 2D, Metal, or OpenGL. You use CIContext objects in conjunction with other Core Image classes, such as CIFilter , CIImage , and CIColor , to process images using Core Image filters.

What is CIImage?

A CIImage is a immutable object that represents an image. It is not an image. It only has the image data associated with it. It has all the information necessary to produce an image. You typically use CIImage objects in conjunction with other Core Image classes such as CIFilter, CIContext, CIColor, and CIVector.

How do I create a custom filter in Swift?

Here's what you need in your subclass: class CustomFilter:CIFilter { let kernel = CIColorKernel(source: "kernel vec4 brightenEffect (sampler src , float k) { \n " + "vec4 currentSource = sample (src, samplerCoord (src));" + "currentSource. rgb = currentSource. rgb + k * currentSource.


2 Answers

- (UIImage *)imageBlackAndWhite {     CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];      CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil].outputImage;     CIImage *output = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", [NSNumber numberWithFloat:0.7], nil].outputImage;       CIContext *context = [CIContext contextWithOptions:nil];     CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];     //UIImage *newImage = [UIImage imageWithCGImage:cgiimage];     UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:image.scale orientation:image.imageOrientation];     CGImageRelease(cgiimage);      return newImage; } 

Upd.: For iOS6 there is CIColorMonochrome filter, but I played with it and found it not so good as mine.

like image 113
Shmidt Avatar answered Sep 23 '22 04:09

Shmidt


here is example with CIColorMonochrome

- (UIImage *)imageBlackAndWhite {     CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];      CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:1.0], @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]], nil].outputImage;      CIContext *context = [CIContext contextWithOptions:nil];     CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];     UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:self.scale orientation:self.imageOrientation];      CGImageRelease(cgiimage);      return newImage; } 
like image 26
wasikuss Avatar answered Sep 25 '22 04:09

wasikuss