Horizontal Flip of a frame in Objective-C
Asked Answered
Y

2

4

I am trying to create a filter for my program (which streams a webcam) which makes the frame flip horizontally, making the webcam act like a mirror.

However, while it compiles and runs, the filter does not seem to have any effect on it.

Here is the code:

CIImage *resultImage = image;
CIFilter *flipFilter = [CIFilter filterWithName:@"CIAffineTransform"];
[flipFilter setValue:resultImage forKey:@"inputTransform"];

NSAffineTransform* flipTransform = [NSAffineTransform transform];
[flipTransform scaleXBy:-1.0 yBy:1.0]; //horizontal flip
[flipFilter setValue:flipTransform forKey:@"inputTransform"];

resultImage = [flipFilter valueForKey:@"outputImage"];

Strangely enough, other filters appear to work. For example:

CIFilter * ciEffectFilter = [CIFilter filterWithName:@"CIGloom"];
[ciEffectFilter setDefaults];
CIImage *resultImage = image;
[ciEffectFilter setValue: resultImage forKey:@"inputImage"];
[ciEffectFilter setValue:[NSNumber numberWithFloat: 0.75]
                  forKey:@"inputIntensity"];
[ciEffectFilter setValue:[NSNumber numberWithFloat: 20]
                  forKey:@"inputRadius"];
resultImage = [ciEffectFilter valueForKey:@"outputImage"];


CIFilter *ciCropFilter = [CIFilter filterWithName:@"CICrop"];
[ciCropFilter setDefaults];
[ciCropFilter setValue: resultImage forKey:@"inputImage"];
[ciCropFilter setValue: [CIVector vectorWithX:50 Y:50 Z:300 W:
                         300] forKey:@"inputRectangle"];

resultImage = [ciCropFilter valueForKey:@"outputImage"];

Is it something to do with NSAffineTransform? I have looked around, but I have not found any answers. Thanks heaps!

Yaws answered 24/1, 2011 at 2:37 Comment(0)
C
8

I'm not sure if this is the problem, but in your first code snippet you are setting two different values for the same key (you set inputTransform twice), I think the first one should be inputImage instead. Try the following instead:

CIImage *resultImage = image;
CIFilter *flipFilter = [CIFilter filterWithName:@"CIAffineTransform"];
[flipFilter setValue:resultImage forKey:@"inputImage"];

NSAffineTransform* flipTransform = [NSAffineTransform transform];
[flipTransform scaleXBy:-1.0 yBy:1.0]; //horizontal flip
[flipFilter setValue:flipTransform forKey:@"inputTransform"];

resultImage = [flipFilter valueForKey:@"outputImage"];

Also, you may need to translate the X axis by the width of the image

[flipTransform translateXBy:... yBy:0];
Combs answered 24/1, 2011 at 2:41 Comment(1)
Note: this is the solution for OS X. On iOS, you would need to use NSValue instead of NSAffineTransform: [NSValue valueWithBytes:&flipTransform objCType:@encode(CGAffineTransform)];Yungyunick
G
1
CIImage *cimage = [[CIImage alloc] initWithImage:image];

You are not Allocating CIImage Object . Try This code.

Gammer answered 1/2, 2012 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.