本文是结合IOS的Cmera和CIImage中的矩形检测器做的一个识别矩形的demo,对于简单场景效果还不错。复杂场景下检测效果还是差了点,后面会更新结合自己写的opencv算法去检测。
1.首先封装一个camera
/*
* Camera.h
*/
#import <AVFoundation/AVFoundation.h>
@interface Camera : NSObject
@property (strong, nonatomic) AVCaptureDevice *device;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayer;
@property (strong, nonatomic) AVCaptureSession *captureSession;
@property (strong, nonatomic) AVCaptureVideoDataOutput *output;
- (void)addVideoPreviewLayer;
- (void)addVideoInputFromCamera;
- (void)addVideoOutput;
@end
/*
* Camera.m
*/
#import <UIKit/UIKit.h>
#import "Camera.h"
@implementation Camera
- (id)init
{
if ((self = [super init]))
{
[self setCaptureSession:[[AVCaptureSession alloc] init]];
_captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
}
return self;
}
- (void)addVideoPreviewLayer
{
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
[_previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
}
- (void)addVideoInputFromCamera
{
self.device = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] firstObject];
NSError *error =