前言
在這篇文章中積累CoreImage
的相關(guān)知識(shí)寞宫,這篇文章會(huì)非常的長(zhǎng)。我從18年的5月份開始琢磨無(wú)他相機(jī)及其它美顏相機(jī)的相關(guān)圖像處理技術(shù)拉鹃,斷斷續(xù)續(xù)的了解了一些辈赋,又忘記了一些,為了防止再次遺忘膏燕,還是決定記錄下來(lái)钥屈。圖像處理有很多種方式,比如直接對(duì)原始位圖(Raw Bitmap
)計(jì)算坝辫,使用Core Grahpics
篷就、使用GPUImage
等,最終近忙,我選擇了CoreImage
竭业,這個(gè)框架在視頻圖像上有更加優(yōu)越的性能表現(xiàn),這是我決定仔細(xì)學(xué)習(xí)它的關(guān)鍵及舍。
正文
從簡(jiǎn)單的圖層混合模式說(shuō)起
將A圖片疊加到B圖片上未辆,并將A進(jìn)行灰度處理,且尺寸自定锯玛。
對(duì)這樣的需求咐柜,使用CoreImage
的代碼,實(shí)現(xiàn)方式如下:
UIImage *im = [UIImage imageNamed:@"0"];
UIImage *d = [UIImage imageNamed:@"1"];
CIImage *cibImage = [CIImage imageWithCGImage:d.CGImage];
UIGraphicsBeginImageContext(d.size);
[im drawInRect:CGRectMake(0, 0, 240, 180)];
UIImage *imd = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CIImage *ciaImage = [CIImage imageWithCGImage:imd.CGImage];
CIFilter *garyFilter = [CIFilter filterWithName:@"CIColorControls"];
[garyFilter setValue:@0 forKey:@"inputSaturation"];
[garyFilter setValue:ciaImage forKey:@"inputImage"];
CIImage *outputImage = garyFilter.outputImage;
// CIFilter *alpheF = [CIFilter filterWithName:@"CIColorMatrix"];
// [CIVector vectorWithX:0 Y:0 Z:0.5 W:0];
CIFilter *blendFilter = [CIFilter filterWithName:@"CISourceAtopCompositing"];
[blendFilter setValue:outputImage forKey:@"inputImage"];
[blendFilter setValue:cibImage forKey:@"inputBackgroundImage"];
CIImage *cic = blendFilter.outputImage;
// 6. Render your output image
CIContext * context = [CIContext contextWithOptions:nil];
CGImageRef outputCGImage = [context createCGImage:cic fromRect:[cic extent]];
UIImage * outputImg = [UIImage imageWithCGImage:outputCGImage];
CGImageRelease(outputCGImage);
UIImage *image = [UIImage imageWithCIImage:cic];
疊加效果如下
如果A的灰度圖片需要添加一定的透明度更振,那代碼實(shí)現(xiàn)如下
// CGFloat weights[] = {
// 0, 0, 0.5, 0,
// 0, 0, 0.5, 0,
// 0, 0, 0.5, 0,
// 0, 0, 0.5, 0
// };
// 對(duì)灰度圖片做透明度處理
CIFilter *alpheF = [CIFilter filterWithName:@"CIColorMatrix"];
// CIVector *alpheV = [CIVector vectorWithValues:weights count:9];
CIVector *alpheV = [CIVector vectorWithX:0 Y:0 Z:0.5 W:0];
[alpheF setValue:alpheV forKey:@"inputAVector"];
[alpheF setValue:outputImage forKey:@"inputImage"];
CIImage *alpheImage = alpheF.outputImage;
效果如下
CIVector
是什么炕桨?
舉個(gè)例子:
// 漸變圖片
// set up the parameters for the filter
CIVector *centerVector = [CIVector vectorWithX:150 Y:150];
CIColor *color0 = [CIColor colorWithRed:0 green:0.5 blue:1.0 alpha:1.0];
CIColor *color1 = [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
NSNumber *radius = [NSNumber numberWithFloat:100.0];
// create a CIImage and apply the filter
CIImage *theCIImage = [[CIImage alloc] init];
theCIImage = [CIFilter filterWithName:@"CIGaussianGradient" keysAndValues:@"inputCenter", centerVector, @"inputColor0", color0, @"inputColor1", color1, @"inputRadius", radius, nil].outputImage;
// crop the image using CICrop
CGRect rect = CGRectMake(0.0, 0.0, 600.0, 600.0);
theCIImage = [CIFilter filterWithName:@"CICrop" keysAndValues:kCIInputImageKey, theCIImage, @"inputRectangle", rect, nil].outputImage;
UIImage *image = [UIImage imageWithCIImage:theCIImage];
這種方式可以創(chuàng)建一個(gè)漸變圖片
效果如下:
再舉個(gè)圓角的例子代碼如下:
UIImage *im = [UIImage imageNamed:@"0"];
CIImage *a8 = [CIImage imageWithCGImage:[UIImage imageNamed:@"8"].CGImage];
CIFilter *col = [CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor *cicor = [[CIColor alloc] initWithColor:[UIColor clearColor]];
[col setValue:cicor forKey:@"inputColor"];
CIImage *colorImage = col.outputImage;
CIFilter *gard = [CIFilter filterWithName:@"CIRadialGradient"];
// CIVector
CIVector *center = [CIVector vectorWithX:im.size.width / 2.0 Y:im.size.height / 2.0];
[gard setValue:center forKey:@"inputCenter"];
[gard setValue:@85 forKey:@"inputRadius0"];
[gard setValue:@100 forKey:@"inputRadius1"];
CIImage *gardImage = gard.outputImage;
CIFilter *blend = [CIFilter filterWithName:@"CIBlendWithMask"];
[blend setValue:a8 forKey:@"inputImage"];
[blend setValue:colorImage forKey:@"inputBackgroundImage"];
[blend setValue:gardImage forKey:@"inputMaskImage"];
CGImageRef cgImg = [[CIContext contextWithOptions:nil] createCGImage:blend.outputImage fromRect:a8.extent];
UIImage *image = [UIImage imageWithCGImage:cgImg];
效果:
原圖
處理后
這里有一篇CoreImage
介紹
給視頻添加水印
- (CIImage *)imageWithLogo:(UIImage *)originalImage {
// 疊加商標(biāo)
CIImage *result = [CIImage imageWithCGImage:originalImage.CGImage];
UIImage *logoImage = [UIImage imageNamed:@"ai_video_logo"];
// CGAffineTransformMakeTranslation用來(lái)調(diào)整相對(duì)屏幕的位置
CGFloat h = logoImage.size.height / 667.f * originalImage.size.height;
CGFloat y = 32.f / 667.f * originalImage.size.height;
CIImage *logoCi = [[CIImage imageWithCGImage:logoImage.CGImage] imageByApplyingTransform:CGAffineTransformMakeTranslation(36, originalImage.size.height - y - h)];
CIFilter *logoF = [CIFilter filterWithName:@"CISourceOverCompositing"];
[logoF setValue:logoCi forKey:@"inputImage"];
[logoF setValue:result forKey:@"inputBackgroundImage"];
result = [logoF.outputImage imageByCroppingToRect:result.extent];
return result;
}