当前位置:   article > 正文

CIDetector 边缘识别

cidetectortyperectangle识别不出

CoreImageCIDetector.h自带了四种识别功能

  1. / * 人脸识别 */
  2. CORE_IMAGE_EXPORT NSString* const CIDetectorTypeFace NS_AVAILABLE(10_7, 5_0);
  3. / * 矩形边缘识别 */
  4. CORE_IMAGE_EXPORT NSString* const CIDetectorTypeRectangle NS_AVAILABLE(10_10, 8_0);
  5. /* 二维码识别 */
  6. CORE_IMAGE_EXPORT NSString* const CIDetectorTypeQRCode NS_AVAILABLE(10_10, 8_0);
  7. /* 文本识别 */
  8. #if __OBJC2__
  9. CORE_IMAGE_EXPORT NSString* const CIDetectorTypeText NS_AVAILABLE(10_11, 9_0);
  10. 复制代码

接下来用CIDetectorTypeRectangle对图片的矩形状边缘进行识别,效果图如下 (Demo链接文章底部已给出

部分代码:

  • 初始化一个高精度的识别器
  1. // 高精度边缘识别器
  2. - (CIDetector *)highAccuracyRectangleDetector
  3. {
  4. static CIDetector *detector = nil;
  5. static dispatch_once_t onceToken;
  6. dispatch_once(&onceToken, ^
  7. {
  8. detector = [CIDetector detectorOfType:CIDetectorTypeRectangle context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
  9. });
  10. return detector;
  11. }
  12. 复制代码
  • 调用照相机捕获摄像头图像
  1. NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  2. AVCaptureDevice *device = [possibleDevices firstObject];
  3. if (!device) return;
  4. _imageDedectionConfidence = 0.0;
  5. AVCaptureSession *session = [[AVCaptureSession alloc] init];
  6. self.captureSession = session;
  7. [session beginConfiguration];
  8. self.captureDevice = device;
  9. NSError *error = nil;
  10. AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
  11. session.sessionPreset = AVCaptureSessionPresetPhoto;
  12. [session addInput:input];
  13. AVCaptureVideoDataOutput *dataOutput = [[AVCaptureVideoDataOutput alloc] init];
  14. [dataOutput setAlwaysDiscardsLateVideoFrames:YES];
  15. [dataOutput setVideoSettings:@{(id)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)}];
  16. [dataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
  17. [session addOutput:dataOutput];
  18. self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
  19. [session addOutput:self.stillImageOutput];
  20. AVCaptureConnection *connection = [dataOutput.connections firstObject];
  21. [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
  22. 复制代码
  • 还需要有个显示已捕获图像的容器
  1. self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  2. GLKView *view = [[GLKView alloc] initWithFrame:self.bounds];
  3. view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  4. view.translatesAutoresizingMaskIntoConstraints = YES;
  5. view.context = self.context;
  6. view.contentScaleFactor = 1.0f;
  7. view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
  8. [self insertSubview:view atIndex:0];
  9. _glkView = view;
  10. glGenRenderbuffers(1, &_renderBuffer);
  11. glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
  12. //图像将绘制进_coreImageContext内
  13. _coreImageContext = [CIContext contextWithEAGLContext:self.context];
  14. [EAGLContext setCurrentContext:self.context];
  15. 复制代码
  • 遵循AVCaptureVideoDataOutputSampleBufferDelegate代理,捕获到图像之后,会调用以下方法
  1. -(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
  2. 复制代码

调用CIDetector进行识别,并且获取最大不规则四边形

  1. // 从缓冲区中获取CIImage
  2. CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
  3. CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
  4. // 用高精度边缘识别器 识别特征
  5. NSArray <CIFeature *>*features = [[self highAccuracyRectangleDetector] featuresInImage:image];
  6. // 选取特征列表中最大的不规则四边形
  7. _borderDetectLastRectangleFeature = [self biggestRectangleInRectangles:features];
  8. 复制代码
  • 识别到边缘之后使用CAShapeLayer将边缘绘制并显示
  1. // 绘制边缘检测图层
  2. - (void)drawBorderDetectRectWithImageRect:(CGRect)imageRect topLeft:(CGPoint)topLeft topRight:(CGPoint)topRight bottomLeft:(CGPoint)bottomLeft bottomRight:(CGPoint)bottomRight
  3. {
  4. if (!_rectOverlay) {
  5. _rectOverlay = [CAShapeLayer layer];
  6. _rectOverlay.fillRule = kCAFillRuleEvenOdd;
  7. _rectOverlay.fillColor = [UIColor colorWithRed:73/255.0 green:130/255.0 blue:180/255.0 alpha:0.4].CGColor;
  8. _rectOverlay.strokeColor = [UIColor whiteColor].CGColor;
  9. _rectOverlay.lineWidth = 5.0f;
  10. }
  11. if (!_rectOverlay.superlayer) {
  12. self.layer.masksToBounds = YES;
  13. [self.layer addSublayer:_rectOverlay];
  14. }
  15. // 将图像空间的坐标系转换成uikit坐标系
  16. TransformCIFeatureRect featureRect = [self transfromRealRectWithImageRect:imageRect topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight];
  17. // 边缘识别路径
  18. UIBezierPath *path = [UIBezierPath new];
  19. [path moveToPoint:featureRect.topLeft];
  20. [path addLineToPoint:featureRect.topRight];
  21. [path addLineToPoint:featureRect.bottomRight];
  22. [path addLineToPoint:featureRect.bottomLeft];
  23. [path closePath];
  24. // 背景遮罩路径
  25. UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(-5,
  26. -5,
  27. self.frame.size.width + 10,
  28. self.frame.size.height + 10)];
  29. [rectPath setUsesEvenOddFillRule:YES];
  30. [rectPath appendPath:path];
  31. _rectOverlay.path = rectPath.CGPath;
  32. }
  33. 复制代码

即可显示出实时识别的效果了

  • 最后拍照之后的裁剪,使用该滤镜将识别出的不规则四边形转换成矩形,即可转换成正正方方的矩形了
  1. /// 将任意四边形转换成长方形
  2. - (CIImage *)correctPerspectiveForImage:(CIImage *)image withFeatures:(CIRectangleFeature *)rectangleFeature
  3. {
  4. NSMutableDictionary *rectangleCoordinates = [NSMutableDictionary new];
  5. rectangleCoordinates[@"inputTopLeft"] = [CIVector vectorWithCGPoint:rectangleFeature.topLeft];
  6. rectangleCoordinates[@"inputTopRight"] = [CIVector vectorWithCGPoint:rectangleFeature.topRight];
  7. rectangleCoordinates[@"inputBottomLeft"] = [CIVector vectorWithCGPoint:rectangleFeature.bottomLeft];
  8. rectangleCoordinates[@"inputBottomRight"] = [CIVector vectorWithCGPoint:rectangleFeature.bottomRight];
  9. return [image imageByApplyingFilter:@"CIPerspectiveCorrection" withInputParameters:rectangleCoordinates];
  10. }
  11. 复制代码

// TODO : 识别出边缘之后,可以手动设置边缘范围

Demo地址: github传送门:https://github.com/madaoCN/MADRectDetect 好心人点下Star呗

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/229340
推荐阅读
相关标签
  

闽ICP备14008679号