2
votes

I am trying to use the GPUImage framework's chromakey filter. I followed the Filtershowcase example, but obviously I am missed something becuase it only shows the video, but no green screen keying out effect. Here's my initialization of video camera/filter:

camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480
                                             cameraPosition:AVCaptureDevicePositionBack];
camera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
camera.horizontallyMirrorFrontFacingCamera = NO;
camera.horizontallyMirrorRearFacingCamera = NO;


chromaKeyFilter = [[GPUImageChromaKeyFilter alloc] init];
chromaKeyFilter.thresholdSensitivity = sliderThres.value;

UIImage *inputImage = [UIImage imageNamed:@"WID-small.jpg"];


GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage
                                                    smoothlyScaleOutput:YES];


[camera addTarget:chromaKeyFilter];
[sourcePicture addTarget:chromaKeyFilter];
[sourcePicture processImage];

[chromaKeyFilter addTarget:viewCamera];


[camera startCameraCapture];

So camera and sourcePicture both feed into the filter, filter than goes into viewCamera. But right now, I only see the plain video, no chromakey effect. (I have a slider that alters the threshold).

UPDATED with GPUImageChromaKeyBlendFilter

camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
camera.outputImageOrientation = UIInterfaceOrientationLandscapeLeft;
chromaKeyFilter = [[GPUImageChromaKeyBlendFilter alloc] init];


UIImage *inputImage;
inputImage = [UIImage imageNamed:@"WID-small.jpg"];
GPUImagePicture *sourcePicture = [[GPUImagePicture alloc] initWithImage:inputImage smoothlyScaleOutput:YES];


[camera addTarget:chromaKeyFilter];
[sourcePicture processImage];
[sourcePicture addTarget:chromaKeyFilter];
[chromaKeyFilter addTarget:viewCamera];

[camera startCameraCapture];

With this filter, it simply replaced it with black, not the actual image.

4

4 Answers

3
votes

Found this gem after a couple of days of trying..... Apparently you are suppose to hang on (strong ref) to GPUImagePicture. I was under the impression that the filter chain internally would have a strong reference, but that's not the case. Keeping it as instance var solved the problem.

1
votes

I think you want the GPUImageChromaKeyBlendFilter, not the GPUImageChromaKeyFilter.

The blend takes in two source images, and replaces pixels matching the keying color in the first image with the pixel from the second image. The regular chroma keying filter simply reduces the alpha channel of pixels matching the target color to 0.0, it doesn't blend in another image.

1
votes

Similar to the original question, I wanted to put a green-screen video on top of a custom view hierarchy incl. live video. Turned out this was not possible with the standard GPUImage ChromaKey filter(s). Instead of alpha blending, it blended the green pixels with the background pixels. For example a red background became yellow and blue became cyan.

The way to get it working involves two steps:

1) make sure the filterview has a transparent background:

filterView.backgroundColor=[UIColor clearColor];

2) Modify GPUImageChromaKeyFilter.m

old: gl_FragColor = vec4(textureColor.rgb, textureColor.a * blendValue);

new: gl_FragColor = vec4(textureColor.rgb * blendValue, 1.0 * blendValue);

Now all keyed (for example green) pixels in the video become transparent and uncover whatever is below the filterview, incl. (live-)video.

0
votes

Have you set the target and selector for the slider?

This worked for me

[(GPUImageChromaKeyFilter *)filter setThresholdSensitivity:[(UISlider *)sender value]];
        [image setImage:[filter imageByFilteringImage:sourceImage]];

Hope that helps.