【发布时间】:2016-08-18 17:25:00
【问题描述】:
我正在使用 iPhone 摄像头捕捉实时视频并将像素缓冲区提供给进行某些对象识别的网络。这是相关代码:(我不会发布设置AVCaptureSession的代码
等等,因为这是相当标准的。)
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
OSType sourcePixelFormat = CVPixelBufferGetPixelFormatType( pixelBuffer );
int doReverseChannels;
if ( kCVPixelFormatType_32ARGB == sourcePixelFormat ) {
doReverseChannels = 1;
} else if ( kCVPixelFormatType_32BGRA == sourcePixelFormat ) {
doReverseChannels = 0;
} else {
assert(false);
}
const int sourceRowBytes = (int)CVPixelBufferGetBytesPerRow( pixelBuffer );
const int width = (int)CVPixelBufferGetWidth( pixelBuffer );
const int fullHeight = (int)CVPixelBufferGetHeight( pixelBuffer );
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
unsigned char* sourceBaseAddr = CVPixelBufferGetBaseAddress( pixelBuffer );
int height;
unsigned char* sourceStartAddr;
if (fullHeight <= width) {
height = fullHeight;
sourceStartAddr = sourceBaseAddr;
} else {
height = width;
const int marginY = ((fullHeight - width) / 2);
sourceStartAddr = (sourceBaseAddr + (marginY * sourceRowBytes));
}
}
然后网络将sourceStartAddr、width、height、sourceRowBytes 和doReverseChannels 作为输入。
我的问题如下:用全白色“像素”替换或删除部分图像数据的最简单和/或最有效的方法是什么?是否可以直接覆盖像素缓冲区数据的e部分,如果可以,如何?
我对这个像素缓冲区的工作原理只有非常基本的了解,所以如果我在这里遗漏了一些非常基本的东西,我深表歉意。我在 Stackoverflow 上发现的与我最密切相关的问题是 this one,其中 EAGLContext 用于向视频帧添加文本。虽然这实际上适用于我只需要替换单个图像的目标,但我认为如果应用于每个视频帧,这一步会降低性能,我想知道是否有另一种方法。如有任何帮助,我们将不胜感激。
【问题讨论】:
标签: ios objective-c avfoundation cvpixelbuffer