0

我正在使用最新的 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);
}

你知道如何放大 aNSData或 aCMSampleBufferRef吗?

4

1 回答 1

0

一种方法是将您的图片放入 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 );
于 2013-02-28T13:11:37.453 回答