版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.01.28 |
前言
Core Image是IOS5中新加入的一個框架,里面提供了強大高效的圖像處理功能十厢,用來對基于像素的圖像進行操作與分析。還提供了很多強大的濾鏡,可以實現(xiàn)你想要的效果麸粮,下面我們就一起解析一下這個框架。感興趣的可以參考上面幾篇镜廉。
1. Core Image框架詳細(xì)解析(一) —— 基本概覽
2. Core Image框架詳細(xì)解析(二) —— Core Image濾波器參考
3. Core Image框架詳細(xì)解析(三) —— 關(guān)于Core Image
4. Core Image框架詳細(xì)解析(四) —— Processing Images處理圖像(一)
5. Core Image框架詳細(xì)解析(五) —— Processing Images處理圖像(二)
6. Core Image框架詳細(xì)解析(六) —— 圖像中的面部識別Detecting Faces in an Image(一)
7. Core Image框架詳細(xì)解析(七) —— 自動增強圖像 Auto Enhancing Images
8. Core Image框架詳細(xì)解析(八) —— 查詢系統(tǒng)中的過濾器 Querying the System for Filters
9. Core Image框架詳細(xì)解析(九) —— 子類化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)建圖像的像素化版本
設(shè)置CIPixellate過濾器的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為包含面部的圖像梗摇。 - 將
inputScale
設(shè)置為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ù)設(shè)置為以下內(nèi)容:
- 將
inputImage
設(shè)置為圖像的像素化版本违诗。 - 將
inputBackgroundImage
設(shè)置為原始圖像。 - 將
inputMaskImage
設(shè)置為合成的綠色圓圈疮蹦。
Pixellate Transition Filter Recipe - Pixellate過渡過濾器Recipe
通過像素化每個圖像從一個圖像轉(zhuǎn)換到另一個圖像诸迟。
要創(chuàng)建一個像素轉(zhuǎn)換濾鏡:
- 使用
CIDissolveTransition
在源圖像和目標(biāo)圖像之間切換。 -
Pixellate
轉(zhuǎn)換過濾器的結(jié)果愕乎。
以下部分顯示如何執(zhí)行每個步驟阵苇。
1. Create a Dissolve Transition - 創(chuàng)建一個Dissolve轉(zhuǎn)換
設(shè)置CIDissolveTransition
過濾器的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為要轉(zhuǎn)換的圖像。 - 將
inputTagetImage
設(shè)置為要轉(zhuǎn)換的圖像妆毕。 - 將
inputTime
設(shè)置為類似于min(max(2 *(time-0.25)慎玖,0),1)
的值笛粘,這是一個斜率函數(shù)趁怔,它夾在兩個值之間。
2. Pixellate the Result of the Transition - 過渡的結(jié)果像素化
設(shè)置CIPixellate
過濾器薪前,通過將其輸入?yún)?shù)設(shè)置如下润努,達到隨著時間的推移改變像素的比例的效果。
- 將
inputImage
設(shè)置為CIDissolveTransition
過濾器的輸出圖像示括。 - 設(shè)置
inputScale
通過提供三角函數(shù)的值隨時間變化:90 *(1-2 * abs(時間-0.5))
铺浇。 - 使用
inputCenter
的默認(rèn)值。
Old Film Filter Recipe - 懷舊過濾Recipe
降低視頻圖像的質(zhì)量垛膝,使其看起來像一個舊的鳍侣,磨損的模擬電影。
要創(chuàng)建一個懷舊效果old-film
的過濾器:
- 將
CISepiaTone
過濾器應(yīng)用于原始視頻圖像吼拥。 - 創(chuàng)建隨機變化的白色斑點倚聚。
- 創(chuàng)建隨機變化的黑暗劃痕。
- 將斑點和劃痕復(fù)合到棕褐色調(diào)的圖像上凿可。
以下部分顯示如何執(zhí)行每個步驟惑折。
1. Apply Sepia to the Video Image - 將棕褐色應(yīng)用于視頻圖像
設(shè)置CISepiaTone
的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為效果要應(yīng)用的視頻圖像。 - 將
inputIntensity
設(shè)置為1.0。
2. Create Randomly Varying White Specks - 創(chuàng)造隨機變化的白色斑點
使用產(chǎn)生彩色噪音的CIRandomGenerator
過濾器惨驶。 它沒有任何輸入?yún)?shù)白热。
要處理噪點,以便只得到白色斑點粗卜,請使用CIColorMatrix
濾鏡屋确,輸入?yún)?shù)設(shè)置如下:
- 將
inputImage
設(shè)置為由隨機生成器生成的輸出。 - 將
inputRVector休建,inputGVector
和inputBVector
設(shè)置為(0,1,0,0)
乍恐。 - 將
inputBiasVector
設(shè)置為(0,0,0,0)
。
通過如下設(shè)置的過濾器的輸入?yún)?shù)测砂,使用CISourceOverCompositing
過濾器將斑點與視頻圖像混合:
- 將
inputImage
設(shè)置為由CIColorMatrix
過濾器生成的白色斑點圖像茵烈。 - 將
inputBackgroundImage
設(shè)置為由CISepiaTone
過濾器生成的圖像。
3. Create Randomly Varying Dark Scratches - 創(chuàng)造隨機變黑暗的劃痕
再次使用CIRandomGenerator
過濾器生成彩色噪音砌些。 然后使用帶有這些輸入?yún)?shù)的CIAffineTransform
過濾器處理其輸出:
- 將
inputImage
設(shè)置為由CIRandomGenerator
過濾器生成的噪聲呜投。 - 將
inputTransform
設(shè)置為scale將x縮放1.5,將y縮放25存璃,這會使像素變粗而長仑荐,但它們?nèi)詫⒈恢?/li>
使用CIAffineTransform
的替代方法是使用imageByApplyingTransform:方法來轉(zhuǎn)換噪聲。
要使像素變暗纵东,請按如下所示設(shè)置CIColorMatrix
濾鏡的輸入?yún)?shù):
- 將
inputImage
設(shè)置為轉(zhuǎn)換的視頻圖像粘招。 - 將
inputRVector
設(shè)置為(4,0,0,0)
。 - 將
inputGVector偎球,inputBVector和inputAVector
設(shè)置為(0,0,0,0)
洒扎。 - 將
inputBiasVector
設(shè)置為(0,1,1,1)
。
這導(dǎo)致青色的劃痕衰絮。
要使劃痕變暗袍冷,請將CIMinimumComponent
過濾器應(yīng)用于青色劃痕。 此濾鏡使用r猫牡,g胡诗,b值的最小值生成灰度圖像。
4. Composite the Specks and Scratches to the Sepia Video Image - 將斑點和劃痕復(fù)合到棕褐色視頻圖像
設(shè)置CIMultiplyCompositing
過濾器的輸入?yún)?shù)如下:
- 設(shè)置
inputBackgroundImage
為處理的視頻圖像(棕褐色調(diào)淌友,白色斑點)煌恢。 - 設(shè)置
inputImage
為黑色劃痕,即來自CIMinimumComponent
過濾器的輸出
后記
本篇已結(jié)束震庭,后面更精彩~~~