神刀安全网

Creating a Bulging Eyes Purikura Effect With Core Image

Creating a Bulging Eyes Purikura Effect With Core Image

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:

let features = detector.featuresInImage(cameraImage).first as? 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 through Gumroad

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Creating a Bulging Eyes Purikura Effect With Core Image

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮