Hidden away in Core Image's Geometry Adjustment category are a set of perspective related filters that change the geometry of flat images to simulate them being viewed in 3D space. If you work in architecture or out-of-home advertising, these filters, used in conjunction with Core Image's rectangle detector, are perfect for mapping images onto 3D surfaces. Alternatively, the filters can synthesise the effects of a perspective control lens.


Project Assets

This post comes with a companion Swift playground which is available here. The two assets we'll use are this picture of a billboard:



...and this picture of The Mona Lisa:



The assets are declared as:

    let monaLisa = CIImage(image: UIImage(named: "monalisa.jpg")!)!
    let backgroundImage = CIImage(image: UIImage(named: "background.jpg")!)!

Detecting the Target Rectangle

Our first task is to find the co-ordinates of the corners of the white rectangle and for that, we'll use a CIDetector. The detector needs a core image context and will return a CIRectangleFeature. In real life, there's no guarantee that it will not return nil, in the playground, with known assets, we can live life on the edge and unwrap it with a !.


    let ciContext =  CIContext()

    let detector = CIDetector(ofType: CIDetectorTypeRectangle,
        context: ciContext,
        options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])

    let rect = detector.featuresInImage(backgroundImage).first as! CIRectangleFeature

Performing the Perspective Transform

Now we have the four points that define the corners of the white billboard, we can apply those, along with the background input image, to a perspective transform filter. The perspective transform moves an image's original corners to a new set of coordinates and maps the pixels of the image accordingly: 


    let perspectiveTransform = CIFilter(name: "CIPerspectiveTransform")!


    perspectiveTransform.setValue(CIVector(CGPoint:rect.topLeft),
        forKey: "inputTopLeft")
    perspectiveTransform.setValue(CIVector(CGPoint:rect.topRight),
        forKey: "inputTopRight")
    perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomRight),
        forKey: "inputBottomRight")
    perspectiveTransform.setValue(CIVector(CGPoint:rect.bottomLeft),
        forKey: "inputBottomLeft")
    perspectiveTransform.setValue(monaLisa,
             forKey: kCIInputImageKey)

The output image of the perspective transform filter now looks like this:




We can now use a source atop compositing filter to simply composite the perspective transformed Mona Lisa over the background:


    let composite = CIFilter(name: "CISourceAtopCompositing")!

    composite.setValue(backgroundImage,
        forKey: kCIInputBackgroundImageKey)
    composite.setValue(perspectiveTransform.outputImage!,

        forKey: kCIInputImageKey)

The result is OK, but the aspect ratio of the transformed image is wrong and The Mona Lisa is stretched:




Fixing Aspect Ratio with Perspective Correction

To fix the aspect ratio, we'll use Core Image's perspective correction filter. This filter works in the opposite way to a perspective transform: it converts four points (which typically map to the corners of an image subject to perspective distortion) and converts them to a flat, two dimensional rectangle. 

We'll pass in the corner coordinates of the white billboard to a perspective correction filter which will return a version of the Mona Lisa cropped to the aspect ration of the billboard if we were looking at it head on:


    let perspectiveCorrection = CIFilter(name: "CIPerspectiveCorrection")!

    perspectiveCorrection.setValue(CIVector(CGPoint:rect.topLeft),
        forKey: "inputTopLeft")
    perspectiveCorrection.setValue(CIVector(CGPoint:rect.topRight),
        forKey: "inputTopRight")
    perspectiveCorrection.setValue(CIVector(CGPoint:rect.bottomRight),
        forKey: "inputBottomRight")
    perspectiveCorrection.setValue(CIVector(CGPoint:rect.bottomLeft),
        forKey: "inputBottomLeft")
    perspectiveCorrection.setValue(monaLisa,
        forKey: kCIInputImageKey)



A little bit of tweaking to centre the corrected image to the centre of the billboard rectangle:


    let perspectiveCorrectionRect = perspectiveCorrection.outputImage!.extent
    let cropRect = perspectiveCorrection.outputImage!.extent.offsetBy(
        dx: monaLisa.extent.midX - perspectiveCorrectionRect.midX,
        dy: monaLisa.extent.midY - perspectiveCorrectionRect.midY)


    let croppedMonaLisa = monaLisa.imageByCroppingToRect(cropRect)

...and we now have an output image of a cropped Mona Lisa at the correct aspect ration:



Finally, using the original perspective transform filter, we pass in the new cropped version rather than the original version to get a composite with the correct aspect ratio:


    perspectiveTransform.setValue(croppedMonaLisa,
        forKey: kCIInputImageKey)

    composite.setValue(perspectiveTransform.outputImage!,

        forKey: kCIInputImageKey)

Which gives the result we're probably after:




Core Image for Swift

Although my book doesn't actually cover detectors or perspective correction, Core Image for Swift, does take a detailed look at almost every aspect of still image processing with Core Image.

Core Image for Swift is available from both Apple's iBooks Store or, as a PDF, from Gumroad. IMHO, the iBooks version is better, especially as it contains video assets which the PDF version doesn't.




8

View comments

  1. Can you go the other way around? Crop to a portion of the image defined by a 4 points?

    ReplyDelete
    Replies
    1. We've already done this at https://github.com/hfossli/AGGeometryKit/, but using our own resampler which makes it all look pretty pixelated. I didn't know CoreImage supported perspective.

      Delete
    2. Perspective correction is the "opposite" of perspective transform.

      Delete
    3. Perspective correction is the "opposite" of perspective transform. Các gói cước 3G Mobifone

      Delete
  2. Hi Simon , This is very helpful blog you have written for perspective.
    What if the area where i have to apply is not CIDetectorTypeRectangle type like polygon or irregular shape ?

    ReplyDelete
  3. I want to rotate image with respect to specific angle before merging.but frame gets change
    please help
    thanks in advance.

    ReplyDelete
  4. I want to rotate image round its center with respect to specific angle before merging.but frame gets change
    please help
    thanks in advance.

    ReplyDelete
  5. This comment has been removed by the author.

    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