Search code examples
iosimage-processingconvolutiongpuimage

Convolution UIImage with GPUImage framework


I'm trying to use GPUImage3x3ConvolutionFilter of GPUImage framework but is not working.

This is my code, I only get a white image.

- (UIImage *)convolution:(UIImage *)inputImage{

    GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

    GPUImage3x3ConvolutionFilter *filter = [[GPUImage3x3ConvolutionFilter alloc] init];
    [filter setConvolutionKernel:(GPUMatrix3x3){
        {-1.0f,  0.0f, 1.0f},
        {-2.0f,  0.0f, 2.0f},
        {-1.0f,  0.0f, 1.0f}
    }];

    [stillImageSource addTarget:filter];
    [stillImageSource processImage];

    return stillImageSource.imageFromCurrentlyProcessedOutput;

}

I'm also try changing:

return stillImageSource.imageFromCurrentlyProcessedOutput;

for:

return filter.imageFromCurrentlyProcessedOutput;

Solution

  • This was due to a bug in the framework. Convolutions were also being applied against the alpha channel of the image, which resulted in a 0 alpha channel value in the the [filter imageFromCurrentlyProcessedOutput] case. I just committed code to fix this.

    Note that [stillImageSource imageFromCurrentlyProcessedOutput] does nothing, because you can't grab the unprocessed images directly from a camera input. Also, I wouldn't use dot notation here, because -imageFromCurrentlyProcessedOutput is not a property of GPUImageOutput.