Search code examples
objective-cioscocoa-touchgpuimage

GPUImage Chroma Key filter


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.


Solution

  • 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.