Quantcast
Channel: FlexMonkey
Viewing all articles
Browse latest Browse all 257

Creating a Bulging Eyes Purikura Effect with Core Image

$
0
0


One of my fellow speakers at try! SwiftMathew Gillingham, suggested I look at the "bulging" eyes effect used in Japanese Purikura photo booths. Always up for a challenge, I thought I'd spend a quick moment repurposing my cartoon eyes experiment to simulate the effect using Core Image. The final project is available in my Purikura GitHub repository.

The main change to this project is in the eyeImage(_:) method. After ensuring that the Core Image face detector has data for the left and right eye positions:

    iflet features = detector.featuresInImage(cameraImage).firstas? CIFaceFeature
        where features.hasLeftEyePosition&& features.hasRightEyePosition
    {
    [...]

..I use a small CGPoint extension to measure the distance between the eye positions on screen:

    [...]
    let eyeDistance = features.leftEyePosition.distanceTo(features.rightEyePosition)
    [...]

I use that eye distance, divided by 1.25, as the radius for two Core Image Bump Distortion filters which are centred over the eye positions:

    [...]
    return cameraImage
        .imageByApplyingFilter("CIBumpDistortion",
            withInputParameters: [
                kCIInputRadiusKey: eyeDistance / 1.25,
                kCIInputScaleKey: 0.5,
                kCIInputCenterKey: features.leftEyePosition.toCIVector()])
        .imageByCroppingToRect(cameraImage.extent)
        .imageByApplyingFilter("CIBumpDistortion",
            withInputParameters: [
                kCIInputRadiusKey: eyeDistance / 1.25,
                kCIInputScaleKey: 0.5,
                kCIInputCenterKey: features.rightEyePosition.toCIVector()])
        .imageByCroppingToRect(cameraImage.extent)
    }

If the detector finds no eye positions, eyeImage(_:) simply returns the original image.

This project has been tested on my iPad Pro and iPhone 6s and works great on both!

If you'd like to learn more about the awesome power of Apple's Core Image, my book, Core Image For Swift, is now available through the iBook Store and, now though Gumroad


Viewing all articles
Browse latest Browse all 257

Trending Articles