gpt4 book ai didi

iphone - iOS CVImageBuffer 使用 AVCaptureSessionPresetPhoto 从 AVCaptureSessionDataOutput 变形

转载 作者:IT王子 更新时间:2023-10-29 07:59:28 25 4
gpt4 key购买 nike

在高层次上,我创建了一个应用程序,让用户可以将他或她的 iPhone 摄像头指向四周,并查看经过视觉效果处理的视频帧。此外,用户可以点击一个按钮将当前预览的定格画面作为高分辨率照片保存在他们的 iPhone 库中。

为此,应用程序遵循以下过程:

1) 创建一个AVCaptureSession

captureSession = [[AVCaptureSession alloc] init];
[captureSession setSessionPreset:AVCaptureSessionPreset640x480];

2) 使用后置摄像头连接 AVCaptureDeviceInput。

videoInput = [[[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error] autorelease];
[captureSession addInput:videoInput];

3) 将 AVCaptureStillImageOutput 连接到 session ,以便能够以照片分辨率捕获静止帧。

stillOutput = [[AVCaptureStillImageOutput alloc] init];
[stillOutput setOutputSettings:[NSDictionary
dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[captureSession addOutput:stillOutput];

4) 将 AVCaptureVideoDataOutput 连接到 session ,以便能够以较低的分辨率捕获单个视频帧 (CVImageBuffers)

videoOutput = [[AVCaptureVideoDataOutput alloc] init];
[videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
[videoOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
[captureSession addOutput:videoOutput];

5) 当视频帧被捕获时,委托(delegate)的方法被作为 CVImageBuffer 的每个新帧调用:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
[self.delegate processNewCameraFrame:pixelBuffer];
}

6) 然后代理处理/绘制它们:

- (void)processNewCameraFrame:(CVImageBufferRef)cameraFrame {
CVPixelBufferLockBaseAddress(cameraFrame, 0);
int bufferHeight = CVPixelBufferGetHeight(cameraFrame);
int bufferWidth = CVPixelBufferGetWidth(cameraFrame);

glClear(GL_COLOR_BUFFER_BIT);

glGenTextures(1, &videoFrameTexture_);
glBindTexture(GL_TEXTURE_2D, videoFrameTexture_);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));

glBindBuffer(GL_ARRAY_BUFFER, [self vertexBuffer]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, [self indexBuffer]);

glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, BUFFER_OFFSET(0));

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
[[self context] presentRenderbuffer:GL_RENDERBUFFER];

glDeleteTextures(1, &videoFrameTexture_);

CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
}

这一切都有效并导致了正确的结果。我可以看到通过 OpenGL 处理的 640x480 视频预览。它看起来像这样:

640x480 Correct Preview

但是,如果我从该 session 中捕获静止图像,其分辨率也将是 640x480。我希望它是高分辨率的,所以在第一步中我将预设行更改为:

[captureSession setSessionPreset:AVCaptureSessionPresetPhoto];

这会以 iPhone4 的最高分辨率 (2592x1936) 正确捕获静止图像。

但是,视频预览(代表在步骤 5 和 6 中收到)现在看起来像这样:

Photo preview incorrect

我已确认所有其他预设(高、中、低、640x480 和 1280x720)都能按预期预览。但是,照片预设似乎以不同的格式发送缓冲区数据。

我还通过获取缓冲区并从中创建 UIImage 而不是将其发送到 openGL 来确认发送到照片预设缓冲区的数据实际上是有效的图像数据:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(cameraFrame), bufferWidth, bufferHeight, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage *anImage = [UIImage imageWithCGImage:cgImage];

这显示了一个未失真的视频帧。

我已经做了很多搜索,但似乎无法修复它。我的直觉是这是一个数据格式问题。也就是说,我相信缓冲区设置正确,但使用的是这一行无法理解的格式:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bufferWidth, bufferHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, CVPixelBufferGetBaseAddress(cameraFrame));

我的直觉是,将外部格式从 GL_BGRA 更改为其他格式会有所帮助,但它没有...并且通过各种方式看起来缓冲区实际上在 GL_BGRA 中。

有人知道这是怎么回事吗?或者您对我如何调试发生这种情况的原因有什么建议吗? ( super 奇怪的是,这发生在 iphone4 而不是 iPhone 3GS ... 都运行 ios4.3)

最佳答案

这太棒了。

正如 Lio Ben-Kereth 指出的那样,您可以从调试器中看到填充为 48

(gdb) po pixelBuffer
<CVPixelBuffer 0x2934d0 width=852 height=640 bytesPerRow=3456 pixelFormat=BGRA
# => 3456 - 852 * 4 = 48

OpenGL 可以弥补这一点,但 OpenGL ES 不能(更多信息在这里 openGL SubTexturing )

下面是我在 OpenGL ES 中的做法:

(CVImageBufferRef)pixelBuffer   // pixelBuffer containing the raw image data is passed in

/* ... */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, videoFrameTexture_);

int frameWidth = CVPixelBufferGetWidth(pixelBuffer);
int frameHeight = CVPixelBufferGetHeight(pixelBuffer);

size_t bytesPerRow, extraBytes;

bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
extraBytes = bytesPerRow - frameWidth*4;

GLubyte *pixelBufferAddr = CVPixelBufferGetBaseAddress(pixelBuffer);

if ( [[captureSession sessionPreset] isEqualToString:@"AVCaptureSessionPresetPhoto"] )
{

glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL );

for( int h = 0; h < frameHeight; h++ )
{
GLubyte *row = pixelBufferAddr + h * (frameWidth * 4 + extraBytes);
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, h, frameWidth, 1, GL_BGRA, GL_UNSIGNED_BYTE, row );
}
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, frameWidth, frameHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixelBufferAddr);
}

之前,我使用 AVCaptureSessionPresetMedium 并获得 30fps。在 AVCaptureSessionPresetPhoto 中,我在 iPhone 4 上获得了 16fps。子纹理的循环似乎不会影响帧速率。

我在 iOS 5 上使用 iPhone 4。

关于iphone - iOS CVImageBuffer 使用 AVCaptureSessionPresetPhoto 从 AVCaptureSessionDataOutput 变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6540710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com