【问题标题】:How to capture a portion of AVCaptureSession?如何捕获 AVCaptureSession 的一部分?
【发布时间】:2017-08-22 01:49:28
【问题描述】:

对于我的应用程序,我正在尝试使用 OCR Tesseract 将图像转换为文本。我已经学会了如何截取整个 AVCaptureSession 的屏幕截图,但我只想捕获绿色方块的图像,以使 OCR Tesseract 更容易转换并获得更清晰的用户体验。

我已阅读文章(如下),但它只捕获视图而不是视图后面的 AVCaptureSession。

ios how to capture a particular portion of screen

这里是代码

@interface OCRScannerViewController ()
@property (strong, nonatomic) IBOutlet UIPinchGestureRecognizer *pinchGestureRecognizer;
@property (weak, nonatomic) IBOutlet UIView *cameraView;
@property (weak, nonatomic) IBOutlet VINCaptureView *captureView;
@property (weak, nonatomic) IBOutlet UIImageView *sampleImageView;



@end

@implementation OCRScannerViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    //Start Session
    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }
    [session addInput:input];

    //Output
    AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
    [session addOutput:output];
    output.videoSettings =
    @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    previewLayer.frame = self.cameraView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [previewLayer.connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];

    //Place Camera View behind all subviews
    [self.view.layer insertSublayer:previewLayer atIndex:0];

    //Start capture session
    [session startRunning];

}

- (UIImage *)takeSnapshotOfView:(UIView *)view
{
    UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height));
    [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) afterScreenUpdates:NO];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

【问题讨论】:

    标签: ios xcode ocr


    【解决方案1】:

    您可以使用 CIDetector 检测卡片矩形并从原始图像中裁剪它,然后从卡片图像中裁剪绿色矩形。最后,使用绿色矩形图像进行 OCR。检测和裁剪示例:

    CIImage *ciImage = image.CIImage;
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeRectangle
                                              context:nil
                                              options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh,
                                                        CIDetectorTracking:@YES,
                                                        CIDetectorMinFeatureSize:@.5f}];
    
    NSArray<CIRectangleFeature *> *rectangleFeatures = (NSArray<CIRectangleFeature *> *)[detector featuresInImage:ciImage];
    for (CIRectangleFeature *rect in rectangleFeatures)
    {
        //find a proper rect, like card's width / height = 4:3
        //following procedure is just an example, adjust it to fit your real needs.
        CGFloat width = fabs(rect.topRight.x - rect.topLeft.x);
        CGFloat height = fabs(rect.topLeft.y - rect.bottomLeft.y);
        if ((width / height - 4 / 3) <= 0.1) {
            CIImage *cardImage = [ciImage imageByCroppingToRect:rect.bounds]; //or create a custom rect to crop if it's not good.
            CGRect greenRect = CGRectMake(0, rect.bounds.size.height * 0.8, rect.bounds.size.width, rect.bounds.size.height * 0.2); //in image coordinates
            CIImage *greenRectCIImage = [cardImage imageByCroppingToRect:greenRect];
    
            UIImage *greenRectImage = [[UIImage alloc] initWithCIImage:greenRectCIImage];
            //use greenRectImage for OCR
    
            return;
        }
    }
    

    【讨论】:

    • 绿色框是背景清晰的 UIView。我应该将其更改为具有清晰背景的 UIImage 吗?还能用吗?
    • 对我来说,绿色框只是表示图像中的一个矩形区域,因此视图层次并不重要。
    猜你喜欢
    • 2023-03-19
    • 2012-01-06
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多