raspberrypi / documentation

The official documentation for Raspberry Pi computers and microcontrollers
https://raspberrypi.com/documentation
Creative Commons Attribution Share Alike 4.0 International
5.24k stars 2k forks source link

[libcamera-still]: How to split the raw data up into its red, green, and blue components? #2571

Closed thomaskleiven closed 2 years ago

thomaskleiven commented 2 years ago

Is it possible to split the raw data up into its red, green, and blue components for images obtained with libcamera-still? Ideally, there would be a similar approach as described for the old picamera module (docs).

1) For example, after capturing a raw image using the command: libcamera-still -r -o test.jpg - how can I extract the red, blue and green components from the test.dng file (before any demosaicing)?

2) Which bytes does the bayer data occupy in the test.dng using the 8-megapixel Camera Module v2?

3) Where in the byte stream does the header start / end?

Thanks in advance for any pointers 🥇

lurch commented 2 years ago

ping @davidplowman

davidplowman commented 2 years ago

You would need a software application or library that allows you to read DNG files. Obviously there are a number of raw editor and converter applications, but I don't know if any would support this feature. There are also a number of Python libraries that would allow you to read DNG files and access the data.

Alternatively, maybe you could consider using Picamera2 directly? Something like this might be what you want:

from picamera2 import Picamera2
picam2 = Picamera2()
picam2.configure(picam2.still_configuration(raw={}))
picam2.start()
raw = picam2.capture_array("raw")

Now raw contains the pixel array, and you can find out about the image with

print(picam2.camera_configuration["raw"])
print(raw.shape)

This will tell you the image size, the Bayer order and so on.

thomaskleiven commented 2 years ago

Thanks for the answer! Using picamera2 like you suggest makes sense. Also, I found that rawpy (docs) allows you to extract the red, green and blue component from images captured with the libcamera-still -r -o test.jpg command.