版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2018.01.28 |
前言
Core Image是IOS5中新加入的一個框架,里面提供了強大高效的圖像處理功能,用來對基于像素的圖像進行操作與分析。還提供了很多強大的濾鏡,可以實現(xiàn)你想要的效果,下面我們就一起解析一下這個框架。感興趣的可以參考上面幾篇。
1. Core Image框架詳細解析(一) —— 基本概覽
2. Core Image框架詳細解析(二) —— Core Image濾波器參考
3. Core Image框架詳細解析(三) —— 關于Core Image
4. Core Image框架詳細解析(四) —— Processing Images處理圖像(一)
5. Core Image框架詳細解析(五) —— Processing Images處理圖像(二)
6. Core Image框架詳細解析(六) —— 圖像中的面部識別Detecting Faces in an Image(一)
7. Core Image框架詳細解析(七) —— 自動增強圖像 Auto Enhancing Images
8. Core Image框架詳細解析(八) —— 查詢系統(tǒng)中的過濾器 Querying the System for Filters
9. Core Image框架詳細解析(九) —— 子類化CIFilter:自定義效果的配方 Subclassing CIFilter: Recipes for Custom Effects(一)
Anonymous Faces Filter Recipe - 讓面孔無法識別過濾Recipe
在圖像中查找面部并對它們進行像素化,使其無法識別。

要創(chuàng)建一個匿名面孔過濾器:
- 創(chuàng)建圖像的像素化版本。
- 使用在圖像中檢測到的臉部構(gòu)建遮罩。
- 使用蒙版將像素化圖像與原始圖像混合。
以下部分顯示如何執(zhí)行每個步驟。
1. Create a Pixellated version of the image - 創(chuàng)建圖像的像素化版本
設置CIPixellate過濾器的輸入?yún)?shù)如下:
- 將
inputImage設置為包含面部的圖像。 - 將
inputScale設置為max(width, height)/60或其他看起來令人愉快的值,其中寬度和高度是指圖像的寬度和高度。
2. Build a Mask From the Faces Detected in the Image - 從圖像中檢測到的臉部構(gòu)建遮罩
使用CIDetector類來查找圖像中的面部。 對于每一張臉:
- 使用CIRadialGradient過濾器來創(chuàng)建圍繞臉部的圓。
- 使用CISourceOverCompositing過濾器將漸變添加到遮罩。
// Listing 5-5 Building a mask for the faces detected in an image
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:nil];
NSArray *faceArray = [detector featuresInImage:image options:nil];
// Create a green circle to cover the rects that are returned.
CIImage *maskImage = nil;
for (CIFeature *f in faceArray) {
CGFloat centerX = f.bounds.origin.x + f.bounds.size.width / 2.0;
CGFloat centerY = f.bounds.origin.y + f.bounds.size.height / 2.0;
CGFloat radius = MIN(f.bounds.size.width, f.bounds.size.height) / 1.5);
CIFilter *radialGradient = [CIFilter filterWithName:@"CIRadialGradient" withInputParameters:@{
@"inputRadius0": @(radius),
@"inputRadius1": @(radius + 1.0f),
@"inputColor0": [CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0],
@"inputColor1": [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
kCIInputCenterKey: [CIVector vectorWithX:centerX Y:centerY],
}];
CIImage *circleImage = [radialGradient valueForKey:kCIOutputImageKey];
if (nil == maskImage)
maskImage = circleImage;
else
maskImage = [[CIFilter filterWithName:@"CISourceOverCompositing" withInputParameters:@{
kCIInputImageKey: circleImage,
kCIInputBackgroundImageKey: maskImage,
}] valueForKey:kCIOutputImageKey];
}
3. Blend the Pixellated Image, the Mask, and the Original Image - 混合像素化圖像,蒙版和原始圖像
將CIBlendWithMask過濾器的輸入?yún)?shù)設置為以下內(nèi)容:
- 將
inputImage設置為圖像的像素化版本。 - 將
inputBackgroundImage設置為原始圖像。 - 將
inputMaskImage設置為合成的綠色圓圈。
Pixellate Transition Filter Recipe - Pixellate過渡過濾器Recipe
通過像素化每個圖像從一個圖像轉(zhuǎn)換到另一個圖像。

要創(chuàng)建一個像素轉(zhuǎn)換濾鏡:
- 使用
CIDissolveTransition在源圖像和目標圖像之間切換。 -
Pixellate轉(zhuǎn)換過濾器的結(jié)果。
以下部分顯示如何執(zhí)行每個步驟。
1. Create a Dissolve Transition - 創(chuàng)建一個Dissolve轉(zhuǎn)換
設置CIDissolveTransition過濾器的輸入?yún)?shù)如下:
- 將
inputImage設置為要轉(zhuǎn)換的圖像。 - 將
inputTagetImage設置為要轉(zhuǎn)換的圖像。 - 將
inputTime設置為類似于min(max(2 *(time-0.25),0),1)的值,這是一個斜率函數(shù),它夾在兩個值之間。
2. Pixellate the Result of the Transition - 過渡的結(jié)果像素化
設置CIPixellate過濾器,通過將其輸入?yún)?shù)設置如下,達到隨著時間的推移改變像素的比例的效果。
- 將
inputImage設置為CIDissolveTransition過濾器的輸出圖像。 - 設置
inputScale通過提供三角函數(shù)的值隨時間變化:90 *(1-2 * abs(時間-0.5))。 - 使用
inputCenter的默認值。
Old Film Filter Recipe - 懷舊過濾Recipe
降低視頻圖像的質(zhì)量,使其看起來像一個舊的,磨損的模擬電影。

要創(chuàng)建一個懷舊效果old-film的過濾器:
- 將
CISepiaTone過濾器應用于原始視頻圖像。 - 創(chuàng)建隨機變化的白色斑點。
- 創(chuàng)建隨機變化的黑暗劃痕。
- 將斑點和劃痕復合到棕褐色調(diào)的圖像上。
以下部分顯示如何執(zhí)行每個步驟。
1. Apply Sepia to the Video Image - 將棕褐色應用于視頻圖像
設置CISepiaTone的輸入?yún)?shù)如下:
- 將
inputImage設置為效果要應用的視頻圖像。 - 將
inputIntensity設置為1.0。
2. Create Randomly Varying White Specks - 創(chuàng)造隨機變化的白色斑點
使用產(chǎn)生彩色噪音的CIRandomGenerator過濾器。 它沒有任何輸入?yún)?shù)。
要處理噪點,以便只得到白色斑點,請使用CIColorMatrix濾鏡,輸入?yún)?shù)設置如下:
- 將
inputImage設置為由隨機生成器生成的輸出。 - 將
inputRVector,inputGVector和inputBVector設置為(0,1,0,0)。 - 將
inputBiasVector設置為(0,0,0,0)。
通過如下設置的過濾器的輸入?yún)?shù),使用CISourceOverCompositing過濾器將斑點與視頻圖像混合:
- 將
inputImage設置為由CIColorMatrix過濾器生成的白色斑點圖像。 - 將
inputBackgroundImage設置為由CISepiaTone過濾器生成的圖像。
3. Create Randomly Varying Dark Scratches - 創(chuàng)造隨機變黑暗的劃痕
再次使用CIRandomGenerator過濾器生成彩色噪音。 然后使用帶有這些輸入?yún)?shù)的CIAffineTransform過濾器處理其輸出:
- 將
inputImage設置為由CIRandomGenerator過濾器生成的噪聲。 - 將
inputTransform設置為scale將x縮放1.5,將y縮放25,這會使像素變粗而長,但它們?nèi)詫⒈恢?/li>
使用CIAffineTransform的替代方法是使用imageByApplyingTransform:方法來轉(zhuǎn)換噪聲。
要使像素變暗,請按如下所示設置CIColorMatrix濾鏡的輸入?yún)?shù):
- 將
inputImage設置為轉(zhuǎn)換的視頻圖像。 - 將
inputRVector設置為(4,0,0,0)。 - 將
inputGVector,inputBVector和inputAVector設置為(0,0,0,0)。 - 將
inputBiasVector設置為(0,1,1,1)。
這導致青色的劃痕。
要使劃痕變暗,請將CIMinimumComponent過濾器應用于青色劃痕。 此濾鏡使用r,g,b值的最小值生成灰度圖像。
4. Composite the Specks and Scratches to the Sepia Video Image - 將斑點和劃痕復合到棕褐色視頻圖像
設置CIMultiplyCompositing過濾器的輸入?yún)?shù)如下:
- 設置
inputBackgroundImage為處理的視頻圖像(棕褐色調(diào),白色斑點)。 - 設置
inputImage為黑色劃痕,即來自CIMinimumComponent過濾器的輸出
后記
本篇已結(jié)束,后面更精彩~~~
