CIDetector之圖片檢測(cè)

引言:作為一個(gè)開(kāi)發(fā)小白禽绪,也注冊(cè)簡(jiǎn)書(shū)也有一段時(shí)間了颜价,但是一直沒(méi)太瀏覽過(guò)桑涎,更別說(shuō)給大家分享一些經(jīng)驗(yàn)了,只能沒(méi)事借鑒別人的經(jīng)驗(yàn)汗唱。突然想到識(shí)別圖片中的二維碼的功能,所以查了一些資料丈攒,發(fā)現(xiàn)CIDetector這個(gè)類(lèi)可以實(shí)現(xiàn)哩罪,在這里簡(jiǎn)單和大家分享一下授霸。

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這個(gè)類(lèi)用于識(shí)別、檢測(cè)靜止圖片或者視頻中的顯著特征(面部际插,矩形和條形碼)碘耳,識(shí)別的具體特征由CIFeature類(lèi)去處理。(由于英文水平有限腹鹉,所有官方文檔只做概述藏畅,英語(yǔ)好的朋友可以細(xì)細(xì)研究)

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類(lèi)只保存基本信息, 所有的附加信息由子類(lèi)保存功咒。

- CIFaceFeature (人臉識(shí)別)

以下是基本屬性愉阎,相信大家也可以看明白(識(shí)別結(jié)果中使用的是坐標(biāo)系和我們常用的有所區(qū)別,這個(gè)會(huì)在后面說(shuō)明

@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;

這四個(gè)屬性和視頻檢測(cè)相關(guān)力奋,我目前也不知道作何使用榜旦。

@property (readonly, assign) BOOL hasTrackingID;

@property (readonly, assign) int trackingID;

@property (readonly, assign) BOOL hasTrackingFrameCount;

@property (readonly, assign) int trackingFrameCount;

- CIQRCodeFeature (二維碼識(shí)別)

除基本的信息位置之外,只有一個(gè)重要信息景殷,messageString溅呢。

@property (nullable, readonly) NSString* messageString;

- CIRectangleFeature (矩形識(shí)別)

只有基本的位置信息。

- CITextFeature (文本識(shí)別)

除基本的信息位置之外猿挚,只有一個(gè)額外信息咐旧,subFeatures。數(shù)組中是CITextFeature對(duì)象(見(jiàn)后門(mén)解釋?zhuān)?br>

@property (nullable, readonly) NSArray *subFeatures;

開(kāi)始擼代碼 (以人臉識(shí)別為例)


初始化CIDetector

```

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

```

context:用于檢測(cè)的圖形上下文绩蜻,可不傳

options: CIDetectorAccuracy

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

指定檢測(cè)精度 (取值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.

是否開(kāi)啟面部追蹤(視頻中使用)

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.

指定識(shí)別要素最大或最邢衬(值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.

臉部透視數(shù)(值為包含1伊约、3、5孕蝉、7屡律、9、11的NSNumber對(duì)象)

開(kāi)始檢測(cè)

```

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

```

image: 一個(gè)CIImage對(duì)象

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.

用于要檢測(cè)其要素的圖像的顯示方向的選項(xiàng)降淮。默認(rèn)為1超埋,具體參考kCGImagePropertyOrientation

CIDetectorEyeBlink

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

是否執(zhí)行額外選項(xiàng)處理以識(shí)別面部眼部閉合

CIDetectorSmile

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

是否執(zhí)行額外選項(xiàng)處理以識(shí)別面部微笑

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.

檢測(cè)中使用的焦距選項(xiàng)值為-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).

矩形檢查的寬高比選項(xiàng)佳鳖,值為正浮點(diǎn)型纳本,配合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).

是否返回檢測(cè)結(jié)果組件,與CIDetectorTypeText配合使用腋颠,默認(rèn)為NO繁成,如果設(shè)置為YES,則CITextFeature的subFeatures中是單個(gè)字符的信息

features:一個(gè)包含所有檢測(cè)結(jié)果的數(shù)組淑玫,數(shù)組中為CIFeature子類(lèi)

坐標(biāo)系轉(zhuǎn)換:

CIDetector使用的坐標(biāo)系
我們常用的坐標(biāo)系


看代碼 ? :

```

CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);

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

CGPoint point = CGPointApplyAffineTransform(point, transform);

```

這里需要一些基本的數(shù)學(xué)嘗試巾腕,通過(guò)蘋(píng)果提供的API進(jìn)行旋轉(zhuǎn)偏移進(jìn)行抓換面睛,然后將CIDetector坐標(biāo)系中的點(diǎn)轉(zhuǎn)為我們常用的坐標(biāo)系。


提示:人臉檢測(cè)大家可以使用Face++尊搬,或者自己研究OpenCV叁鉴,我現(xiàn)在正在利用空余時(shí)間研究利用OpenCV與OCR技術(shù)識(shí)別文字,若有進(jìn)展佛寿,再來(lái)分享給大家幌墓。

感謝您的閱讀,如有不足之處還望您能指正冀泻。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末常侣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子弹渔,更是在濱河造成了極大的恐慌胳施,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,366評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肢专,死亡現(xiàn)場(chǎng)離奇詭異舞肆,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)博杖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)椿胯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人剃根,你說(shuō)我怎么就攤上這事压状。” “怎么了跟继?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,689評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)镣丑。 經(jīng)常有香客問(wèn)我舔糖,道長(zhǎng),這世上最難降的妖魔是什么莺匠? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,925評(píng)論 1 295
  • 正文 為了忘掉前任金吗,我火速辦了婚禮,結(jié)果婚禮上趣竣,老公的妹妹穿的比我還像新娘摇庙。我一直安慰自己,他們只是感情好遥缕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布卫袒。 她就那樣靜靜地躺著,像睡著了一般单匣。 火紅的嫁衣襯著肌膚如雪夕凝。 梳的紋絲不亂的頭發(fā)上宝穗,一...
    開(kāi)封第一講書(shū)人閱讀 51,727評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音码秉,去河邊找鬼逮矛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛转砖,可吹牛的內(nèi)容都是我干的须鼎。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼府蔗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晋控!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起礁竞,我...
    開(kāi)封第一講書(shū)人閱讀 39,349評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤糖荒,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后模捂,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體捶朵,經(jīng)...
    沈念sama閱讀 45,820評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評(píng)論 3 337
  • 正文 我和宋清朗相戀三年狂男,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了综看。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,127評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡岖食,死狀恐怖红碑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泡垃,我是刑警寧澤析珊,帶...
    沈念sama閱讀 35,812評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站蔑穴,受9級(jí)特大地震影響忠寻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜存和,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評(píng)論 3 331
  • 文/蒙蒙 一奕剃、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捐腿,春花似錦纵朋、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,017評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至宪祥,卻和暖如春寺鸥,著一層夾襖步出監(jiān)牢的瞬間猪钮,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,142評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工胆建, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留烤低,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,388評(píng)論 3 373
  • 正文 我出身青樓笆载,卻偏偏與公主長(zhǎng)得像扑馁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子凉驻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容