【问题标题】:Make zoom on NSData or on CMSampleBufferRef放大 NSData 或 CMSampleBufferRef
【发布时间】:2013-02-11 20:54:29
【问题描述】:

我正在使用最新的 SDK 开发一个 iOS 应用程序。

这个应用程序将与 OpenCV 一起使用,我必须在相机上进行缩放,但是,它在 iOS SDK 上不可用,所以我想以编程方式进行。

我必须对每个视频帧进行“缩放”。这是我必须这样做的地方:

#pragma mark - AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    /*Lock the image buffer*/
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    /*Get information about the image*/
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    //size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer);

    //put buffer in open cv, no memory copied
    cv::Mat image = cv::Mat(height, width, CV_8UC4, baseAddress);
    // copy the image
    //cv::Mat copied_image = image.clone();
    _lastFrame = [NSData dataWithBytes:image.data
                                  length:image.elemSize() * image.total()];

    [DataExchanger postFrame];

    /*We unlock the  image buffer*/
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}

你知道如何在NSDataCMSampleBufferRef 上进行缩放吗?

【问题讨论】:

  • 缩放是为了直接显示图像还是为了对像素进行图像处理,因此,您需要访问缩放后的像素数据吗?另外,您将什么定义为缩放:如果我将 640x480 缩放 2,这是否意味着每个像素都会被复制到它旁边 (x) 和它下面 (y) 的像素?

标签: ios opencv camera zooming avfoundation


【解决方案1】:

一种方法是将您的图片放入 CGImageRef,在该图片中选择一个正方形,然后将其再次绘制为正常大小。像这样的东西(尽管可能有更好的方法):

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);


    CGImageRef smallQuartzImage = CGImageCreateWithImageInRect(quartzImage, CGRectMake(200, 200, 600, 600));

    cv::Mat image(height, width, CV_8UC4 );
    CGContextRef contextRef = CGBitmapContextCreate( image.data, width, height, 8, cvMat.step[0], colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst );
    CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), smallQuartzImage);
    CGContextRelease( contextRef );
    CGColorSpaceRelease( colorSpace );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多