I'm a big fan of Apple's Core Image technology: my Nodality application is based entirely around Core Image filters. However, for new users, the code for adding a simple filter to an image is a little oblique and the implementation is very "stringly" typed.

This post looks at an alternative, GPUImage from Brad Larson. GPUImage is a framework containing a rich set of image filters, many of which aren't in Core Image. It has a far simpler and more strongly typed API and, in some cases, is faster than Core Image.

To kick off, let's look at the code required to apply a Gaussian blue to an image (inputImage) using Core Filter:


        let inputImage = UIImage()
        let ciContext = CIContext(options: nil)
        
        let blurFilter = CIFilter(name: "CIGaussianBlur")
        blurFilter.setValue(CIImage(image: inputImage), forKey: "inputImage")
        blurFilter.setValue(10, forKey: "inputRadius")
        
        let outputImageData = blurFilter.valueForKey("outputImage"as CIImage!
        let outputImageRef: CGImage = ciContext.createCGImage(outputImageData, fromRect: outputImageData.extent())

        let outputImage = UIImage(CGImage: outputImageRef)!

...not only do we need to explicitly define the context, both the filter name and parameter are strings and we need a few steps to convert the filter's output into a UIImage

Here's the same functionality using GPUImage:


        let inputImage = UIImage()
        
        let blurFilter = GPUImageGaussianBlurFilter()
        blurFilter.blurRadiusInPixels = 10
        
        let outputImage = blurFilter.imageByFilteringImage(inputImage)

Here, both the filter and its blur radius parameter are properly typed and the filter returns a UIImage instance.

On the flip-side, there is some setting up to do. Once you've got a local copy of GPUImage, drag the framework project into your application's project. Then under the application target's build phases, add a target dependency, a reference to GPUImage.framework under link binaries and a copy files stage.

Your build phases screen should look like this:



Then, by simply importing GPUImage, you're ready to roll.

To show off some of the funkier filters contained in GPUImage, I've created a little demonstration app, GPUImageDemo.

The app demonstrates Polar Pixellate, Polka Dot, Sketch, Threshold Sketch, Toon, Smooth Toon, Emboss, Sphere Refraction and Glass Sphere - none of which are available in Core Image. 

The filtering work is all done in my GPUImageDelegate class where a switch statement declares a GPUImageOutput variable (the class that includes the imageByFiltering() method) and sets it to the appropriate concrete class depending on the user interface.

For example, if the picker is set the threshold sketch, the following case statement is executed:


       case ImageFilter.ThresholdSketch:

            gpuImageFilter =  GPUImageThresholdSketchFilter()
            
            if let gpuImageFilter = gpuImageFilter as? GPUImageThresholdSketchFilter
            {
                if values.count > 1
                {
                    gpuImageFilter.edgeStrength = values[0]
                    gpuImageFilter.threshold = values[1]
                }
            }

If you build this project, you may encounter a build error on the documentation target. I've simply deleted this target on affected machines.

GPUImage is fast enough to filter video. I've taken my recent two million particles experiment and added a post processing step that consists of a cartoon filter and an emboss filter. These are packaged together in a GPUImageFilterGroup:


        let toonFilter = GPUImageSmoothToonFilter()
        let embossFilter = GPUImageEmbossFilter()

        let filterGroup = GPUImageFilterGroup()


        toonFilter.threshold = 1
        embossFilter.intensity = 2
        filterGroup.addFilter(toonFilter)
        filterGroup.addFilter(embossFilter)
        
        toonFilter.addTarget(embossFilter)
        
        filterGroup.initialFilters = [ toonFilter ]

        filterGroup.terminalFilter = embossFilter

Since GPUImageFilterGroup extends GPUImageFilterOutput, I can take the output from the  Metal texture, create a UIImage instance of it and pass it to the composite filter:


        self.imageView.image = self.filterGroup.imageByFilteringImage(UIImage(CGImage: imageRef)!)

On my iPad Air 2, the final result of 2,000,000 particles with a two filter post process on a 1,024 x 1,024 image still runs at around 20 frames per second. Here's a real time screen capture:



The source code for my GPUImageDemo is available at my GitHub repository here and GPUImage lives here.


4

View comments

  1. their is errors when i tried to run the application
    how can i solve it i use xcode 7.2

    ReplyDelete
  2. I'm using an old version of GPUImage. I'll try to find time to update it.

    ReplyDelete
  3. Same problem. We would all be thankful if you could update it. Thanks.

    ReplyDelete
  4. Just updated that project - runs fine now under Xcode 7.2. Cheers!

    ReplyDelete
Primordial Particle System in SideFX Houdini
5
"Sparse Vermiform" Rendering of Fluids with Pressure Based Color
3
Particle Advection by Gray Scott Reaction Diffusion Revisited
Particle Advection by Reaction Diffusion in SideFX Houdini
1
Revisiting Mitosis in SideFX Houdini
4
Faux Grain / Fluid Interaction in Houdini
1
Parametric Fibonacci Spheres in Houdini
Houdini Grain Solver with Custom VEX Forces
Animating Rayleigh-Taylor Instability in SideFX Houdini
1
Faking Toroidal Eddies in Side FX Houdini
Mixing Fluids in Houdini
Animating Gravitational Tides with Houdini FLIP Fluids
1
Houdini FLIP Fluid & Radial Gravity
1
Reaction Diffusion in SideFX Houdini
2
Animating Planet Engulfment in Houdini
1
Simulating Accretion with Houdini's Grains Solver
Chaotic Magnetic Pendulum with Custom Radial Forces in Houdini
More Chaos in Houdini: Simulating a Double Pendulum
Creating a Geometric Structure from Mitosis
2
Simulating Mitosis in Houdini
4
Stripy Viscous Fluid Impacts in Houdini
1
Using Houdini VOPs to Deform Geometry
1
Creating a Swarm Chemistry Digital Asset in Houdini
Swarm Chemistry in SideFX Houdini
1
Randomly Transforming Scattered Cones in Houdini
1
Metaball Morphogenesis in Houdini
Melting Geometry in Houdini
2
Simulating Belousov-Zhabotinsky Reaction in Houdini
Chaos in Houdini! Modeling Strange Attractors with Particles
Experimenting with Impacts in SideFX Houdini
Chaos in Swift! Visualising the Lorenz Attractor with Metal
Swift 3.0 for Core Image Developers
4
A Core Image Transverse Chromatic Aberration Filter in Swift
1
Random Numbers in Core Image Kernel Language
Core Image for Swift Version 1.3
Core Image for Swift Version 1.3
Nodality for AudioKit: Node Based Synth for iPad
Histogram Equalisation with Metal Performance Shaders
A Histogram Display Component in Swift for iOS
Simulating Depth-of-Field with Variable Bokeh in Core Image
Simulating Bokeh with Metal Performance Shaders
1
Creating a Lens Flare Filter in Core Image
Loading, Filtering & Saving Videos in Swift
3
vImage Histogram Functions Part II: Specification
Histogram Functions in Accelerate vImage
1
New Core Image Procedural Noise Generators for Filterpedia
Recreating Kai's Power Tools Goo in Swift
11
Creating Procedural Normal Maps for SceneKit
New Custom Core Image Filters
1
A Look at Perspective Transform & Correction with Core Image
8
Creating a Custom Variable Blur Filter in Core Image
3
Core Image for Swift v1.2 Released!
3
Creating a Selective HSL Adjustment Filter in Core Image
Creating a Bulging Eyes Purikura Effect with Core Image
1
Sweetcorn: A Node Based Core Image Kernel Builder
1
Core Image for Swift: Advanced Image Processing for iOS
2
Properly Typed Selectors in Xcode 7.3 beta 4
7
Core Image for Swift Available for Pre-Order!
10
Playing with Interpolation Functions in Swift
1
Metal Kernel Functions as Core Image Filter Engines
New Custom Core Image Filters Added to Filterpedia
5
Computational Fluid Dynamics in CoreImage with CIKernel
3
Creating a Slide Show App with Core Image Transitions
Filterpedia: Core Image Filter Explorer
5
CartoonEyes: Compositing Cartoon Eyes over Face from Front Camera in Swift
3
Apple Pencil Controlled Christmas Tree Bowling with SceneKit
1
BristlePaint: Embossed Painting with Individual Bristles using SpriteKit Normal Mapping
1
Scribe: A Handwriting Recognition Component for iOS
2
MercurialPaint: Globular Embossed Painting with Metal & Core Image
MercurialText: Embossed Type using SceneKit and CIShadedMaterial
1
FurrySketch: Hirsute Drawing with an Apple Pencil
PencilSynth - An Apple Pencil Controlled Synthesiser
PencilController - Using Apple Pencil as a 3D Controller for Image Editing
5
A Look at Agents, Goals & Behaviours in GameplayKit
PencilScale - Using an Apple Pencil with an iPad Pro as an Electronic Scale
2
A First Look at Metal Performance on the iPad Pro
Smooth Drawing for iOS in Swift with Hermite Spline Interpolation
3
Introducing Image Processing in Metal
Swift Hierarchical Selector Component based on UIPickerView & UICollectionView
The Plum-O-Meter: Weighing Plums Using 3D Touch in Swift
27
Book Review: Swift Documentation Markup by Erica Sadun
1
3D Touch in Swift: A Retrospective
3D ReTouch: An Experimental Retouching App Using 3D Touch
1
ForceZoom: Popup Image Detail View using 3D Touch Peek
Globular: Colourful Metaballs Controlled by 3D Touch
2
ForceSketch: A 3D Touch Drawing App using CIImageAccumulator
DeepPressGestureRecognizer - A 3D Touch Custom Gesture Recogniser in Swift
2
Rotatable: A Swift Protocol Extension to Rotate any UIView
ChromaTouch: a 3D Touch Colour Picker in Swift
3
3D Touch in Swift: Implementing Peek & Pop
1
A First Look at Metal Performance on the iPhone 6s
Applying Gaussian Blur to UIViews with Swift Protocol Extensions
7
Advanced Touch Handling in iOS9: Coalescing and Prediction
3
A Swift Node Based User Interface Component for iOS
2
Using an iPhone as a 3D Mouse with Multipeer Connectivity in Swift
6
A Swift Nixie Tube Display Component
CoreMotion Controlled 3D Sketching on an iPhone with Swift
2
iOS Live Camera Controlled Particles in Swift & Metal
6
Metal Performance Shaders & Fallback Copy Allocators
Event Dispatching in Swift with Protocol Extensions
1
Hybrid Marking Menu / Radial Slider Swift Component for iOS
About Me
About Me
Labels
Labels
Blog Archive
Loading