User avatar
schoolpost
Posts: 49
Joined: Sun Feb 19, 2017 10:47 am
Location: Canada
Contact: Website

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 6:06 am

Bryan I'm a little lost as to what you are aiming for.

You want to use the RAW information. You've mentioned DNG aswell. ( what camera version do you have? )

I very recently created a pretty simple to use python script to do this:
https://github.com/schoolpost/pydng

I believe it creates a pretty decent output in terms of color/quality and with the addition
of standard lossless DNG compression you can half the file size at no cost. Really cant get better than that.

The mentioned C tool will pack the RAW into a 16bit DNG. It works with both V1 and V2.
But lacks the correct color matrix and doesn't do any RAW calibration prior to packing it ( lens shading, hot pixel removal )

If you want to use the RAW data, do some debayering and then output into a more standard format you could possibly explore OpenCV debayer aswell.

Not to "inflate" or "hype" myself here, but in this situation I believe my aforementioned script above
will give you the best result if DNG is what you are after. Go ahead and tweak it as you see fit.
I realize too its not the most intuitive to work with, but should be quite workable.

I'm really trying to get the process done right, so feedback is very welcome.
-Csaba Nagy

perrociego
Posts: 14
Joined: Fri Jan 06, 2017 4:21 am

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 6:10 am

Hi Brian:

I tried the second code. It`s outputs is a .ppm without header. You may open it with Photoshop. 8 bits per color.

And I just tryng to understand the first one. Look quite good. It´s clear 16 bits.

8 bit´s vs 16 bit´s. Images wont look the same.
Attachments
Screen Shot 2017-01-28 at 19.44.19 copy.jpg
Photoshop options
Screen Shot 2017-01-28 at 19.44.19 copy.jpg (50.31 KiB) Viewed 3434 times

perrociego
Posts: 14
Joined: Fri Jan 06, 2017 4:21 am

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 6:21 am

Hi schoolpost:

Do you know how long does it take to your code to process an image?

Thanks for sharing

Marcelo

User avatar
schoolpost
Posts: 49
Joined: Sun Feb 19, 2017 10:47 am
Location: Canada
Contact: Website

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 7:52 am

perrociego wrote:Hi schoolpost:

Do you know how long does it take to your code to process an image?

Thanks for sharing

Marcelo
If you want to do a straight 16-bit DNG output it will take a 3-5 seconds.
( there's some processing going on with lens shading, hot pixel mapping. )
Its certainly not C level of speed / efficiency but good enough I would imagine for most applications.

I also have a 10-bit DNG output, that takes considerably longer as my method packing is a very crude, around 23 seconds.
It packs the 10-bit data like this:
AAAAAAAA AABBBBBB BBBBCCCC CCCCCCDD DDDDDDDD
This is needed for the pixel data to be read properly. ( kind of wish the RAW was originally packed this way :? )

This however once run through lossless compression will yield nearly half the uncompressed raw size.
16-bit will not achieve the same compression for obvious reasons.

If anyone knows how this packing could be improved its more than welcome. ( where rowData is a numpy rowslice of the bayer data )

Code: Select all

                count = 0
                rowPacked = []
                for i in range(0, len(rowData) + 1, 4):
                    if i != 0:
                        temp = ''
                        for k in rowData[count:i]:
                            temp += '{0:010b}'.format(k)
                        mask = 0
                        for j in range(0, 40 + 1, 8):
                            if j != 0:
                                rowPacked.append(int(temp[mask:j], 2))
                                mask = j
                        count = i
                self.wf.write(struct.pack('B' * len(rowPacked), *rowPacked))
-Csaba Nagy

Bryan See
Posts: 11
Joined: Mon Apr 10, 2017 2:33 pm

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 10:21 am

schoolpost wrote:Bryan I'm a little lost as to what you are aiming for.

You want to use the RAW information. You've mentioned DNG aswell. ( what camera version do you have? )

I very recently created a pretty simple to use python script to do this:
https://github.com/schoolpost/pydng

I believe it creates a pretty decent output in terms of color/quality and with the addition
of standard lossless DNG compression you can half the file size at no cost. Really cant get better than that.

The mentioned C tool will pack the RAW into a 16bit DNG. It works with both V1 and V2.
But lacks the correct color matrix and doesn't do any RAW calibration prior to packing it ( lens shading, hot pixel removal )

If you want to use the RAW data, do some debayering and then output into a more standard format you could possibly explore OpenCV debayer aswell.

Not to "inflate" or "hype" myself here, but in this situation I believe my aforementioned script above
will give you the best result if DNG is what you are after. Go ahead and tweak it as you see fit.
I realize too its not the most intuitive to work with, but should be quite workable.

I'm really trying to get the process done right, so feedback is very welcome.
I'm using camera version 2.1. IMX219.

User avatar
DougieLawson
Posts: 39547
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Pi Camera V2 raw bayer data

Fri Apr 14, 2017 2:29 pm

I get this

Code: Select all

(cheese:8868): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files

(cheese:8868): dconf-CRITICAL **: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.

(cheese:8868): dconf-CRITICAL **: unable to create directory '/run/user/0/dconf': Permission denied.  dconf will not work properly.
Failed to create secure directory (/run/user/0/pulse): Permission denied
Failed to create secure directory (/run/user/0/pulse): Permission denied
AL lib: (WW) alc_initconfig: Failed to initialize backend "pulse"
Failed to create secure directory (/run/user/0/pulse): Permission denied
Failed to create secure directory (/run/user/0/pulse): Permission denied
AL lib: (EE) alsa_open_capture: Could not open capture device 'default': No such file or directory

(cheese:8868): cheese-WARNING **: Could not open device.: gstopenalsrc.c(506): gst_openal_src_prepare (): /GstCameraBin:camerabin/GstAutoAudioSrc:audiosrc/GstOpenalSrc:audiosrc-actual-src-opena:
ALC error: Invalid Value


(cheese:8868): cheese-WARNING **: Internal data flow error.: gstbasesrc.c(2933): gst_base_src_loop (): /GstCameraBin:camerabin/GstAutoAudioSrc:audiosrc/GstOpenalSrc:audiosrc-actual-src-opena:
streaming task paused, reason not-negotiated (-4)
which looks to me like a bug in the Cheese programs.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

thatneat
Posts: 4
Joined: Tue Dec 04, 2018 5:52 pm

Re: Pi Camera V2 raw bayer data

Tue Dec 04, 2018 7:46 pm

This is a pretty old thread, but I thought folks reading it might be interested to know - in Python, my team just released a fork of picamera which pulls out the raw data from a raw+jpeg file into a more consumable, numpy format. It includes only the most basic demosaicing. Check out https://github.com/OsmoSystems/picamraw/

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Wed Aug 12, 2020 8:50 pm

Hello Everyone - and welcome back to a post that may live forever! I've been reading through this (trying to solve a problem) and have found all the discussions to be very helpful. But, of course, I can't _quite_ find the solution to what I'm trying to do. So I thought I'd post it here in the hope that there's a (quick? easy?) solution.

I have a v2 Pi camera. I've been able to save the raw Bayer data as a jpeg in a couple different ways.

1) I modified the code in the https://picamera.readthedocs.io/en/rele ... a-captures example to save a file as a jpeg which gives me color files that look like this: (detail, enlarged)
image_3 8bit_detail.jpg
image_3 8bit_detail.jpg (206.84 KiB) Viewed 622 times
2) I used https://github.com/OsmoSystems/picamraw/ from @theneat and able to save the file as black-and-white jpeg: (detail, enlarged)
picamraw_bayer_array_8_detail.jpg
picamraw_bayer_array_8_detail.jpg (24.54 KiB) Viewed 622 times
So, what I believe is that essentially I now have a jpegs that shows the raw bayer data. One colorized, one b&w.

What I'd like to do is find a way to take that jpeg, treat it as though it was raw, and then demosaic/develop it, so that I end up with a finished image.

I've tried opening the "bayer jpeg" in Photoshop, Lightroom, Gimp, Darktable, etc without any luck. As well as https://github.com/colour-science/colour-demosaicing. They see a jpeg, so I can't demosaic/develop.

This may seem like a weird thing to want to do. But my goal is to be able to do some manipulation to the "Bayer jpeg" and then develop that to a final image.

Any pointers or suggestions or help would be greatly appreciated.

david,

User avatar
schoolpost
Posts: 49
Joined: Sun Feb 19, 2017 10:47 am
Location: Canada
Contact: Website

Re: Pi Camera V2 raw bayer data

Wed Aug 12, 2020 10:21 pm

dky01 wrote:
Wed Aug 12, 2020 8:50 pm
This may seem like a weird thing to want to do. But my goal is to be able to do some manipulation to the "Bayer jpeg" and then develop that to a final image.

What kind of manipulation are you attempting to do to the RAW data? You can skip the whole "Saving to JPEG" by just accessing the RAW data as 2D Numpy Array?

You can take a look at my Python3 Library: PyDNG
https://github.com/schoolpost/PyDNG

Specifically you can look at this script ( https://github.com/schoolpost/PyDNG/blo ... process.py ) for how you can do your own processing/filters onto the Bayer RAW data and then have those changes baked into the RAW data before it gets saved into the final DNG image. ( You can then open that DNG with most RAW editors and have the image demosaiced and do all your usual RAW edits )
-Csaba Nagy

therealdavidp
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 75
Joined: Tue Jan 07, 2020 9:15 am

Re: Pi Camera V2 raw bayer data

Wed Aug 12, 2020 10:56 pm

Hi dky01 (and everyone else!)

At the risk of rambling slightly around the topic, a certain amount of work has been done on turning the Pi's native jpg+raw images into DNG files that you can use in standard raw converter programs, see here: https://www.raspberrypi.org/blog/proces ... ty-camera/

That work is particularly geared to the HQ cam using the imx477 sensor, and will apply the camera white balance and give you a reasonable colour calibration. For the v2 imx219 sensor you could still make use of the white balance (I don't actually know off-hand if PyDNG does this... Csaba?) but we don't have any similar colour calibration for it. Moreover, the imx219 is a more challenging sensor in some ways. Whereas vignetting and especially colour shading is quite mild on the imx477 (at least with the lenses we've used), the imx219 is a rather different story. There's strong luminance fall-off towards the corners and, worse, the centre of the image is very pink compared to the edges. So you'd probably be wanting to have a way to fix that up too.

A colour calibration for the imx219 is doable in principle, though you'd have to decide how you want to handle the colour shading as it has a very pronounced effect depending on exactly how you fix it. Tables for fixing the colour shading could be gleaned from the libcamera tuning of the imx219 (buried in here: https://git.linuxtv.org/libcamera.git/t ... mx219.json), but obviously there's some effort involved there.

By all means get back to me here if you wanted to know more about any of that - I hope the information is useful.

PS. libcamera has a test app called qcam which will actually capture DNG raw files directly (for the imx219 and other sensors). It will even record the camera white balance and a single colour matrix (rather than the more usual two, but it's better than nothing). It won't solve the lens/colour shading issues for you, however.

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Thu Aug 13, 2020 4:14 pm

schoolpost wrote:
Wed Aug 12, 2020 10:21 pm
dky01 wrote:
Wed Aug 12, 2020 8:50 pm
This may seem like a weird thing to want to do. But my goal is to be able to do some manipulation to the "Bayer jpeg" and then develop that to a final image.

What kind of manipulation are you attempting to do to the RAW data? You can skip the whole "Saving to JPEG" by just accessing the RAW data as 2D Numpy Array?

You can take a look at my Python3 Library: PyDNG
https://github.com/schoolpost/PyDNG

Specifically you can look at this script ( https://github.com/schoolpost/PyDNG/blo ... process.py ) for how you can do your own processing/filters onto the Bayer RAW data and then have those changes baked into the RAW data before it gets saved into the final DNG image. ( You can then open that DNG with most RAW editors and have the image demosaiced and do all your usual RAW edits )
Hi Csaba and therealdavidp (and everyone else)...

This looks very close to what I want to do. But... of course, there's a catch...

For my "manipulations' what I'm actually doing is passing the raw images into some ai/machine learning code. In general my code loads several images at once, does its ai-stuff, and then generates new numpy arrays which I then save. Currently I'm saving as jpeg, but saving as PyDNG "raw" files would be great -- I could then open them in Photoshop or wherever, to do the developing/demosaicing.

I could easily modify my code so that I load the pi-cam's raw and extract a bayer array (either with picamraw or something else (could PyDNG do this?)). What I'd then like to do is, once I've created a new numpy array with my code, is pass that array to PyDNG and have it output the fancy raw/dng file. Is that possible?

Thanks!

david,

User avatar
schoolpost
Posts: 49
Joined: Sun Feb 19, 2017 10:47 am
Location: Canada
Contact: Website

Re: Pi Camera V2 raw bayer data

Thu Aug 13, 2020 10:38 pm

dky01 wrote:
Thu Aug 13, 2020 4:14 pm
I could easily modify my code so that I load the pi-cam's raw and extract a bayer array (either with picamraw or something else (could PyDNG do this?)). What I'd then like to do is, once I've created a new numpy array with my code, is pass that array to PyDNG and have it output the fancy raw/dng file. Is that possible?

Thanks!

david,

This can be done yes, again I would take a closer look at some of the examples I have in PyDNG.
https://github.com/schoolpost/PyDNG/tre ... r/examples

https://github.com/schoolpost/PyDNG/blo ... raw2dng.py
This one in particular shows you how to save any arbitrary bayer raw data from a numpy array into a DNG. You will need to adjust the DNG tags accordingly.

https://github.com/schoolpost/PyDNG/blo ... process.py
If you want this to be even simpler and don't want to fiddle with setting up the DNG metadata tags, you should use this example and add all your processing into this function:

Code: Select all

from pydng.core import RPICAM2DNG
import numpy as np

def processing(raw):
    # access to the bayer raw numpy array here. 
    # you can add all your AI processing here, the raw object is the numpy array containing the bayer raw information.
    # do all you processing to it and make sure to return that processed numpy array. 
    # simple example here just prints the shape and size of the numpy array
    print(raw.shape, raw.size)
    return raw

RPICAM2DNG().convert("imx477.jpg", process=processing)
Hope that helps
-Csaba Nagy

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Tue Aug 18, 2020 8:13 pm

Thank you! The PyDNG code has been very helpful. I was able to get the basic 'd.convert' to work perfectly. But I'm running into a problem where the image tint is way off, and there are strange noise artifacts. I have the same problem in a couple different ways I've used the code. So I'll paste code below which, while is a bit strange, is the simplest way I can demonstrate what's going on...

Code: Select all

from pydng.core import RPICAM2DNG
from pydng.core import RAW2DNG, DNGTags, Tag

import numpy as np
from PIL import Image 

def processing2(raw, inputfile):
    # access to the bayer raw numpy array here. 
    print(raw.shape, raw.size)

    # This saves a jpg that shows the raw bayer pattern
    #output = (raw >> 2).astype(np.uint8)
    #im = Image.fromarray(output, 'L')
    #im.save("processing2_raw_8.jpg")
    
    # uncalibrated color matrix, just for demo. 
    ccm1 = [[19549, 10000], [-7877, 10000], [-2582, 10000],	
            [-5724, 10000], [10121, 10000], [1917, 10000],
            [-1267, 10000], [ -110, 10000], [ 6621, 10000]]

    bpp= 10 # a value of 8 results in too bright an image, 12 (the orig value) is too dark.
    width = 3280 # orig value was 4096
    height = 2464 #orig value was 3072

    # set DNG tags.
    t = DNGTags()
    t.set(Tag.ImageWidth, width)
    t.set(Tag.ImageLength, height)
    t.set(Tag.TileWidth, width)
    t.set(Tag.TileLength, height)
    t.set(Tag.Orientation, 1)
    t.set(Tag.PhotometricInterpretation, 32803)
    t.set(Tag.SamplesPerPixel, 1)
    t.set(Tag.BitsPerSample, bpp)
    t.set(Tag.CFARepeatPatternDim, [2,2])
    t.set(Tag.CFAPattern, [1, 2, 0, 1])
    t.set(Tag.BlackLevel, (4096 >> (16 - bpp)))
    t.set(Tag.WhiteLevel, ((1 << bpp) -1) )
    t.set(Tag.ColorMatrix1, ccm1)
    t.set(Tag.CalibrationIlluminant1, 21)
    t.set(Tag.AsShotNeutral, [[1,1],[1,1],[1,1]])
    t.set(Tag.DNGVersion, [1, 4, 0, 0])
    t.set(Tag.DNGBackwardVersion, [1, 2, 0, 0])
    t.set(Tag.Make, "Camera Brand")
    t.set(Tag.Model, "Camera Model")
    t.set(Tag.PreviewColorSpace, 2)

    # save to dng file.
    RAW2DNG().convert(raw, tags=t, filename="custom_process_10", path="")
    
    return raw

RPICAM2DNG().convert("/home/pi/myimage.jpg", process=processing2)
In the code, note that I've changed the width and height, and bpp from the defaults. (I also added an extra arg to the processing function because I was getting the error 'TypeError: processing2() takes 1 positional argument but 2 were given')

The image I'm using was captured using the command 'raspistill --raw -o myimage.jpg' And this is the result:
t1.jpg
t1.jpg (140.18 KiB) Viewed 486 times
But here is what the code above produces (when opened in Photoshop with Camera Raw) -- very purple:
t2.jpg
t2.jpg (125.3 KiB) Viewed 486 times
Here's a detail, showing some strange artifacts (after I've adjusted the Temperature and Tint):
t32.jpg
t32.jpg (191.15 KiB) Viewed 486 times
I feel like I'm super-close. Perhaps I'm just doing something crazy in Photoshop. But any suggestions would be greatly appreciated.

david,

therealdavidp
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 75
Joined: Tue Jan 07, 2020 9:15 am

Re: Pi Camera V2 raw bayer data

Tue Aug 18, 2020 9:09 pm

Hi again

The purple colour, I would guess, is caused by not having correct white balance defined in your DNG, nor a correct colour matrix. You can get reasonable values for both from the makernote in the jpeg file that was saved. The "AsShotNeutral" values need to be the reciprocals of the colour gains recorded there, and you'll also find a camera RGB -> sRGB matrix that can be converted into the form that DNG files expect. (You have to take the matrix, pre-multiply by an RGB->XYZ matrix, post-multiply by a diagonal matrix of the colour gains, and then invert the result. But give me a shout if you want to know more about that.)

The other problem you refer to is the strange maze-like artefacts on the lampshade? To me those look like green imbalance (I've sometimes seen it called "crosstalk" too). The problem is that the two types of green pixel (the ones on blue rows and the ones on red rows) sometimes respond a bit differently (pixel crosstalk from the other colours, presumably), and then the demosaic algorithm, trying to eke out detail, turns these differences into "maze artefacts". I'm not familiar with Adobe Camera Raw, but I assume it must have a function for correcting this.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9236
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 6:58 am

That shade of purple normally means the bayer order is wrong. Greens swapped with the red&blue. That also tallies with David's comments on green crosstalk - you do as you're actually looking at the blue and red pixels.

You could try altering your cfapattern tag to 0 1 1 2, or 2 1 1 0.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Jack Hogan
Posts: 25
Joined: Wed May 13, 2020 2:08 pm
Contact: Website

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 8:40 am

dky01 wrote:
Tue Aug 18, 2020 8:13 pm
The image I'm using was captured using the command 'raspistill --raw -o myimage.jpg'
May I ask why your image is upside down? If because that's the unflipped raw file be aware that the HQ cam would then be in RGGB format, so in that case you would want '0 1 1 2' as the Bayer DNG sequence (I explain this a little bit in the raspiraw section at the bottom of the page here). If this is the issue it will probably fix the artifacts as well. I don't know about V2 layouts, so experiment a bit as 6x9 suggested.

Jack

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9236
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 8:56 am

With raspiraw and the raw+jpeg from raspistill there is a header to the raw data that contains all the information on bpp, bayer order, width, height, stride, flips, and a number of other parameters. (It's optional with raspiraw).

The fork of dcraw parses this header so it can interpret captures from all camera variants and flip parameters. I don't know how easy that would be to lift into the python world.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 6:30 pm

Ok - some progress thanks to the last comments...

(Side note: the image is upside down because my camera is upside down.)

I changed the CFAPattern to [2, 1, 1, 0] and that made a huge improvement.

The remaining issue is that the image needs a lot of (I think) white balance adjusting to get the colors right. I tried the code from https://pypi.org/project/ExifRead/ to see what is in the raw jpg captured by the camera and it gave me this:

Code: Select all

Key: Image ImageWidth, value 3280
Key: Image ImageLength, value 2464
Key: Image Make, value RaspberryPi
Key: Image Model, value RP_imx219
Key: Image XResolution, value 72
Key: Image YResolution, value 72
Key: Image ResolutionUnit, value Pixels/Inch
Key: Image DateTime, value 2020:08:18 15:39:31
Key: Image YCbCrPositioning, value Centered
Key: Image ExifOffset, value 192
Key: Thumbnail ImageWidth, value 64
Key: Thumbnail ImageLength, value 48
Key: Thumbnail Compression, value JPEG (old-style)
Key: Thumbnail XResolution, value 72
Key: Thumbnail YResolution, value 72
Key: Thumbnail ResolutionUnit, value Pixels/Inch
Key: Thumbnail JPEGInterchangeFormat, value 1046
Key: Thumbnail JPEGInterchangeFormatLength, value 24576
Key: EXIF ExposureTime, value 8873/250000
Key: EXIF FNumber, value 2
Key: EXIF ExposureProgram, value Aperture Priority
Key: EXIF ISOSpeedRatings, value 80
Key: EXIF ExifVersion, value 0220
Key: EXIF DateTimeOriginal, value 2020:08:18 15:39:31
Key: EXIF DateTimeDigitized, value 2020:08:18 15:39:31
Key: EXIF ComponentsConfiguration, value YCbCr
Key: EXIF ShutterSpeedValue, value 2408181/500000
Key: EXIF ApertureValue, value 2
Key: EXIF BrightnessValue, value 66/25
Key: EXIF MaxApertureValue, value 2
Key: EXIF MeteringMode, value CenterWeightedAverage
Key: EXIF Flash, value Flash did not fire
Key: EXIF FocalLength, value 3039/1000
Key: EXIF FlashPixVersion, value 0100
Key: EXIF ColorSpace, value sRGB
Key: EXIF ExifImageWidth, value 3280
Key: EXIF ExifImageLength, value 2464
Key: Interoperability InteroperabilityIndex, value R98
Key: EXIF InteroperabilityOffset, value 910
Key: EXIF ExposureMode, value Auto Exposure
Key: EXIF WhiteBalance, value Auto
When I ran the same code on the .dng file produced with PyDNG I got this:

Code: Select all

Key: Image SubfileType, value Full-resolution Image
Key: Image ImageWidth, value 3280
Key: Image ImageLength, value 2464
Key: Image BitsPerSample, value 10
Key: Image Compression, value Uncompressed
Key: Image PhotometricInterpretation, value 32803
Key: Image Make, value Camera Brand
Key: Image Model, value Camera Model
Key: Image Orientation, value Horizontal (normal)
Key: Image SamplesPerPixel, value 1
Key: Image Software, value PyDNG
Key: Image TileWidth, value 3280
Key: Image TileLength, value 2464
Key: Image TileOffsets, value 452
Key: Image TileByteCounts, value 10102400
Key: Image CFARepeatPatternDim, value [2, 2]
Key: Image CFAPattern, value [2, 1, 1, 0]
Key: Image Tag 0xC612, value [1, 4, 0, 0]
Key: Image Tag 0xC613, value [1, 2, 0, 0]
Key: Image BlackLevel, value 64
Key: Image Tag 0xC61D, value 1023
Key: Image Tag 0xC621, value [19549/10000, -7877/10000, -1291/5000, -1431/2500, 10121/10000, 1917/10000, -1267/10000, -11/1000, 6621/10000]
Key: Image Tag 0xC628, value [1, 1, 1]
Key: Image Tag 0xC65A, value 21
Key: Image Tag 0xC71A, value 2
1. Does it matter that it doesn't display EXIF tags the same way as the raw jpeg?

2. I'm hoping if I can add some of the tags from the raw jpg to the .dng file then my whitebalance problem will improve.

But, I couldn't figure out how to set some of the exif tags. For example,

Code: Select all

t.set(Tag.ApertureValue, 2)
results in this runtime error:

Code: Select all

TAG Encoding Error! 'int' object is not iterable ((37378, (5, 8)), (2,))
And similar errors if I try to set FocalLength, etc.

Am I trying to set an EXIF tag incorrectly?

Thanks,

david,

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9236
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 7:08 pm

If memory serves correctly, those exif tags are rationals, not ints. It probably wants numerator and denominator separately, but don't ask me exactly the format this particular library wants.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

therealdavidp
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 75
Joined: Tue Jan 07, 2020 9:15 am

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 9:04 pm

To get the white balance right, you need to set the "AsShotNeutral" values. These can be found in the jpg+raw file using

Code: Select all

strings <jpg+raw file> | grep gain
and look for gain_r and gain_b.

As 6by9 says, they are indeed rational values (three of them in the order R, G and B). I tend to use exiftool ('sudo apt install exiftool' I think). When I run it on a dng file I get something like

Code: Select all

...
Color Matrix 1                  : 0.5433267949 -0.06696011083 -0.060241967 -0.2519178387 1.117224455 0.1122495155 0.001039618627 0.1516246643 0.3499031064
As Shot Neutral                 : 0.3838596939 1 0.5335966944
...

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 9:34 pm

I'm getting close the edge of my Python skills. So sorry if this is Python 101...

When I do 'strings <jpg+raw file> | grep gain' -- the values I see are: gain_r=1.613 gain_b=1.734

How do I store these for AsShotNeutral? In the code, the example of setting that tag is:

Code: Select all

t.set(Tag.AsShotNeutral, [[1,1],[1,1],[1,1]])
Separately, when I run EXIF.py on the .dng file produced from my PyPNG code they don't look as pretty (missing the English EXIF names). Is this ok?:

Code: Select all

Image Tag 0xC612 (Byte): [1, 4, 0, 0]
Image Tag 0xC613 (Byte): [1, 2, 0, 0]
Image Tag 0xC61D (Short): 1023
Image Tag 0xC621 (Signed Ratio): [19549/10000, -7877/10000, -1291/5000, -1431/2500, 10121/10000, 1917/10000, -1267/10000, -11/1000, 6621/10000]
Image Tag 0xC628 (Ratio): [1, 1, 1]
Image Tag 0xC65A (Short): 21
Image Tag 0xC71A (Long): 2
Image TileByteCounts (Long): 10102400
Image TileLength (Short): 2464
If anything, this is a good way to keep a four year old thread up and running ;)

Thank you,

david,

therealdavidp
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 75
Joined: Tue Jan 07, 2020 9:15 am

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 10:01 pm

dky01 wrote:
Wed Aug 19, 2020 9:34 pm
I'm getting close the edge of my Python skills. So sorry if this is Python 101...

When I do 'strings <jpg+raw file> | grep gain' -- the values I see are: gain_r=1.613 gain_b=1.734

How do I store these for AsShotNeutral? In the code, the example of setting that tag is:

Code: Select all

t.set(Tag.AsShotNeutral, [[1,1],[1,1],[1,1]])
So AsShotNeutral needs to record the reciprocals of the colour gains which are 0.620 (= 1/1.613 for red), 1 (for green) and 0.577 (= 1/1.734 for blue). So that line of code will look something like

Code: Select all

t.set(Tag.AsShotNeutral, [[620,1000],[1,1],[577,1000]])

dky01
Posts: 14
Joined: Mon Mar 04, 2019 5:56 pm

Re: Pi Camera V2 raw bayer data

Wed Aug 19, 2020 10:08 pm

That was it -- it works!!!! :shock:

Thank you everyone for all your help.

Best wishes,

david,

Return to “Camera board”