Get RAW Bayer pixel data from CVPixelBufferRef

I use AVCaptureSession & AVCapturePhotoOutput to capture RAW photos from a device’s camera in kCVPixelFormatType_14Bayer_RGGB format.

I got to the raw photo buffer in the AVCapturePhotoCaptureDelegate :

 func capture(captureOutput: AVCapturePhotoOutput, didFinishProcessingRawPhotoSampleBuffer rawSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { guard let rawSampleBuffer = rawSampleBuffer else { return } guard let pixelBuffer = CMSampleBufferGetImageBuffer(rawSampleBuffer) else { return } } 

Now I am trying to follow the answers to this question in order to get pixel values ​​from CVPixelBufferRef , but I can’t figure out how to do this when using the Bayer RGGB 14-bit pixel format, unlike the 32-bit RGB format mentioned in the answers.

+8
source share
1 answer

First you need to find out the "type of pixel format" of the buffer. This is done using the CVPixelBufferGetPixelFormatType function.

Available types are listed here . In your case, I guess kCVPixelFormatType_14Bayer_BGGR .

Then you need to know how the data is stored. According to What is CVPixelBuffer in iOS? this is

"Bayer 14-bit Little-Endian, packaged in 16-bit, ordered by RGR G ... alternating with GBG B ..."

Then you retrieve the pixels for Uint16 in the same way as they did for Uint8 's Get the pixel value from CVPixelBufferRef in Swift .

0
source

Source: https://habr.com/ru/post/1268238/


All Articles