7

我正在使用 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 上发现的与我最密切相关的问题是这个问题,其中 aEAGLContext用于向视频帧添加文本。虽然这实际上适用于我只需要替换单个图像的目标,但我认为如果应用于每个视频帧,此步骤会降低性能,我想知道是否有另一种方法。这里的任何帮助将不胜感激。

4

3 回答 3

17

这是一种CVPixelBufferRef无需使用其他库(如 Core Graphics 或 OpenGL)即可操作 a 的简单方法:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    const int kBytesPerPixel = 4;
    CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
    int bufferWidth = (int)CVPixelBufferGetWidth( pixelBuffer );
    int bufferHeight = (int)CVPixelBufferGetHeight( pixelBuffer );
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow( pixelBuffer );
    uint8_t *baseAddress = CVPixelBufferGetBaseAddress( pixelBuffer );

    for ( int row = 0; row < bufferHeight; row++ )
    {
        uint8_t *pixel = baseAddress + row * bytesPerRow;
        for ( int column = 0; column < bufferWidth; column++ )
        {
            if ((row < 100) && (column < 100) {
                pixel[0] = 255; // BGRA, Blue value
                pixel[1] = 255; // Green value
                pixel[2] = 255; // Red value
            }
            pixel += kBytesPerPixel;
        }
    }

    CVPixelBufferUnlockBaseAddress( pixelBuffer, 0 );

    // Do whatever needs to be done with the pixel buffer
}

这会用白色像素覆盖图像中左上角的 100 x 100 像素块。

我在这个名为RosyWriter的 Apple Developer Example 中找到了这个解决方案。

考虑到这很容易,我在这里没有得到任何答案,有点惊讶。希望这可以帮助某人。

于 2016-05-20T12:17:30.390 回答
7

用 Swift 实现更新它。

        CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
        let bufferWidth = Int(CVPixelBufferGetWidth(pixelBuffer))
        let bufferHeight = Int(CVPixelBufferGetHeight(pixelBuffer))
        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)

        guard let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer) else {
                return
        }

        for row in 0..<bufferHeight {
            var pixel = baseAddress + row * bytesPerRow
            for col in 0..<bufferWidth {
                let blue = pixel
                blue.storeBytes(of: 255, as: UInt8.self)

                let red = pixel + 1
                red.storeBytes(of: 255, as: UInt8.self)

                let green = pixel + 2
                green.storeBytes(of: 255, as: UInt8.self)
             
                let alpha = pixel + 3
                alpha.storeBytes(of: 255, as: UInt8.self)
                
                pixel += 4;
            }
        }

        CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))

由于baseAddressGivesUnsafeMutableRawPointer不支持下标,因此您必须storeBytes改用。这基本上是与上述 Objective-C 版本的唯一关键区别。

于 2019-06-08T02:23:19.483 回答
3

我必须使用 captureOutput 和 CVPixelBuffer 处理来自 iPhone 相机的帧。我用你的代码(谢谢!)在像素缓冲区中以每秒 15 帧的速度循环大约 200k 像素,但我经常遇到丢帧的问题。事实证明,在 Swift 中,while循环比for ... in循环快 10 倍。

像:

0.09 秒:

   for row in 0..<bufferHeight {

        for col in 0..<bufferWidth {
          // process pixels

0.01 秒:

    var x = 0
    var y = 0

    while y < bufferHeight
    {
        y += 1
        x = 0;
        while x < bufferWidth
        {
        // process pixels 
        }
     }
于 2020-04-09T20:27:09.090 回答