Core Image

本文介绍如何使用Core Image框架应用滤镜到图像上,包括创建Core Image上下文、生成图像、设置滤镜参数、获取输出图像以及渲染结果。同时讨论了滤镜类别、属性值类型、创建不同性能级别的Core Image上下文以及从多种来源创建CIImage对象的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • CIFilter is a mutable object that represents an effect. A filter object has at least one input parameter and produces an output image.

  • CIImage is an immutable object that represents an image. You can synthesize image data or provide it from a file or the output of another CIFilter object.

  • CIContext is an object through which Core Image draws the results produced by a filter. A Core Image context can be based on the CPU or the GPU.

The basics of applying a filter to an image

CIContext *context = [CIContext contextWithOptions:nil]; // 1
CIImage *image = [CIImage imageWithContentsOfURL:myURL]; // 2
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"]; // 3
[filter setValue:image forKey:kCIInputImgeKey];
[filter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"InputIntensity"];
CIImage *result = [filter valueForKey:kCIOutputImageKey]; // 4
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]; 



filter category specifies the type of effect—blur, distortion, generator, and so forth—or its intended use—still images, video, nonsquare pixels, and so on. A filter can be a member of more than one category.

Attribute value data types

Data Type

Object

Description

Strings

NSString

Used for such things as display names

Floating-point values

NSNumber

Scalar values such as intensity levels and radii

Vectors

CIVector

Specify positions, areas, and color values; can have 2, 3, or 4 elements, each of which is a floating-point number

Colors

CIColor

Contain color values and a color space in which to interpret the values

Images

CIImage

Lightweight objects that specify image “recipes”

Transforms

NSDataon iOS

NSAffineTransformon OS X

An affine transformation to apply to an image

Creating a Core Image Context

 Methods that create a Core Image context

Context

Renderer

Supported Platform

contextWithOptions:

CPU or GPU

iOS

contextWithCGContext:options:

NSGraphicsContext

CPU or GPU

OS X

contextWithEAGLContext:

contextWithEAGLContext:options:

GPU

iOS

contextWithCGLContext:pixelFormat:options:

contextWithCGLContext:pixelFormat:colorSpace:options:

GPU

OS X

creating a Core Image Context on iOS When You Don’t Need Real-Time Performance

you can create a CIContext object as follows:

CIContext *context = [CIContext contextWithOptions:nil];

Creating a Core Image Context on iOS When You Need Real-Time Performanc

myEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

Creating a CIContext on iOS for real-time performance

    NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
    [options setObject: [NSNull null] forKey: kCIContextWorkingColorSpace];
    myContext = [CIContext contextWithEAGLContext:myEAGLContext options:options];

Creating a CIImage Object

Methods used to create a CIImage object from existing image sources

Image source

Methods

Platform

URL

imageWithContentsOfURL:

imageWithContentsOfURL:options:

iOS, OS X

Quartz 2D image (CGImageRef)

imageWithCGImage:

imageWithCGImage:options:

iOS, OS X

Bitmap data

imageWithBitmapData:bytesPerRow:size:format:colorSpace:

imageWithImageProvider:size:format:colorSpace:options:

iOS, OS X

Encoded data (an image in memory)

imageWithData:

imageWithData:options:

iOS, OS X

CIImageProvider

imageWithImageProvider:size:format:colorSpace:options:

iOS, OS X

OpenGL texture

imageWithTexture:size:flipped:colorSpace:

OS X, iOS 6.0 and later

CVPixelBuffer

imageWithCVPixelBuffer:

imageWithCVPixelBuffer:options:

iOS

CVPixelBuffer

imageWithIOSurface:

imageWithIOSurface:options:

OS X

CVImageBuffer

imageWithCVImageBuffer:

imageWithCVImageBuffer:options:

OS X

Quartz 2D layer (CGLayerRef)

imageWithCGLayer:

imageWithCGLayer:options:

OS X

NSCIImageRep

initWithBitmapImageRep: See NSCIImageRep Class Reference for more information on this AppKit addition.

OS X

Except where noted, iOS methods are available starting with iOS v5.0.

Creating a CIFilter Object and Setting Values

The  filterWithName:  method creates a filter whose type is specified by the  name  argument.Defaults are set for you on iOS but not on OS X.
If you don’t know the input parameters for a filter, you can get an array of them using the method inputKeys
hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setValue: myCIImage forKey: @"inputImage"];
[hueAdjust setValue: [NSNumber numberWithFloat: 2.094]
                    forKey: @"inputAngle"];
or
hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"] keysAndValues:
                         @"inputImage",myCIImage,
                         @"inputAngle, [NSNumber numberWithFloat: 2.094],
                         nil];

Getting the Output Image

You get the output image by retrieving the value for the outputImage key:

CIImage *result = [hueAdjust valueForKey: @"outputImage"];

Rendering the Resulting Output Image

Method

Use

drawImage:inRect:fromRect:

Renders a region of an image to a rectangle in the context destination.

On iOS, this method renders only to aCIContext object that is created withcontextWithEAGLContext:.

On OS X, the dimensions of the destination rectangle are in points if the CIContextobject is created with a CGContextRef. The dimensions are in points if the CIContextobject is created with a CGLContext object.

createCGImage:fromRect:

createCGImage:fromRect:format:colorSpace:

Create a Quartz 2D image from a region of aCIImage object. (iOS and OS X)

render:toBitmap:rowBytes:bounds:format:colorSpace:

Renders a CIImage object into a bitmap. (iOS and OS X)s

createCGLayerWithSize:info:

Creates a CGLayer object renders theCIImage object into the layer. (OSX only)

render:toCVPixelBuffer:

render:toCVPixelBuffer:bounds:colorSpace:

Renders the CIImage object into a Core Video pixel buffer. (iOS only)

[myContext drawImage:result inRect:destinationRect fromRect:contextRect];

Maintaining Thread Safety

CIContext and CIImage objects are immutable, which means each can be shared safely among threads. Multiple threads can use the same GPU or CPU CIContext object to render CIImage objects. However, this is not the case for CIFilter objects, which are mutable. A CIFilter object cannot be shared safely among threads. If your app is multithreaded, each thread must create its own CIFilter objects. Otherwise, your app could behave unexpectedly.

Chaining Filters

 Creating, setting up, and applying a gloom filter

gloom = [CIFilter filterWithName:@"CIGloom"];
[gloom setDefaults]; // 1
[gloom setValue: result forKey: @"inputImage"];
[gloom setValue: [NSNumber numberWithFloat: 25]
                    forKey: @"inputRadius"]; // 2
[gloom setValue: [NSNumber numberWithFloat: 0.75]
                    forKey: @"inputIntensity"]; // 3
result = [gloom valueForKey: @"outputImage"]; 

Detecting Faces

Creating a face detector

CIContext *context = [CIContext contextWithOptions:nil]; // 1
NSDictionary  *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
                                                  forKey:CIDetectorAccuracy]; // 2
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                               context:context
                                               options:opts]; // 3
opts = [NSDictionary dictionaryWithObject:
                      [[myImage properties] valueForKey:kCGImagePropertyOrientation;
                                                 forKey:CIDetectorImageOrientation]]; // 4
NSArray *features = [detector featuresInImage:myImage
                                      options:opts]; // 5
         

Examining face feature bounds

for (CIFaceFeature *f in features)
{
    NSLog(NSStringFromRect(f.bounds));
 
    if (f.hasLeftEyePosition)
        printf("Left eye %g %g\n", f.leftEyePosition.x.
                               f.leftEyePosition.y);
    if (f.hasRightEyePosition)
        printf("Right eye %g %g\n", f.rightEyePosition.x.
                               f.rightEyePosition.y);
    if (f.hasmouthPosition)
        printf("Mouth %g %g\n", f.mouthPosition.x.
                               f.mouthPosition.y);
}














评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值