版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.01.28 |
前言
Core Image是IOS5中新加入的一個框架外恕,里面提供了強(qiáng)大高效的圖像處理功能,用來對基于像素的圖像進(jìn)行操作與分析乡翅。還提供了很多強(qiáng)大的濾鏡,可以實現(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ì)解析(七) —— 自動增強(qiáng)圖像 Auto Enhancing Images
8. Core Image框架詳細(xì)解析(八) —— 查詢系統(tǒng)中的過濾器 Querying the System for Filters
子類化CIFilter:自定義效果的配方
您可以使用一個圖像濾鏡的輸出作為另一個圖像濾鏡的輸入來創(chuàng)建自定義效果,并根據(jù)需要鏈接盡可能多的濾鏡价认。 當(dāng)您通過多次使用這種方式創(chuàng)建效果時嗅定,請考慮繼承CIFilter并封裝過濾器的效果。
本章介紹Core Image如何繼承自CIFilter類并創(chuàng)建為CIColorInvert
濾鏡用踩。 然后它描述了鏈接在一起的各種過濾器的recipes
渠退,以實現(xiàn)有趣的效果。 按照Subclassing CIFilter to Create the CIColorInvert Filter的子類化過程捶箱,您應(yīng)該能夠從本章中的配方創(chuàng)建過濾器智什,或者創(chuàng)建自己感興趣的Core Image提供的內(nèi)置過濾器組合。
Subclassing CIFilter to Create the CIColorInvert Filter - 子類CIFilter創(chuàng)建CIColorInvert過濾器
當(dāng)您對CIFilter
進(jìn)行子類化時丁屎,您可以通過使用預(yù)設(shè)值對其進(jìn)行編碼或?qū)⑺鼈冩溄釉谝黄饋硇薷默F(xiàn)有的過濾器荠锭。 Core Image使用這種技術(shù)實現(xiàn)了一些內(nèi)置的過濾器。
要創(chuàng)建一個過濾器的子類晨川,您需要執(zhí)行以下任務(wù):
聲明過濾器輸入?yún)?shù)的屬性证九。 您必須在每個輸入?yún)?shù)名稱前添加
input
删豺,如inputImage。如有必要愧怜,重寫
setDefaults
方法呀页。 (這個例子中沒有必要,因為輸入?yún)?shù)是設(shè)定值拥坛。)重寫
outputImage
方法蓬蝶。
Core Image提供的CIColorInvert
濾鏡是CIColorMatrix
濾鏡的變體。 顧名思義猜惋,CIColorInvert
將矢量提供給CIColorMatrix
丸氛,以反轉(zhuǎn)輸入圖像的顏色。 按照Listing 5-1和Listing 5-2所示的簡單示例構(gòu)建自己的過濾器著摔。
// Listing 5-1 The interface for the CIColorInvert filter
@interface CIColorInvert: CIFilter {
CIImage *inputImage;
}
@property (retain, nonatomic) CIImage *inputImage;
@end
// Listing 5-2 The outputImage method for the CIColorInvert filter
@implementation CIColorInvert
@synthesize inputImage;
- (CIImage *) outputImage
{
CIFilter *filter = [CIFilter filterWithName:@"CIColorMatrix"
withInputParameters: @{
kCIInputImageKey: inputImage,
@"inputRVector": [CIVector vectorWithX:-1 Y:0 Z:0],
@"inputGVector": [CIVector vectorWithX:0 Y:-1 Z:0],
@"inputBVector": [CIVector vectorWithX:0 Y:0 Z:-1],
@"inputBiasVector": [CIVector vectorWithX:1 Y:1 Z:1],
return filter.outputImage;
}
Chroma Key Filter Recipe - 色度鍵過濾配方
從源圖像中移除顏色或顏色范圍缓窜,然后將源圖像與背景圖像進(jìn)行合成。
創(chuàng)建色度鍵過濾器:
- 創(chuàng)建映射您想要移除的顏色值的數(shù)據(jù)的立方體貼圖谍咆,使其透明(alpha值為0.0)禾锤。
- 使用
CIColorCube
過濾器和立方體貼圖從源圖像中刪除色度鍵顏色。 - 使用
CISourceOverCompositing
過濾器將處理后的源圖像混合到背景圖像上
以下部分顯示如何執(zhí)行每個步驟摹察。
1. Create a Cube Map - 創(chuàng)建一個多維數(shù)據(jù)圖
彩色立方體是3D顏色查找表恩掷。Core Image過濾器CIColorCube
將顏色值作為輸入,并將查找表應(yīng)用于這些值港粱。 CIColorCube
的默認(rèn)查找表是一個單位矩陣螃成,意思是它對提供的數(shù)據(jù)沒有任何作用旦签。但是查坪,這個recipe
要求你從圖像中刪除所有的綠色。 (如果您愿意宁炫,可以移除不同的顏色偿曙。)
你需要通過設(shè)置綠色到alpha = 0.0來清除圖像中的所有綠色,這使得透明羔巢。 “Green”包含一系列的顏色望忆。最直接的方法是將圖像中的顏色值從RGBA轉(zhuǎn)換為HSV值。在HSV中竿秆,hue色調(diào)表示為圍繞圓柱的中心軸的角度启摄。在該表示中,可以將顏色可視化為餅圖切片幽钢,然后簡單地刪除表示色度鍵顏色的切片歉备。
要去除綠色,您需要定義包含綠色色調(diào)的中央通道周圍的最小和最大角度匪燕。那么蕾羊,對于任何綠色的東西喧笔,您將其alpha值設(shè)置為0.0。純綠色的數(shù)值相當(dāng)于120o龟再。最小和最大角度需要以該值為中心书闸。
立方體貼圖數(shù)據(jù)必須預(yù)乘alpha,所以創(chuàng)建立方體貼圖的最后一步是將RGB值乘以剛剛計算的alpha值利凑,對于綠色的色調(diào)為0.0浆劲,否則為1.0。Listing 5-3顯示了如何創(chuàng)建此過濾器recipe
所需的顏色立方體哀澈。
// Listing 5-3 The color cube in code
// Allocate memory
const unsigned int size = 64;
float *cubeData = (float *)malloc (size * size * size * sizeof (float) * 4);
float rgb[3], hsv[3], *c = cubeData;
// 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, hsv);
// Use the hue value to determine which to make transparent
// The minimum and maximum hue angle depends on
// the color you want to remove
float alpha = (hsv[0] > minHueAngle && hsv[0] < maxHueAngle) ? 0.0f: 1.0f;
// Calculate premultiplied alpha values for the cube
c[0] = rgb[0] * alpha;
c[1] = rgb[1] * alpha;
c[2] = rgb[2] * alpha;
c[3] = alpha;
c += 4; // advance our pointer into memory for the next color value
}
}
}
// Create memory with the cube data
NSData *data = [NSData dataWithBytesNoCopy:cubeData
length:cubeDataSize
freeWhenDone:YES];
CIColorCube *colorCube = [CIFilter filterWithName:@"CIColorCube"];
[colorCube setValue:@(size) forKey:@"inputCubeDimension"];
// Set data for cube
[colorCube setValue:data forKey:@"inputCubeData"];
2. Remove green from the source image -
從源圖像中刪除綠色
現(xiàn)在梳侨,您已經(jīng)有了彩色map數(shù)據(jù),將前景圖像(即日丹,要從中去除綠色的圖像)提供給CIColorCube
濾鏡并獲取輸出圖像
[colorCube setValue:myInputImage forKey:kCIInputImageKey];
CIImage *result = [colorCube valueForKey:kCIOutputImageKey];
3. Blend the processed source image over a background image - 將處理的源圖像混合到背景圖像上
設(shè)置CISourceOverCompositing
過濾器的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為從CIColorCube
過濾器生成的圖像走哺。 - 將
inputBackgroundImage
設(shè)置為顯示新背景的圖像。 這個例子使用海灘圖像哲虾。
現(xiàn)在丙躏,前景圖像就好像在沙灘上一樣。
White Vignette for Faces Filter Recipe - 面孔過濾Recipe的白色小插圖
增加圖像中檢測到的臉部周圍圖像的亮度束凑。
要創(chuàng)建一個白色的小插圖過濾器:
- 在圖像中找到人臉晒旅。
使用以臉部為中心的CIRadialGradient
創(chuàng)建基本陰影貼圖。 - 將基本陰影貼圖與原始圖像混合汪诉。
以下部分顯示如何執(zhí)行每個步驟废恋。
1. Find the Face - 找到臉部
使用CIDetector
類在圖像中定位面部。 featuresInImage:options:
返回數(shù)組中的第一個元素扒寄,就是濾波器要作用的面部鱼鼓。 在識別臉部后,從探測器提供的邊界計算臉部的中心该编。 您需要中心值來創(chuàng)建陰影圖迄本。 Listing 5-4顯示了如何使用CIDetector
定位面部。
// Listing 5-4 Using CIDetector to locate one face
CIDetector *detector = [CIDector detectorOfType:CIDetectorTypeFace
context:nil
options:nil];
NSArray *faceArray = [detector featuresInImage:image options:nil];
CIFeature *face = faceArray[0];
CGFloat xCenter = face.bounds.origin.x + face.bounds.size.width/2.0;
CGFloat yCenter = face.bounds.origin.y + face.bounds.size.height/2.0;
CIVector *center = [CIVector vectorWithX:xCenter Y:yCenter];
2. Create a Shade Map - 創(chuàng)建一個陰影圖
使用CIRadialGradient
過濾器創(chuàng)建一個居中于人臉的陰影貼圖课竣。 陰影貼圖的中心應(yīng)該是透明的嘉赎,以便圖像中的面部保持不變。 圖的邊緣應(yīng)該是不透明的白色于樟。 兩者之間的區(qū)域應(yīng)該有不同程度的透明度公条。
要達(dá)到此效果,請將輸入?yún)?shù)設(shè)置為CIRadialGradient
迂曲,如下所示:
- 將
inputRadius0
設(shè)置為大于圖像最長尺寸的值靶橱。 - 將
inputRadius1
設(shè)置為比face更大的值,例如face.bounds.size.height + 50
。 - 將
inputColor0
設(shè)置為不透明的白色抓韩。 - 將
inputColor1
設(shè)置為透明白色纠永。 - 將
inputCenter
設(shè)置為您使用Listing 5-4計算的面部邊界的中心。
3. Blend the Gradient with the Face - 與臉部融合的漸變
設(shè)置CISourceOverCompositing
過濾器的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為原始圖像谒拴。 - 將
inputBackgroundImage
設(shè)置為上一步生成的陰影貼圖尝江。
Tilt-Shift Filter Recipe - 傾斜移位濾波器Recipe
選擇性地聚焦圖像以模擬微型場景。
要創(chuàng)建一個傾斜移位濾鏡:
- 創(chuàng)建圖像的模糊版本英上。
- 創(chuàng)建兩個線性漸變炭序。
- 通過合成線性漸變創(chuàng)建一個蒙版。
- 合成模糊的圖像苍日,蒙版和原始圖像惭聂。
以下部分顯示如何執(zhí)行每個步驟。
1. Create a Blurred Version of the image - 創(chuàng)建圖像的模糊版本
設(shè)置CIGaussianBlur
過濾器的輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為要處理的圖像相恃。 - 將
inputRadius
設(shè)置為10.0(這是默認(rèn)值)辜纲。
2. Create Two Linear Gradients - 創(chuàng)建兩個線性漸變
使用從上到下變化的單一顏色(例如綠色或灰色)創(chuàng)建線性漸變。 設(shè)置CILinearGradient
的輸入?yún)?shù)如下:
- 將
inputPoint0
設(shè)置為(0拦耐,0.75 * h)
- 將
inputColor0
設(shè)置為(0,1,0,1)
- 將
inputPoint1
設(shè)置為(0耕腾,0.5 * h)
- 將
inputColor1
設(shè)置為(0,1,0,0)
創(chuàng)建一個從下到上變化的綠色線性漸變。 設(shè)置CILinearGradient
的輸入?yún)?shù)如下:
- 將
inputPoint0
設(shè)置為(0,0.25 * h)
- 將
inputColor0
設(shè)置為(0,1,0,1)
- 將
inputPoint1
設(shè)置為(0杀糯,0.5 * h)
- 將
inputColor1
設(shè)置為(0,1,0,0)
3. Create a Mask from the Linear Gradients - 從線性漸變創(chuàng)建一個蒙版
要創(chuàng)建一個遮罩扫俺,請按如下方式設(shè)置CIAdditionCompositing
濾鏡的輸入?yún)?shù):
- 將
inputImage
設(shè)置為您創(chuàng)建的第一個線性漸變。 - 將
inputBackgroundImage
設(shè)置為您創(chuàng)建的第二個線性漸變固翰。
4. Combine the Blurred Image, Source Image, and the Gradients - 結(jié)合模糊圖像狼纬,源圖像和漸變
最后一步是使用CIBlendWithMask
過濾器,設(shè)置輸入?yún)?shù)如下:
- 將
inputImage
設(shè)置為圖像的模糊版本骂际。 - 將
inputBackgroundImage
設(shè)置為原始未處理的圖像疗琉。 - 將
inputMaskImage
設(shè)置為蒙版,即組合的漸變方援。
蒙版只會影響圖像的外部没炒。 蒙版的透明部分將通過原始未處理的圖像顯示涛癌。 蒙版的不透明部分允許顯示模糊的圖像犯戏。
后記
本篇已結(jié)束,后面更精彩~~~