CIDetector之图片检测

引言:作为一个开发小白,也注册简书也有一段时间了,但是一直没太浏览过,更别说给大家分享一些经验了,只能没事借鉴别人的经验。突然想到识别图片中的二维码的功能,所以查了一些资料,发现CIDetector这个类可以实现,在这里简单和大家分享一下。

CIDetector 概述

A CIDetector object uses image processing to search for and identify notable features (faces, rectangles, and barcodes) in a still image or video. Detected features are represented by CIFeature objects that provide more information about each feature.

This class can maintain many state variables that can impact performance. So for best performance, reuse CIDetector instances instead of creating new ones.

CIDetector这个类用于识别、检测静止图片或者视频中的显著特征(面部,矩形和条形码),识别的具体特征由CIFeature类去处理。(由于英文水平有限,所有官方文档只做概述,英语好的朋友可以细细研究)

CIFeature 介绍

A CIFeature object represents a portion of an image that a detector believes matches its criteria. Subclasses of CIFeature typically hold additional information specific to the detector that discovered the feature.

CIFeature类只保存基本信息, 所有的附加信息由子类保存。

- CIFaceFeature (人脸识别)

以下是基本属性,相信大家也可以看明白(识别结果中使用的是坐标系和我们常用的有所区别,这个会在后面说明

@property (readonly, assign) BOOL hasLeftEyePosition;

@property (readonly, assign) CGRect bounds;

@property (readonly, assign) CGPoint leftEyePosition;

@property (readonly, assign) BOOL hasRightEyePosition;

@property (readonly, assign) CGPoint rightEyePosition;

@property (readonly, assign) BOOL hasMouthPosition;

@property (readonly, assign) CGPoint mouthPosition;

@property (readonly, assign) BOOL hasTrackingID;

@property (readonly, assign) int trackingID;

@property (readonly, assign) BOOL hasSmile;

@property (readonly, assign) BOOL leftEyeClosed;

@property (readonly, assign) BOOL rightEyeClosed;

这四个属性和视频检测相关,我目前也不知道作何使用。

@property (readonly, assign) BOOL hasTrackingID;

@property (readonly, assign) int trackingID;

@property (readonly, assign) BOOL hasTrackingFrameCount;

@property (readonly, assign) int trackingFrameCount;

- CIQRCodeFeature (二维码识别)

除基本的信息位置之外,只有一个重要信息,messageString。

@property (nullable, readonly) NSString* messageString;

- CIRectangleFeature (矩形识别)

只有基本的位置信息。

- CITextFeature (文本识别)

除基本的信息位置之外,只有一个额外信息,subFeatures。数组中是CITextFeature对象(见后门解释)

@property (nullable, readonly) NSArray *subFeatures;

开始撸代码 (以人脸识别为例)


初始化CIDetector

```

CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];

```

context:用于检测的图形上下文,可不传

options: CIDetectorAccuracy

A key used to specify the desired accuracy for the detector.

指定检测精度 (取值CIDetectorAccuracyHigh :CIDetectorAccuracyLow)

CIDetectorTracking

A key used to enable or disable face tracking for the detector. Use this option when you want to track faces across frames in a video.

是否开启面部追踪(视频中使用)

CIDetectorMinFeatureSize

A key used to specify the minimum size that the detector will recognize as a feature.

The value for this key is an NSNumber object ranging from 0.0 through 1.0 that represents a fraction of the minor dimension of the image.

指定识别要素最大或最小(值0~1,表示次要维度,我也不懂这是什么鬼)

CIDetectorNumberOfAngles

The number of perspectives to use for detecting a face in video input.

The value for this key is an NSNumber object containing the number 1, 3, 5, 7, 9, or 11. At higher numbers of angles, face detection in video becomes more accurate, but at a higher computational cost.

脸部透视数(值为包含1、3、5、7、9、11的NSNumber对象)

开始检测

```

NSArray *features = [detector featuresInImage:(CIImage *)image options:(NSDictionary*)options];

```

image: 一个CIImage对象

options: CIDetectorImageOrientation

An option for the display orientation of the image whose features you want to detect.

The value of this key is an NSNumber object whose value is an integer between 1 and 8. The TIFF and EXIF specifications define these values to indicate where the pixel coordinate origin (0,0) of the image should appear when it is displayed. The default value is 1, indicating that the origin is in the top left corner of the image. For further details, see kCGImagePropertyOrientation.

Core Image detects only faces whose orientation matches that of the image. You should provide a value for this key if you want to detect faces in a different orientation.

用于要检测其要素的图像的显示方向的选项。默认为1,具体参考kCGImagePropertyOrientation

CIDetectorEyeBlink

An option for whether Core Image will perform additional processing to recognize closed eyes in detected faces.

是否执行额外选项处理以识别面部眼部闭合

CIDetectorSmile

An option for whether Core Image will perform additional processing to recognize smiles in detected faces.

是否执行额外选项处理以识别面部微笑

CIDetectorFocalLength

An option identifying the focal length used in capturing images to be processed by the detector.

The value of this key is an NSNumber object whose value is a floating-point number between -1.0 and 1.0. Use this option with the CIDetectorTypeRectangle detector type to fine-tune the accuracy of the detector.

检测中使用的焦距选项值为-1~1,配合CIDetectorTypeRectangle使用

CIDetectorAspectRatio

An option specifying the aspect ratio (width divided by height) of rectangles to search for.

The value of this key is an NSNumber object whose value is a positive floating-point number. Use this option with the CIDetectorTypeRectangle detector type to fine-tune the accuracy of the detector. For example, to more accurately find a business card (3.5 x 2 inches) in an image, specify an aspect ratio of 1.75 (3.5 / 2).

矩形检查的宽高比选项,值为正浮点型,配合CIDetectorTypeRectangle使用

CIDetectorReturnSubFeatures

An option specifying whether to return feature information for components of detected features..

The value of this key is an NSNumber object with a Boolean value. Use this option with the CIDetectorTypeText detector type to choose whether to detect only regions likely to contain text (NO, the default) or to also identify sub-regions likely to contain individual characters of text (YES).

是否返回检测结果组件,与CIDetectorTypeText配合使用,默认为NO,如果设置为YES,则CITextFeature的subFeatures中是单个字符的信息

features:一个包含所有检测结果的数组,数组中为CIFeature子类

坐标系转换:

CIDetector使用的坐标系
我们常用的坐标系


看代码   :

```

CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);

transform = CGAffineTransformTranslate(transform, 0, -image.size.height);

CGPoint point = CGPointApplyAffineTransform(point, transform);

```

这里需要一些基本的数学尝试,通过苹果提供的API进行旋转偏移进行抓换,然后将CIDetector坐标系中的点转为我们常用的坐标系。


提示:人脸检测大家可以使用Face++,或者自己研究OpenCV,我现在正在利用空余时间研究利用OpenCV与OCR技术识别文字,若有进展,再来分享给大家。

感谢您的阅读,如有不足之处还望您能指正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容