- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在高层次上,我创建了一个应用程序,让用户可以将他或她的 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 视频预览。它看起来像这样:
但是,如果我从该 session 中捕获静止图像,其分辨率也将是 640x480。我希望它是高分辨率的,所以在第一步中我将预设行更改为:
[captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
这会以 iPhone4 的最高分辨率 (2592x1936) 正确捕获静止图像。
但是,视频预览(代表在步骤 5 和 6 中收到)现在看起来像这样:
我已确认所有其他预设(高、中、低、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/
我正在尝试在我的网站上嵌入多个 .svg 文件。只要我使用 Chrome、Firefox 或我目前测试过的任何移动浏览器,一切似乎都运行良好。但是,有一个异常(exception):每当我在 Wind
我正在尝试在我的网站上嵌入多个 .svg 文件。只要我使用 Chrome、Firefox 或我目前测试过的任何移动浏览器,一切似乎都运行良好。但是,有一个异常(exception):每当我在 Wind
是否有某种方法(库或算法)可用于在 java.awt.Shape 或其路径迭代器的两个实例之间进行插值?例如,要在矩形和椭圆之间无缝过渡?或者更一般的 Path2D 情况。 最佳答案 SwingX 中
我试图在动态大小的视频上包含一个 Canvas 元素,该视频将异步加载。在 Canvas 上,用户将能够拖动矩形选择框并调整其大小。 在我的 JS 文件中,我有一个监听器监 window 口,并通过
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。 去
有没有办法通过 GDAL(使用 Python API)使用移位向量来扭曲图像? 通过移位向量,我的意思是例如。包含以下列的 CSV(或 numpy)文件:starting_x,starting_y,t
我正在创建一个导航按钮。当用户按下它时,按钮的图像应该改变,反射(reflect)它的状态(例如菜单打开/关闭)。我决定为此做一个 morphing-liek 动画。可以用CoreAnimation来
我在 Pandas 中有以下示例数据框。如何获取每个 'Id' 的 'label_weight' 值的最大值并将相应的 'label' 列分配给该 'Id' 在新列 'assgined_label'
文本使我的框变形。 这是我的: This text is deforming the "leftOne" 还有 CSS: .leftOne { float: left;
HTML: Home Services About Us Contact Us CSS: ul { pa
我想在这里得到 openCV 爱好者的帮助。 我想知道关于如何变形 2 个面孔的方向(以及一些建议或代码段),以及一种比率,即第一个面孔的 10% 和第二个面孔的 90%。 我见过像 cvWarpAf
我已经搜索了很长时间,但还没有找到真正的答案,但是,也许我的眼睛上有西红柿,但是真的没有针对 python/MATLAB 的框架可以进行面部扭曲/开箱即用? 一个框架,我在其中放入两张带有特征点的图像
根据material.io float 操作按钮可以变身为操作菜单,如 this .有什么方法可以只使用 Material 库(没有第三方库)吗? 我试过了this库,但它会在菜单关闭后根据底部应用栏
这就是我想用我的 NSOpenGLView 做的事情。目前 NSOpenGLView 覆盖了窗口的整个区域,我想在 NSOpenGLView 顶部添加按钮、nsviews 和图像。我浏览了网页,发现
我正在遍历在 Controller 中定义的集合。 我正在使用基础轨道插件将其转变为轮播。 但是我的HTML被弄乱了,并且破坏了插件,因为它期望获得一定的输出。
我不知道如何使用 BufferedImage 使图像变形。有人能帮我吗 ?我绝对绝望了。感谢您的所有提示。对不起我的英语不好。 | |
这个问题的答案似乎相互矛盾,我很困惑在 Core Data 数据库中存储图像的最佳方式是什么。 This question说可变形,但是this question说要使用二进制数据。 如果我只是想把它
您好,我不确定如何处理有关表单的逻辑。所以,表格很大,我知道有 20 多个字段被认为是“不好的做法”,表格应该最少,但这就是客户想要的,所以不用争论,无论如何表格都会接受订单,但有不同的顺序类型(更具
我正在使用 animator() 在我的应用程序的帧( subview )之间横向滚动 NSScrollView。当动画发生并且我调整 NSWindow 的大小时,整个 NSView 会像这样扭曲:
仍在我的太空入侵者克隆上工作,我想在屏幕底部添加可破坏的基地: 我已经弄清楚如何通过让炸弹和盾牌相互接触来修改盾牌的外观,然后在 didBegincontact 中,从炸弹爆炸的 mask 和盾牌的当
我是一名优秀的程序员,十分优秀!