首页 > 代码库 > ios OpenCv的配置和人脸识别技术
ios OpenCv的配置和人脸识别技术
作为一个好奇心非常重的人,面对未知的世界都想去一探到底。
于是做了个人脸识别的demo。
眼下国内的关于opencv技术文章非常少。都是互相抄袭。关键是抄个一小部分还不全。时间又是非常久之前的了,和如今的一些东西对不上。
没事,我是个实在人,啥也不多说,直接上開始。期间參考了国内很多opencv的文章,代码部分參考http://m.blog.csdn.net/blog/u013810454/27868973。大家能够查看。只是他那个项目下载下来有问题。
我这个融合了全部的长处,更加全面。从配置到使用。
首先我们来配置opencv在xcodeproject。
1.opencv官网下载ios下的框架,先把opencv2.framework下载下来。
然后直接拖到先前创建好的工程中。
然后
然后
如今主要的配置已经完毕。是时候展现真正的技术了。当然别忘记把.m改成.mm。以便使用c++。
#import "ViewController.h" #import <Foundation/Foundation.h> int currentvalue = http://www.mamicode.com/9;"haarcascade_frontalface_alt2" ofType:@"xml"]; CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad([path cStringUsingEncoding:NSASCIIStringEncoding], NULL, NULL, NULL); CvMemStorage* storage = cvCreateMemStorage(0); cvClearMemStorage(storage); //关键部分。使用cvHaarDetectObjects进行检測。得到一系列方框 CvSeq* faces = cvHaarDetectObjects(small_image, cascade, storage ,1.1, currentvalue, CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0), cvSize(0, 0)); NSLog(@"faces:%d",faces->total); //创建画布将人脸部分标记出 CGImageRef imageRef = img.CGImage; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef contextRef = CGBitmapContextCreate(NULL, img.size.width, img.size.height,8, img.size.width * 4,colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault); CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), imageRef); CGContextSetLineWidth(contextRef, 4); CGContextSetRGBStrokeColor(contextRef, 1.0, 0.0, 0.0, 1); //对人脸进行标记 for(int i = 0; i < faces->total; i++) { // Calc the rect of faces CvRect cvrect = *(CvRect*)cvGetSeqElem(faces, i); CGRect face_rect = CGContextConvertRectToDeviceSpace(contextRef, CGRectMake(cvrect.x*scale, cvrect.y*scale , cvrect.width*scale, cvrect.height*scale)); CGContextStrokeRect(contextRef, face_rect); } _imageView.image = [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)]; } } //检測略耗时,开一个新线程来处理吧 -(void)btn { [NSThread detachNewThreadSelector:@selector(opencvFaceDetect) toTarget:self withObject:nil]; } -(void)createButton { UIButton *btn = [[UIButton alloc]init]; btn.backgroundColor = [UIColor redColor]; btn.frame = CGRectMake(0, 100, 30, 30); [btn addTarget:self action:@selector(btn) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } @end
ok,如今能够检測人脸了。
是不是非常奇妙。非常好玩?赶紧动手试一试吧。
ios OpenCv的配置和人脸识别技术
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。