How to use CIColorMatrix in iOS5?

I am trying to figure out how to change the color / hue of a UIImage. I found out that iOS5 has a lot of image filters, but it's hard for me to find documentation on the proper use of the CIColorMatrix filter.

 -(void)doCIColorMatrixFilter { //does not work, returns nil image CIImage* inputImage = [CIImage imageWithCGImage:[[UIImage imageNamed:@"button.jpg"]CGImage]]; CIFilter *myFilter; NSDictionary *myFilterAttributes; myFilter = [CIFilter filterWithName:@"CIColorMatrix"]; [myFilter setDefaults]; myFilterAttributes = [myFilter attributes]; [myFilterAttributes setValue:inputImage forKey:@"inputImage"]; //How to set up attributes? CIContext *context = [CIContext contextWithOptions:nil]; CIImage *ciimage = [myFilter outputImage]; CGImageRef cgimg = [context createCGImage:ciimage fromRect:[ciimage extent]]; UIImage *uimage = [UIImage imageWithCGImage:cgimg scale:1.0f orientation:UIImageOrientationUp]; [imageView setImage:uimage]; CGImageRelease(cgimg); } 

What code is included in the dictionary for this filter?

+6
source share
2 answers

In a month...

This is an example of CIColorMatrix , setting all its parameters :)

 -(void)doCIColorMatrixFilter { // Make the input image recipe CIImage *inputImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"facedetectionpic.jpg"].CGImage]; // 1 // Make the filter CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; // 2 [colorMatrixFilter setDefaults]; // 3 [colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; // 4 [colorMatrixFilter setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5 [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0] forKey:@"inputGVector"]; // 6 [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0] forKey:@"inputBVector"]; // 7 [colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8 // Get the output image recipe CIImage *outputImage = [colorMatrixFilter outputImage]; // 9 // Create the context and instruct CoreImage to draw the output image recipe into a CGImage CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; // 10 // Draw the image in screen UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:cgimg]]; CGRect f = imageView2.frame; f.origin.y = CGRectGetMaxY(imageView.frame); imageView2.frame = f; [self.view addSubview:imageView2]; } 

So here is what the selection does:

In 1 we create ciimage, if you get zero, then make sure you pass the correct UIImage / CGImage or path.

In 2 create a filter, you know that :)

In 3 set the default filter parameters, the CoreImage programming guide offers us to do this, though (I have not experimented with some strange / bad things if avoided.)

In 4 set the input ciimage

From 5 to 8 we set the parameters. For example, I made a red vector {1,1,1,0}, so the image looks reddish. 6 , 7 and 8 not needed here, since their values โ€‹โ€‹coincide with the default values โ€‹โ€‹(remember, we called -setDefaults ?), But for educational purposes, I think they are fine :)

At 9 set the output image, although it has not yet been drawn.

Finally, at 10 you tell CoreImage to draw the output image in CGImage, and we will put this CGImage in UIImage and inside UIImageView.

This is the result (I used the same image as this ):

Reddish

Hope this helps.

+20
source

Just a Swift example with a chain to add to the nacho4d answer above

 var output = CIFilter(name: "CIColorControls") output.setValue(ciImage, forKey: kCIInputImageKey) output.setValue(1.8, forKey: "inputSaturation") output.setValue(0.01, forKey: "inputBrightness") filter = CIFilter(name: "CIColorMatrix") filter.setDefaults() filter.setValue(output.outputImage, forKey: kCIInputImageKey) filter.setValue(CIVector(x: 1, y: 0.1, z: 0.1, w: 0), forKey: "inputRVector") filter.setValue(CIVector(x: 0, y: 1, z: 0, w: 0), forKey: "inputGVector") filter.setValue(CIVector(x: 0, y: 0, z: 1, w: 0), forKey: "inputBVector") 

Note that the default values โ€‹โ€‹are 1.0.0 0.1.0 and 0.0.1 respectively for RGB, and if you want to make it more red, you must set inputRVector to 1.1.1 and leave the others the same (since it is a multiple of the blue and green components according to the red values)

+5
source

Source: https://habr.com/ru/post/909490/


All Articles