博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS Core Image之二
阅读量:6088 次
发布时间:2019-06-20

本文共 4236 字,大约阅读时间需要 14 分钟。

在上篇博客IOS Core Image之一中了解了下CIImage、CIFilter、CIContext三个类的使用,这篇了解下滤镜链(多滤镜)和人脸检测(不是人脸识别)。

一、多滤镜

1.有些效果不只是一个滤镜能完成的,需要多个滤镜叠加,让一个滤镜的outputImage作为另一个滤镜的inputImage。

在下面的代码中,给图片加了两个滤镜效果一个高斯模糊一个旋转。

#import "ViewController.h"#import 
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; CIImage *inputImg = [[CIImage alloc]initWithCGImage:[UIImage imageNamed:@"1.jpg"].CGImage]; // 滤镜链合成 CIImage *outputImage = [self oldPhoto:inputImg withAmount:10]; CIContext *context=[CIContext contextWithOptions:nil]; CGImageRef cgimg =[context createCGImage:outputImage fromRect:[outputImage extent]]; UIImageView *img = [[UIImageView alloc] initWithFrame:self.view.bounds]; img.backgroundColor = [UIColor redColor]; img.contentMode = UIViewContentModeScaleToFill; img.image=[UIImage imageWithCGImage:cgimg]; [self.view addSubview:img]; CGImageRelease(cgimg); }-(CIImage *)oldPhoto:(CIImage *)img withAmount:(float)intensity { //高斯模糊滤镜 CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"]; [gaussianBlurFilter setValue:img forKey:@"inputImage"]; [gaussianBlurFilter setValue: @(intensity) forKey:@"inputRadius"]; //旋转滤镜 CIFilter *affineTransformFilter = [CIFilter filterWithName:@"CIAffineTransform"]; [affineTransformFilter setValue:gaussianBlurFilter.outputImage forKey:@"inputImage"]; [affineTransformFilter setValue: [NSValue valueWithCGAffineTransform:CGAffineTransformMakeRotation(intensity)] forKey:@"inputTransform"]; return affineTransformFilter.outputImage;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

2.效果图

二、人脸检测

1.网上有的写的是人脸识别,了解了下发现CoreImage中的算不上人脸识别,也只是检测下人脸的位置,眼睛、嘴巴、是否微笑等,并不能识别出是不是同一个人。这个参考了博客http://blog.csdn.net/wildfireli/article/details/7164628.用自己素颜图(儿童不宜,戴墨镜观看防止亮瞎眼)试了下还算OK。

////  ViewController.m//  CoreImage////  Created by City--Online on 15/11/10.//  Copyright © 2015年 City--Online. All rights reserved.//#import "ViewController.h"#import 
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UIImage* image = [UIImage imageNamed:@"psu.jpg"]; UIImageView *testImage = [[UIImageView alloc] initWithImage: image]; [testImage setTransform:CGAffineTransformMakeScale(1, -1)]; [[[UIApplication sharedApplication] delegate].window setTransform: CGAffineTransformMakeScale(1, -1)]; [testImage setFrame:CGRectMake(0, 0, testImage.image.size.width, testImage.image.size.height)]; [self.view addSubview:testImage]; CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage];// detectorOfType 检测类型 context画布 options字典 CIDetector *detector=[CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; NSArray *features= [detector featuresInImage:ciimage]; // 在 CIFeature类中有CIFaceFeature、CIRectangleFeature、CIQRCodeFeature、CITextFeature子类 for (CIFaceFeature *faceFeature in features) { CGFloat faceWidth = faceFeature.bounds.size.width; if (faceFeature.hasLeftEyePosition) { [self addViewWithPoint:faceFeature.leftEyePosition withWidth:faceWidth]; } if (faceFeature.hasRightEyePosition) { [self addViewWithPoint:faceFeature.rightEyePosition withWidth:faceWidth]; } if (faceFeature.hasMouthPosition) { [self addViewWithPoint:faceFeature.mouthPosition withWidth:faceWidth]; } } }-(void)addViewWithPoint:(CGPoint)point withWidth:(float)faceWidth{ UIView* view = [[UIView alloc] initWithFrame:CGRectMake(point.x-faceWidth*0.1,point.y-faceWidth*0.1, faceWidth*0.2, faceWidth*0.2)]; [view setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.3]]; [view setCenter:point]; view.layer.cornerRadius = faceWidth*0.1; [self.view addSubview:view];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

2.效果图

由于清屏网无耻,抄袭我博客并将我头像放在它网上,我就把这图去了

三、高斯模糊第三方

项目背景视图要用高斯模糊的效果,在网上找了个第三方UIImage+ImageEffects,东西也不多,效果又很好。

你可能感兴趣的文章
hadoop的初始配置
查看>>
jdk在win7下的安装与配置
查看>>
一键PHP环境搭建教程,php本地环境轻松搞定!
查看>>
Spring+Shiro搭建基于Redis的分布式权限系统(有实例)
查看>>
Struts报java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory的解决办法
查看>>
java.util.concurrent.CyclicBarrier组件说明
查看>>
JS控制input只允许输入的各种指定内容
查看>>
Jsoup解析HTML
查看>>
mybatis(五)TypeHandler简介及配置(mybatis源码篇)
查看>>
Python 基础——字符串maketrans(),translate()
查看>>
MyEclipse下CVS的配置
查看>>
Node.js https.server API解析
查看>>
PHPCMS自定义标签,获取全部栏目的最新内容
查看>>
underscore: 竞价算法手记
查看>>
WebQQ更新:用户注册功能
查看>>
怎样才能在MathType中快速引用公式编号
查看>>
_BSMachError: (os/kern) invalid name (15)
查看>>
IDEA14.1 console log4j utf-8乱码
查看>>
SQL使用case when 动态的修改where条件
查看>>
[leetcode] Longest Valid Parentheses
查看>>