SlideShare ist ein Scribd-Unternehmen logo
1 von 58
Downloaden Sie, um offline zu lesen
Core Image	

The Most Fun API	

You’re Not Using
Chris Adamson • @invalidname	

CocoaConf Columbus, August 2014
“Core Image is an image processing and analysis
technology designed to provide near real-time
processing for still and video images.”
Agenda
• Images, Filters, and Contexts	

• The Core Image Filter Gallery	

• Neat Tricks with Built-In Filters	

• Core Image on OS X
Core Image, Core Concepts
• Core Image is
of the time	

• A chain of filters describes a “recipe” of processing
steps to be applied to one or more images	

• “Stringly typed”	

• You only get pixels when you render
Typical Workflow
• Start with a source CIImage	

• Apply one or more filters	

• Render resulting CIImage to a CIContext, or
convert CIImage out to another type	

• A few filters take or produce types other than
CIImage (CIQRCodeGenerator)
CIImage
• An image provided to or produced by Core Image	

• But no bitmap of pixel data!	

• Immutable	

• -imageByCroppingToRect,
-imageByApplyingTransform	

• -extent — a CGRect of the image’s size
CIImage sources
• NSURL	

• CGImageRef	

• Bitmap or JPEG/PNG/TIFF in NSData	

• OpenGL texture	

• Core Video image/pixel buffer
CIContext
• Rendering destination for a CIImage (-
[drawImage:inRect:fromRect:])	

• This is where you get pixels (also, this is the processor-
intenstive part)	

• On iOS, must be created from an EAGLContext. On
Mac, can be created with CGContextRef	

• Can also produce output as a CGImageRef, bitmap data,
or a CVPixelBuffer (iOS only)
????
CIFilter
• Performs an image processing operation	

• Typically takes and produces a CIImage	

• All parameters are provided via -[setValue:forKey:]	

• Stringly-typed!	

• Output is retrieved with -[valueForKey:]
–Core Image Cat
“I can has filterz?
Yes, you can has Filterz!
Core Image Filter Reference
Filter Name
Parameters
Note the type & number to
provide
Categories
Watch for CICategoryBuiltIn
and CICategoryVideo
Example Figure
Availability
Watch for versioning and
OS X-only filters
Filter Categories
• Group filters by functionality: CICategoryBlur,
CICategoryGenerator,
CICategoryCompositeOperation, etc.	

• Also group filters by availability and
appropriateness: CICategoryBuiltIn,
CICategoryVideo, CICategoryNonSquarePixels
CICategoryGenerator
• No input image, just produces an output	

• CICategoryGradient is also output-only	

• Example: CICheckerboardGenerator
CICategoryBlur
• Algorithmically spreads/blends pixels	

• CICategorySharpen offers an opposite effect	

• Example: CIGaussianBlur
CICategoryColorAdjustement
• Changes distribution of color throughout an image	

• Example: CIColorControls (adjusts saturation,
brightness, contrast)
CICategoryColorEffect
• Color changes that affect the subjective nature of
the image	

• Example: CIPhotoEffectNoir
CICategoryDistortionEffect
• Moves pixels to achieve an effect	

• Example: CITorusLensDistortion
CICategoryStylize
• Various stylistic effects	

• Example: CIPointillize
CICategoryGeometryAdjustment
• Moves pixels via cropping, affine transforms, etc.	

• Example: CICrop
CICategoryTileEffect
• Repeatedly copies all or part of an image	

• Example: CIAffineTile
CICategoryCompositeOperation
• Combines multiple images	

• Example: CISourceOverCompositing
Demo
Creating CIColorControls Filter
_colorControlsFilter
Setting input values
[self
! ! ! ! ! ! ! ! ! ! ! !
[self
! ! ! ! ! ! ! ! ! ! ! !
[self
! ! ! ! ! ! ! ! ! ! ! !
Setting input image
CIImage
! ! ! ! ! ! ! ! ! !
[self
! ! ! ! ! ! ! !
Getting output image
ciImage = [
UIImage
self
Other output options
• Use a CIContext	

• -[drawImage:inRect:fromRect:] draws pixels to
the EAGLContext (iOS) or CGContextRef (OS
X) that the CIContext was created from.	

• CIContext can also render to a void* bitmap	

• On iOS, can create a CVPixelBufferRef, typically
used for writing to a file with AVAssetWriter
Chaining filters
• Use the output of one filter as the input to the next	

• This doesn’t cost anything, because the CIImages
just hold state, not pixels
Demo
Creating CIContext
if (!
! !
! ! ! ! ! !
! !
}!
! ! !
// make CIContext from GL context, clearing out default color space
self
! ! ! ! ! ! ! ! ! ! ! ! !
! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
!
Note: This is in a subclass of GLKView
Set up Sepia Tone filter
_sepiaToneFilter
[_sepiaToneFilter
! ! ! ! ! ! ! ! !
Set up Mask to Alpha filter
UIImage
_circleMaskFilter
CIImage
! ! ! ! ! ! ! ! ! ! ! ! circleImageUI.
[_circleMaskFilter
! ! ! ! ! ! !
_circleMask
circle-mask-100x100.png
Set up Blend with Mask filter
_constantColorGeneratorFilter
! ! ! ! ! ! ! ! ! !
[_constantColorGeneratorFilter
! ! ! [
! ! ! ! ! ! ! ! ! ! ! ! ! !
forKey
_backgroundAlphaFill
! ! ! ! ! ! ! ! ! ! !
!
_blendWithMaskFilter
[_blendWithMaskFilter
! ! ! ! ! ! ! !
[_blendWithMaskFilter
! ! ! ! ! ! ! !
Apply filters
// Get CIImage from source image
CGImageRef
! ! ! ! ! ! ! !
loupeImage = [
!
// Apply sepia filter
[self
loupeImage = [
!
// Set sepia-filtered image as input to blend-with-mask
[_blendWithMaskFilter
! ! ! ! ! !
loupeImage = [
Render in CIContext
if ([
! [
}!
! !
[self
!
// GL-on-Retina fix
CGRect
drawBoundsInPoints.
drawBoundsInPoints.
! !
// drawing to CIContext draws to the EAGLESContext it's based on
[self
! ! ! ! !! ! !
! ! ! ! ! ! ! !
!
// Refresh GLKView contents immediately
[self
Working with Video
• AVFoundation AVCaptureVideoDataOutput and
AVAssetReader deliver CMSampleBuffers	

• CMSampleBuffers have timing information and
CVImageBuffers/CVPixelBuffers	

• +[CIImage imageWithCVPixelBuffer:]
Demo
Chroma Key (“green screen”
recipe
• Use a CIColorCube to map green-ish colors to
transparent	

• Use CISourceOverCompositing to draw this
alpha’ed image over another image
CIColorCube
Maps colors from one RGB “cube” to another
http://en.wikipedia.org/wiki/RGB_color_space
Using CIColorCube
CIColorCube maps green(-ish) colors to 0.0 alpha, all
other colors pass through
CISourceOverCompositing
inputBackgroundImage inputImage
outputImage
CIColorCube Dataconst unsigned int size = 64;!
size_t cubeDataSize = size * size * size * sizeof (float) * 4;!
float *keyCubeData = (float *)malloc (cubeDataSize);!
//! float *alphaMatteCubeData = (float *)malloc (cubeDataSize);!
//! float rgb[3], hsv[3], *keyC = keyCubeData, *alphaC = alphaMatteCubeData;!
float rgb[3], hsv[3], *keyC = keyCubeData;!
// Populate cube with a simple gradient going from 0 to 1!
for (int z = 0; z < size; z++){!
! rgb[2] = ((double)z)/(size-1); // Blue value!
! for (int y = 0; y < size; y++){!
! ! rgb[1] = ((double)y)/(size-1); // Green value!
! ! for (int x = 0; x < size; x ++){!
! ! ! rgb[0] = ((double)x)/(size-1); // Red value!
!
! ! ! // Convert RGB to HSV!
! ! ! // You can find publicly available rgbToHSV functions on the Internet!
!
! ! ! RGBtoHSV(rgb[0], rgb[1], rgb[2],!
! ! ! ! ! &hsv[0], &hsv[1], &hsv[2]);!
!
! ! ! // RGBtoHSV uses 0 to 360 for hue, while UIColor (used above) uses 0 to 1.!
! ! ! hsv[0] /= 360.0;!
! ! ! !
! ! ! // Use the hue value to determine which to make transparent!
! ! ! // The minimum and maximum hue angle depends on!
! ! ! // the color you want to remove!
! ! ! !
! ! ! bool keyed = (hsv[0] > minHueAngle && hsv[0] < maxHueAngle) &&!
! ! ! (hsv[1] > minSaturation && hsv[1] < maxSaturation) &&!
! ! ! (hsv[2] > minBrightness && hsv[2] < maxBrightness);!
! ! ! !
! ! ! float alpha = keyed ? 0.0f : 1.0f;!
! ! ! !
! ! ! // re-calculate c pointer!
! ! ! keyC = (((z * size * size) + (y * size) + x) * sizeof(float)) + keyCubeData;!
! ! ! !
! ! ! // Calculate premultiplied alpha values for the cube!
! ! ! keyC[0] = rgb[0] * alpha;!
! ! ! keyC[1] = rgb[1] * alpha;!
! ! ! keyC[2] = rgb[2] * alpha;!
! ! ! keyC[3] = alpha;!
! ! ! ! ! ! ! !
! ! }!
! }!
}!
See “Chroma Key Filter Recipe” in Core Image Programming Guide
Create CIColorCube from
mapping data
// build the color cube filter and set its data to above!
self.colorCubeFilter = [CIFilter filterWithName:@"CIColorCube"];!
[self.colorCubeFilter setValue:[NSNumber numberWithInt:size]!
! !! ! ! ! ! ! ! ! forKey:@"inputCubeDimension"];!
NSData *data = [NSData dataWithBytesNoCopy:keyCubeData!
! !! ! ! ! ! ! ! length:cubeDataSize!
! !! ! ! ! ! freeWhenDone:YES];!
[self.colorCubeFilter setValue:data forKey:@"inputCubeData"];!
Create CISourceOverCompositing
// source over filter!
self.backgroundImage = [UIImage imageNamed:!
! !! ! ! ! ! ! ! ! ! @"img_washington_small_02.jpg"]; !
self.backgroundCIImage = [CIImage imageWithCGImage:!
! !! ! ! ! ! ! ! ! ! ! self.backgroundImage.CGImage];!
self.sourceOverFilter = [CIFilter filterWithName:!
! !! ! ! ! ! ! ! ! ! @"CISourceOverCompositing"];!
[self.sourceOverFilter setValue:self.backgroundCIImage !
! !! ! ! ! ! ! ! forKeyPath:@"inputBackgroundImage"];!
Apply Filters in Capture Callback
CIImage *bufferCIImage = [CIImage imageWithCVPixelBuffer:cvBuffer];!
!
[self.colorCubeFilter setValue:bufferCIImage !
! !! ! ! ! ! ! ! ! ! forKey:kCIInputImageKey];!
CIImage *keyedCameraImage = [self.colorCubeFilter valueForKey:!
! !! ! ! ! ! ! ! ! ! ! ! kCIOutputImageKey];!
!
[self.sourceOverFilter setValue:keyedCameraImage !
! !! ! ! ! ! ! ! ! forKeyPath:kCIInputImageKey];!
!
CIImage *compositedImage = [self.sourceOverFilter valueForKeyPath:!
! !! ! ! ! ! ! ! ! ! ! kCIOutputImageKey];
Then draw compositedImage to CIContext as before
Other Points of Interest
• CIQRCodeGenerator filter — Converts data (e.g., a string) to
a QR Code	

• CILenticularHaloGenerator filter — aka, lens flare	

• CIDetector — Class (not a filter) to find features in images.
Currently only supports face finding (returned as an array of
CIFeatures). Optionally detects smiles and eye blinks within
faces.	

• CIImage has a red-eye enhancement that takes the array of
face CIFeatures to tell it where to apply the effect
Core Image on OS X
• Core Image is part of QuartzCore (or Image Kit), so
you don’t @import CoreImage	

• Many more filters are available	

• Can create your own filter with OpenGL Shading
Language (plus some CI extensions). See CIKernel.	

• Also available in iOS 8	

• Filters can be set on CALayers
CALayer Filters on OS X
• Views must be layer-backed (obviously)	

• Must also call -[NSView
setLayerUsesCoreImageFilters:] on 10.9+	

• CALayer has properties: filters, compositingFilter,
backgroundFilters, minificationFilter,
magnificationFilter	

• These exist on iOS, but do nothing
Demo
Adding CIPixellate to layer’s
filters
self
! !! ! ! ! ! ! ! !
self
[self
! !! ! ! ! ! [
! !! ! ! ! ! ! ! !
!
[self
! !! ! !
! !! ! ! ! ! ! ! ! !
self
! !! ! !
Updating a layer’s filters
-(void
! [
! !! ! ! !
! !! ! ! ! ! ! ! ! ! !
! !! ! ! !
}
Wrap Up: Stuff to Remember
• Get psyched about filters, but remember to check
that they’re on your targeted platform/version.	

• Drawing to a CIContext on iOS must be GL-
backed (e.g., with a GLKView)
Q&A
Slides and code will be posted to:	

http://www.slideshare.net/invalidname/
!
@invalidname	

http://subfurther.com/blog

Weitere ähnliche Inhalte

Andere mochten auch

Andere mochten auch (16)

Advanced Imaging on iOS
Advanced Imaging on iOSAdvanced Imaging on iOS
Advanced Imaging on iOS
 
Get On The Audiobus (CocoaConf Atlanta, November 2013)
Get On The Audiobus (CocoaConf Atlanta, November 2013)Get On The Audiobus (CocoaConf Atlanta, November 2013)
Get On The Audiobus (CocoaConf Atlanta, November 2013)
 
Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)
Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)
Building A Streaming Apple TV App (CocoaConf San Jose, Nov 2016)
 
Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)
Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)
Building A Streaming Apple TV App (CocoaConf DC, Sept 2016)
 
Video Killed the Rolex Star (CocoaConf Columbus, July 2015)
Video Killed the Rolex Star (CocoaConf Columbus, July 2015)Video Killed the Rolex Star (CocoaConf Columbus, July 2015)
Video Killed the Rolex Star (CocoaConf Columbus, July 2015)
 
Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...
Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...
Firebase: Totally Not Parse All Over Again (Unless It Is) (CocoaConf San Jose...
 
Get On The Audiobus (CocoaConf Boston, October 2013)
Get On The Audiobus (CocoaConf Boston, October 2013)Get On The Audiobus (CocoaConf Boston, October 2013)
Get On The Audiobus (CocoaConf Boston, October 2013)
 
Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)
Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)
Video Killed the Rolex Star (CocoaConf San Jose, November, 2015)
 
Introduction to the Roku SDK
Introduction to the Roku SDKIntroduction to the Roku SDK
Introduction to the Roku SDK
 
Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)Firebase: Totally Not Parse All Over Again (Unless It Is)
Firebase: Totally Not Parse All Over Again (Unless It Is)
 
Gpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and MetalGpu Programming With GPUImage and Metal
Gpu Programming With GPUImage and Metal
 
Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...
Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...
Revenge of the 80s: Cut/Copy/Paste, Undo/Redo, and More Big Hits (CocoaConf C...
 
Stupid Video Tricks
Stupid Video TricksStupid Video Tricks
Stupid Video Tricks
 
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is FineForward Swift 2017: Media Frameworks and Swift: This Is Fine
Forward Swift 2017: Media Frameworks and Swift: This Is Fine
 
Image Processing Basics
Image Processing BasicsImage Processing Basics
Image Processing Basics
 
Introduction to digital image processing
Introduction to digital image processingIntroduction to digital image processing
Introduction to digital image processing
 

Ähnlich wie Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)

Ähnlich wie Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014) (20)

Core Image
Core ImageCore Image
Core Image
 
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextHi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
 
Animation in iOS
Animation in iOSAnimation in iOS
Animation in iOS
 
Core Animation
Core AnimationCore Animation
Core Animation
 
COMP 4026 Lecture 5 OpenFrameworks and Soli
COMP 4026 Lecture 5 OpenFrameworks and SoliCOMP 4026 Lecture 5 OpenFrameworks and Soli
COMP 4026 Lecture 5 OpenFrameworks and Soli
 
Demystifying Angular Animations
Demystifying Angular AnimationsDemystifying Angular Animations
Demystifying Angular Animations
 
Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»Сергій Міськів, «SwiftUI: Animations»
Сергій Міськів, «SwiftUI: Animations»
 
Core animation
Core animationCore animation
Core animation
 
Starting Core Animation
Starting Core AnimationStarting Core Animation
Starting Core Animation
 
Working with Cocoa and Objective-C
Working with Cocoa and Objective-CWorking with Cocoa and Objective-C
Working with Cocoa and Objective-C
 
Image processing for robotics
Image processing for roboticsImage processing for robotics
Image processing for robotics
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev intro
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone development
 
Before Going Vector
Before Going VectorBefore Going Vector
Before Going Vector
 
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual MachineDino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
Dino2 - the Amazing Evolution of the VA Smalltalk Virtual Machine
 
MNT2014: Mobile Hibrido com Phonegap
MNT2014: Mobile Hibrido com PhonegapMNT2014: Mobile Hibrido com Phonegap
MNT2014: Mobile Hibrido com Phonegap
 
What is image in Swift?/はるふ
What is image in Swift?/はるふWhat is image in Swift?/はるふ
What is image in Swift?/はるふ
 
Open Cv Tutorial Ii
Open Cv Tutorial IiOpen Cv Tutorial Ii
Open Cv Tutorial Ii
 
Open Cv Tutorial Ii
Open Cv Tutorial IiOpen Cv Tutorial Ii
Open Cv Tutorial Ii
 
Top 10 HTML5 features
Top 10 HTML5 featuresTop 10 HTML5 features
Top 10 HTML5 features
 

Mehr von Chris Adamson

iOS Media APIs (MobiDevDay Detroit, May 2013)
iOS Media APIs (MobiDevDay Detroit, May 2013)iOS Media APIs (MobiDevDay Detroit, May 2013)
iOS Media APIs (MobiDevDay Detroit, May 2013)
Chris Adamson
 
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Core Audio in iOS 6 (CocoaConf San Jose, April 2013) Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Chris Adamson
 
Core Audio in iOS 6 (CocoaConf DC, March 2013)
Core Audio in iOS 6 (CocoaConf DC, March 2013)Core Audio in iOS 6 (CocoaConf DC, March 2013)
Core Audio in iOS 6 (CocoaConf DC, March 2013)
Chris Adamson
 
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Chris Adamson
 

Mehr von Chris Adamson (14)

Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)
Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)
Whatever Happened to Visual Novel Anime? (AWA/Youmacon 2018)
 
Whatever Happened to Visual Novel Anime? (JAFAX 2018)
Whatever Happened to Visual Novel Anime? (JAFAX 2018)Whatever Happened to Visual Novel Anime? (JAFAX 2018)
Whatever Happened to Visual Novel Anime? (JAFAX 2018)
 
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
Media Frameworks Versus Swift (Swift by Northwest, October 2017)Media Frameworks Versus Swift (Swift by Northwest, October 2017)
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
 
Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...
Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...
Fall Premieres: Media Frameworks in iOS 11, macOS 10.13, and tvOS 11 (CocoaCo...
 
CocoaConf Chicago 2017: Media Frameworks and Swift: This Is Fine
CocoaConf Chicago 2017: Media Frameworks and Swift: This Is FineCocoaConf Chicago 2017: Media Frameworks and Swift: This Is Fine
CocoaConf Chicago 2017: Media Frameworks and Swift: This Is Fine
 
Glitch-Free A/V Encoding (CocoaConf Boston, October 2013)
Glitch-Free A/V Encoding (CocoaConf Boston, October 2013)Glitch-Free A/V Encoding (CocoaConf Boston, October 2013)
Glitch-Free A/V Encoding (CocoaConf Boston, October 2013)
 
iOS Media APIs (MobiDevDay Detroit, May 2013)
iOS Media APIs (MobiDevDay Detroit, May 2013)iOS Media APIs (MobiDevDay Detroit, May 2013)
iOS Media APIs (MobiDevDay Detroit, May 2013)
 
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Core Audio in iOS 6 (CocoaConf San Jose, April 2013) Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
Core Audio in iOS 6 (CocoaConf San Jose, April 2013)
 
Core Audio in iOS 6 (CocoaConf DC, March 2013)
Core Audio in iOS 6 (CocoaConf DC, March 2013)Core Audio in iOS 6 (CocoaConf DC, March 2013)
Core Audio in iOS 6 (CocoaConf DC, March 2013)
 
Mobile Movies with HTTP Live Streaming (CocoaConf DC, March 2013)
Mobile Movies with HTTP Live Streaming (CocoaConf DC, March 2013)Mobile Movies with HTTP Live Streaming (CocoaConf DC, March 2013)
Mobile Movies with HTTP Live Streaming (CocoaConf DC, March 2013)
 
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
Core Audio in iOS 6 (CocoaConf Chicago, March 2013)
 
Core Audio Intro (Detroit Mobile City 2013)
Core Audio Intro (Detroit Mobile City 2013)Core Audio Intro (Detroit Mobile City 2013)
Core Audio Intro (Detroit Mobile City 2013)
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
Core Audio in iOS 6 (CocoaConf Raleigh, Dec. '12)
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Kürzlich hochgeladen (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)

  • 1. Core Image The Most Fun API You’re Not Using Chris Adamson • @invalidname CocoaConf Columbus, August 2014
  • 2.
  • 3.
  • 4. “Core Image is an image processing and analysis technology designed to provide near real-time processing for still and video images.”
  • 5. Agenda • Images, Filters, and Contexts • The Core Image Filter Gallery • Neat Tricks with Built-In Filters • Core Image on OS X
  • 6. Core Image, Core Concepts • Core Image is of the time • A chain of filters describes a “recipe” of processing steps to be applied to one or more images • “Stringly typed” • You only get pixels when you render
  • 7. Typical Workflow • Start with a source CIImage • Apply one or more filters • Render resulting CIImage to a CIContext, or convert CIImage out to another type • A few filters take or produce types other than CIImage (CIQRCodeGenerator)
  • 8. CIImage • An image provided to or produced by Core Image • But no bitmap of pixel data! • Immutable • -imageByCroppingToRect, -imageByApplyingTransform • -extent — a CGRect of the image’s size
  • 9. CIImage sources • NSURL • CGImageRef • Bitmap or JPEG/PNG/TIFF in NSData • OpenGL texture • Core Video image/pixel buffer
  • 10. CIContext • Rendering destination for a CIImage (- [drawImage:inRect:fromRect:]) • This is where you get pixels (also, this is the processor- intenstive part) • On iOS, must be created from an EAGLContext. On Mac, can be created with CGContextRef • Can also produce output as a CGImageRef, bitmap data, or a CVPixelBuffer (iOS only)
  • 11. ????
  • 12. CIFilter • Performs an image processing operation • Typically takes and produces a CIImage • All parameters are provided via -[setValue:forKey:] • Stringly-typed! • Output is retrieved with -[valueForKey:]
  • 13. –Core Image Cat “I can has filterz?
  • 14. Yes, you can has Filterz!
  • 15.
  • 16. Core Image Filter Reference Filter Name Parameters Note the type & number to provide Categories Watch for CICategoryBuiltIn and CICategoryVideo Example Figure Availability Watch for versioning and OS X-only filters
  • 17. Filter Categories • Group filters by functionality: CICategoryBlur, CICategoryGenerator, CICategoryCompositeOperation, etc. • Also group filters by availability and appropriateness: CICategoryBuiltIn, CICategoryVideo, CICategoryNonSquarePixels
  • 18. CICategoryGenerator • No input image, just produces an output • CICategoryGradient is also output-only • Example: CICheckerboardGenerator
  • 19. CICategoryBlur • Algorithmically spreads/blends pixels • CICategorySharpen offers an opposite effect • Example: CIGaussianBlur
  • 20. CICategoryColorAdjustement • Changes distribution of color throughout an image • Example: CIColorControls (adjusts saturation, brightness, contrast)
  • 21. CICategoryColorEffect • Color changes that affect the subjective nature of the image • Example: CIPhotoEffectNoir
  • 22. CICategoryDistortionEffect • Moves pixels to achieve an effect • Example: CITorusLensDistortion
  • 23. CICategoryStylize • Various stylistic effects • Example: CIPointillize
  • 24. CICategoryGeometryAdjustment • Moves pixels via cropping, affine transforms, etc. • Example: CICrop
  • 25. CICategoryTileEffect • Repeatedly copies all or part of an image • Example: CIAffineTile
  • 26. CICategoryCompositeOperation • Combines multiple images • Example: CISourceOverCompositing
  • 27. Demo
  • 29. Setting input values [self ! ! ! ! ! ! ! ! ! ! ! ! [self ! ! ! ! ! ! ! ! ! ! ! ! [self ! ! ! ! ! ! ! ! ! ! ! !
  • 30. Setting input image CIImage ! ! ! ! ! ! ! ! ! ! [self ! ! ! ! ! ! ! !
  • 31. Getting output image ciImage = [ UIImage self
  • 32. Other output options • Use a CIContext • -[drawImage:inRect:fromRect:] draws pixels to the EAGLContext (iOS) or CGContextRef (OS X) that the CIContext was created from. • CIContext can also render to a void* bitmap • On iOS, can create a CVPixelBufferRef, typically used for writing to a file with AVAssetWriter
  • 33. Chaining filters • Use the output of one filter as the input to the next • This doesn’t cost anything, because the CIImages just hold state, not pixels
  • 34. Demo
  • 35. Creating CIContext if (! ! ! ! ! ! ! ! ! ! ! }! ! ! ! // make CIContext from GL context, clearing out default color space self ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Note: This is in a subclass of GLKView
  • 36. Set up Sepia Tone filter _sepiaToneFilter [_sepiaToneFilter ! ! ! ! ! ! ! ! !
  • 37. Set up Mask to Alpha filter UIImage _circleMaskFilter CIImage ! ! ! ! ! ! ! ! ! ! ! ! circleImageUI. [_circleMaskFilter ! ! ! ! ! ! ! _circleMask circle-mask-100x100.png
  • 38. Set up Blend with Mask filter _constantColorGeneratorFilter ! ! ! ! ! ! ! ! ! ! [_constantColorGeneratorFilter ! ! ! [ ! ! ! ! ! ! ! ! ! ! ! ! ! ! forKey _backgroundAlphaFill ! ! ! ! ! ! ! ! ! ! ! ! _blendWithMaskFilter [_blendWithMaskFilter ! ! ! ! ! ! ! ! [_blendWithMaskFilter ! ! ! ! ! ! ! !
  • 39. Apply filters // Get CIImage from source image CGImageRef ! ! ! ! ! ! ! ! loupeImage = [ ! // Apply sepia filter [self loupeImage = [ ! // Set sepia-filtered image as input to blend-with-mask [_blendWithMaskFilter ! ! ! ! ! ! loupeImage = [
  • 40. Render in CIContext if ([ ! [ }! ! ! [self ! // GL-on-Retina fix CGRect drawBoundsInPoints. drawBoundsInPoints. ! ! // drawing to CIContext draws to the EAGLESContext it's based on [self ! ! ! ! !! ! ! ! ! ! ! ! ! ! ! ! // Refresh GLKView contents immediately [self
  • 41. Working with Video • AVFoundation AVCaptureVideoDataOutput and AVAssetReader deliver CMSampleBuffers • CMSampleBuffers have timing information and CVImageBuffers/CVPixelBuffers • +[CIImage imageWithCVPixelBuffer:]
  • 42. Demo
  • 43. Chroma Key (“green screen” recipe • Use a CIColorCube to map green-ish colors to transparent • Use CISourceOverCompositing to draw this alpha’ed image over another image
  • 44. CIColorCube Maps colors from one RGB “cube” to another http://en.wikipedia.org/wiki/RGB_color_space
  • 45. Using CIColorCube CIColorCube maps green(-ish) colors to 0.0 alpha, all other colors pass through
  • 47. CIColorCube Dataconst unsigned int size = 64;! size_t cubeDataSize = size * size * size * sizeof (float) * 4;! float *keyCubeData = (float *)malloc (cubeDataSize);! //! float *alphaMatteCubeData = (float *)malloc (cubeDataSize);! //! float rgb[3], hsv[3], *keyC = keyCubeData, *alphaC = alphaMatteCubeData;! float rgb[3], hsv[3], *keyC = keyCubeData;! // Populate cube with a simple gradient going from 0 to 1! for (int z = 0; z < size; z++){! ! rgb[2] = ((double)z)/(size-1); // Blue value! ! for (int y = 0; y < size; y++){! ! ! rgb[1] = ((double)y)/(size-1); // Green value! ! ! for (int x = 0; x < size; x ++){! ! ! ! rgb[0] = ((double)x)/(size-1); // Red value! ! ! ! ! // Convert RGB to HSV! ! ! ! // You can find publicly available rgbToHSV functions on the Internet! ! ! ! ! RGBtoHSV(rgb[0], rgb[1], rgb[2],! ! ! ! ! ! &hsv[0], &hsv[1], &hsv[2]);! ! ! ! ! // RGBtoHSV uses 0 to 360 for hue, while UIColor (used above) uses 0 to 1.! ! ! ! hsv[0] /= 360.0;! ! ! ! ! ! ! ! // Use the hue value to determine which to make transparent! ! ! ! // The minimum and maximum hue angle depends on! ! ! ! // the color you want to remove! ! ! ! ! ! ! ! bool keyed = (hsv[0] > minHueAngle && hsv[0] < maxHueAngle) &&! ! ! ! (hsv[1] > minSaturation && hsv[1] < maxSaturation) &&! ! ! ! (hsv[2] > minBrightness && hsv[2] < maxBrightness);! ! ! ! ! ! ! ! float alpha = keyed ? 0.0f : 1.0f;! ! ! ! ! ! ! ! // re-calculate c pointer! ! ! ! keyC = (((z * size * size) + (y * size) + x) * sizeof(float)) + keyCubeData;! ! ! ! ! ! ! ! // Calculate premultiplied alpha values for the cube! ! ! ! keyC[0] = rgb[0] * alpha;! ! ! ! keyC[1] = rgb[1] * alpha;! ! ! ! keyC[2] = rgb[2] * alpha;! ! ! ! keyC[3] = alpha;! ! ! ! ! ! ! ! ! ! ! }! ! }! }! See “Chroma Key Filter Recipe” in Core Image Programming Guide
  • 48. Create CIColorCube from mapping data // build the color cube filter and set its data to above! self.colorCubeFilter = [CIFilter filterWithName:@"CIColorCube"];! [self.colorCubeFilter setValue:[NSNumber numberWithInt:size]! ! !! ! ! ! ! ! ! ! forKey:@"inputCubeDimension"];! NSData *data = [NSData dataWithBytesNoCopy:keyCubeData! ! !! ! ! ! ! ! ! length:cubeDataSize! ! !! ! ! ! ! freeWhenDone:YES];! [self.colorCubeFilter setValue:data forKey:@"inputCubeData"];!
  • 49. Create CISourceOverCompositing // source over filter! self.backgroundImage = [UIImage imageNamed:! ! !! ! ! ! ! ! ! ! ! @"img_washington_small_02.jpg"]; ! self.backgroundCIImage = [CIImage imageWithCGImage:! ! !! ! ! ! ! ! ! ! ! ! self.backgroundImage.CGImage];! self.sourceOverFilter = [CIFilter filterWithName:! ! !! ! ! ! ! ! ! ! ! @"CISourceOverCompositing"];! [self.sourceOverFilter setValue:self.backgroundCIImage ! ! !! ! ! ! ! ! ! forKeyPath:@"inputBackgroundImage"];!
  • 50. Apply Filters in Capture Callback CIImage *bufferCIImage = [CIImage imageWithCVPixelBuffer:cvBuffer];! ! [self.colorCubeFilter setValue:bufferCIImage ! ! !! ! ! ! ! ! ! ! ! forKey:kCIInputImageKey];! CIImage *keyedCameraImage = [self.colorCubeFilter valueForKey:! ! !! ! ! ! ! ! ! ! ! ! ! kCIOutputImageKey];! ! [self.sourceOverFilter setValue:keyedCameraImage ! ! !! ! ! ! ! ! ! ! forKeyPath:kCIInputImageKey];! ! CIImage *compositedImage = [self.sourceOverFilter valueForKeyPath:! ! !! ! ! ! ! ! ! ! ! ! kCIOutputImageKey]; Then draw compositedImage to CIContext as before
  • 51. Other Points of Interest • CIQRCodeGenerator filter — Converts data (e.g., a string) to a QR Code • CILenticularHaloGenerator filter — aka, lens flare • CIDetector — Class (not a filter) to find features in images. Currently only supports face finding (returned as an array of CIFeatures). Optionally detects smiles and eye blinks within faces. • CIImage has a red-eye enhancement that takes the array of face CIFeatures to tell it where to apply the effect
  • 52. Core Image on OS X • Core Image is part of QuartzCore (or Image Kit), so you don’t @import CoreImage • Many more filters are available • Can create your own filter with OpenGL Shading Language (plus some CI extensions). See CIKernel. • Also available in iOS 8 • Filters can be set on CALayers
  • 53. CALayer Filters on OS X • Views must be layer-backed (obviously) • Must also call -[NSView setLayerUsesCoreImageFilters:] on 10.9+ • CALayer has properties: filters, compositingFilter, backgroundFilters, minificationFilter, magnificationFilter • These exist on iOS, but do nothing
  • 54. Demo
  • 55. Adding CIPixellate to layer’s filters self ! !! ! ! ! ! ! ! ! self [self ! !! ! ! ! ! [ ! !! ! ! ! ! ! ! ! ! [self ! !! ! ! ! !! ! ! ! ! ! ! ! ! self ! !! ! !
  • 56. Updating a layer’s filters -(void ! [ ! !! ! ! ! ! !! ! ! ! ! ! ! ! ! ! ! !! ! ! ! }
  • 57. Wrap Up: Stuff to Remember • Get psyched about filters, but remember to check that they’re on your targeted platform/version. • Drawing to a CIContext on iOS must be GL- backed (e.g., with a GLKView)
  • 58. Q&A Slides and code will be posted to: http://www.slideshare.net/invalidname/ ! @invalidname http://subfurther.com/blog