之前學(xué)習(xí)人臉識(shí)別涩维,使用的是CIDetector,但是發(fā)現(xiàn)這個(gè)API給的開放接口少之又少袁波,看到天天p圖上的武則天變裝等瓦阐,覺得這不是用此法做的蜗侈,進(jìn)而查到了OpenCV,學(xué)習(xí)記錄之:
基于 OpenCV 的人臉識(shí)別:這里有對(duì)OpenCV的基本介紹和用法睡蟋。
CGRect rect = [UIScreen mainScreen].bounds;
self.imageView.frame= rect;
UIImage* image = [UIImage imageNamed:@"robin"];
// Convert UIImage * to cv::Mat
UIImageToMat(image,cvImage);
if(!cvImage.empty()) {
cv::Matgray;
// Convert the image to grayscale;
cv::cvtColor(cvImage, gray,CV_RGBA2GRAY);
// Apply Gaussian filter to remove small edges
cv::GaussianBlur(gray, gray,cv::Size(5,5),1.2,1.2);
// Calculate edges with Canny
cv::Matedges;
cv::Canny(gray, edges,0,60);
// Fill image with white color
cvImage.setTo(cv::Scalar::all(255));
// Change color on edges
cvImage.setTo(cv::Scalar(0,128,255,255), edges);
// Convert cv::Mat to UIImage* and show the resulting image
self.imageView.image=MatToUIImage(cvImage);
}
拷了一份代碼在.m文件里踏幻,由于是混編,要把.m改成.mm戳杀。
導(dǎo)入頭文件:
然后出現(xiàn)了幾個(gè)問題:
1:#import <opencv2/opencv.hpp>
導(dǎo)入此頭文件報(bào)紅该面,拷貝以上保存解決之
2:導(dǎo)入庫(kù)后Undefined symbols for architecture i386:
Problem solved adding several frameworks :CoreVideo.framework,AssetsLibrary.framework,CoreMedia.framework.
添加系統(tǒng)庫(kù)解決:http://stackoverflow.com/questions/23634940/opencv2-framework-not-compile-with-linker-flag-objc/23648133#23648133
3:ld: '/Users/zq/Documents/oc-test/NotePod/NotePod/opencv2.framework/opencv2(cap_avfoundation.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)、
通過把target里面的Enable Bitcode設(shè)置成NO解決:http://stackoverflow.com/questions/30848208/new-warnings-in-ios9