我正在使用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
作为输入。我的问题是:最简单和/或最有效的方法是什么,可以用所有白色的“像素”替换或删除图像数据的一部分? 是否可能直接覆盖像素缓冲区数据的一部分? 如果是,如何覆盖?
我只有对这个像素缓冲区的非常基础的了解,所以如果我漏掉了一些基本的东西,我很抱歉。我在Stackoverflow上找到的与我的问题最相关的问题是这个问题,其中使用
EAGLContext
将文本添加到视频帧中。虽然这对于我只需要这种替换单个图像的目标来说实际上是可行的,但我认为如果应用于每个视频帧,此步骤会影响性能,我想找出是否有另一种方法。任何关于此的帮助将不胜感激。