0

Hi I'm working on getting a raw image data from AVCaptureVideoDataoutput.

I have lots of experience in using avfoundation and worked lots of projects but this time I'm working on image processing project which I don't have any experience.

public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {

}

I know I'm getting CMSampleBuffer right here in delegate callback function.

MY Questions.

  1. is CMSampleBuffer is 'RAW' data which is from image sensor?
  2. I want to know whole flow of making CMSampleBuffer to here. But I could not find any detail flow.
  3. I found some possible solutions searching on stackoverflow (Raw image data from camera like "645 PRO")

in this question, the author tried using opencv and got what he wanted. but.. what is exactly different the result by opencv from CMSamplebuffer?(why the opencv result is real raw data the author said)

  1. And also (How to get the Y component from CMSampleBuffer resulted from the AVCaptureSession?)

if i set as below,

if self.session.canAddOutput(self.captureOutput) {
self.session.addOutput(self.captureOutput)
captureOutput.videoSettings = [
    kCVPixelBufferPixelFormatTypeKey : kCVPixelFormatType_32BGRA
] as [String : Any]
captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "capture"))
captureOutput.alwaysDiscardsLateVideoFrames = true
}

by setting format key _32BGRA,, AM i now getting raw data from samplebuffer?

1 Answers1

1
  1. It's not RAW in your case. All modern sensors build on Bayer filter, so you get an image converted from the Bayer format. You can't get raw image with this api. There is a format called kCVPixelFormatType_14Bayer_BGGR, but the camera probably won't support it.

  2. Maybe on WWDC 419 session you will find answer. I don't know

  3. It's the same; cv::Mat is just a wrapper around the image data from CMSampleBuffer. If you save your data as PNG, you will not lose any quality. The TIFF format saves without any compression, but you can also use PNG without compression.

  4. If you use RGBA format, behind the scenes it is converted from Bayer to RGBA. To get the Y channel, you need to additionally apply an RGBA to YUV conversion and take the Y channel. Or you can use the kCVPixelFormatType_420YpCbCr8BiPlanarFullRange format and get the Y channel from the first plane. Also note that VideoRange has different chroma output range.

// ObjC code
int width = CVPixelBufferGetWidth(imageBuffer);
int height = CVPixelBufferGetHeight(imageBuffer);
uint8_t *yBuffer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
size_t yPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);

cv::Mat yChannel(height, width, CV_8UC1, yBuffer, yPitch);
Gralex
  • 4,285
  • 7
  • 26
  • 47
  • Really Thanks a lot :) I mean you are such a great. Your answer gave me a lot of support and things to study for my own. – Hyun Bin CHo Jan 19 '23 at 00:36
  • Since I have not studied Image processing yet(will soon), 1. you told me that if you use a kCVPixelFormatType_14Bayer_BGGR, you can get Bayer data, but the camera won't support which means i think there are no any way to get Bayer format data using avfoundation right? 2. Thanks for the link. 3. I will study about opencv more :). So what does the author got 'REAL RAW' from opencv is not true then? 4. THANKS AGAIN about detain behind the scene. – Hyun Bin CHo Jan 19 '23 at 00:42
  • @HyunBinCHo 1) As I know - Yes. 3) Real raw possible to get with `AVCaptureSessionPresetPhoto` and it work with different API. Do you really need to get raw data? On my current work we use images from camera for processing. For our algorithms it's fine to you chroma channel only. The main challenge for us remove lens distortion and gamma correction. You probably need to read about gamma correction, cause it also change image before you get in output. – Gralex Jan 19 '23 at 08:17
  • Hi Gralex. Since This is national holiday for new year in south korea, I really appreciate what you gave me. And also happy new year! :) + I really want to study deep in Image processing, I guess you are such a great expertise on this subject. I can't think which subject(lecture) I should study to be like you. would you give me any roadmap? – Hyun Bin CHo Jan 22 '23 at 04:03
  • My experience is not as big as you think. I got it while working on an analog of `ARKit` and calibrating a camera. I recommend reading this blog post: https://vas3k.ru/blog/computational_photography/. The English version is broken for now, but you can open it from the web archive: https://web.archive.org/web/20221217170658/https://vas3k.com/blog/computational_photography/. – Gralex Jan 22 '23 at 08:28