Page 1 of 1

Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Nov 04, 2016 1:46 am
by imadaily
I have recently removed the bayer filter layer from a pi camera and am now trying to access the image data before it is processed through the traditional route (debayered,interpolated,compressed to jpg) as that would limit the benefits of having a modified sensor.

I am however struggling to get the RAW into a useful format

to capture an image I've been using:

Code: Select all

raspistill -w 64 -h 48 -n -r -o rawtest
The file size is as expected: 6.13MB for low res jpg + RAW (and was 9.39MB for full size jpg with RAW)

Trying to decode it in DCRaw:

Code: Select all

pi@Pi-2B:~ $ dcraw -v rawtest
Cannot decode file rawtest

pi@Pi-2B:~ $ dcraw -v -i rawtest

Filename: rawtest
Timestamp: Fri Nov  4 01:13:28 2016
Camera: RaspberryPi RP_ov5647
ISO speed: 500
Shutter: 1/8.0 sec
Aperture: f/2.9
Focal length: 3.6 mm
Embedded ICC profile: no
Number of raw images: 0
Thumb size:    64 x 48
Full size:   2592 x 1944
DCRaw isn't able to read the RAW data and seems to report that the .jpg doesn't contain any raw images yet seems to correctly identify the resolution of the RAW data...

I think it's probably something really simple that I've missed, any help would be appreciated.

Thanks,

Finally If anyone would like more info or has questions on the debayering of the sensor let me know and i'll answer them or do a brief write-up.
debayering bru.jpg
Image from the mostly debayered sensor, showing B&W central section and coloured edge where the bayer filter remains.
debayering bru.jpg (54.69 KiB) Viewed 2982 times

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Nov 04, 2016 10:22 am
by 6by9
Where have you got your version of dcraw from? The main version doesn't have support for the latest changes to the Pi Camera code.
https://github.com/6by9/RPiTest/tree/master/dcraw has been updated to handle both V1 and V2 cameras, plus auto detection of Bayer order and resolution via the raw header.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Nov 04, 2016 5:41 pm
by imadaily
Ah, I didn't know that the Pi Camera code had changed... I was using version 9.21 which I got through

Code: Select all

sudo apt-get install dcraw

Thanks for the link to your version,

New problem: I think dcraw might be reading the jpg encoded portion of the file and not the raw data??
Or at least that's what seems to be happening.

Code: Select all

raspistill -n -w 240 -h 180 -o noirtest.jpg -e jpg -r 
which gives this:
noirtest.jpg
the image in .jpg format
noirtest.jpg (21.04 KiB) Viewed 2853 times
running dcraw gives an output which is the same size as the jpg and clearly isn't decoding correctly.

Code: Select all

pi@Pi-2B:~/DCRaw69 $ ./dcraw -v -T noirtest.jpg
Loading RaspberryPi RP_ov5647 image from noirtest.jpg ...
Scaling with darkness 0, saturation 1023, and
multipliers 1.000000 1.000000 1.000000 1.000000
AHD interpolation...
Building histograms...
Writing data to noirtest.tiff ...
noirtest(tif).jpg
the file output from dcraw
noirtest(tif).jpg (26.79 KiB) Viewed 2853 times
When running the "Identify files and show matadata" option should the metadata be showing the resolution of the .jpg or of the raw data?

Code: Select all

pi@Pi-2B:~/DCRaw6by9 $ ./dcraw -v -i noirtest.jpg

Filename: noirtest.jpg
Timestamp: Fri Nov  4 17:06:12 2016
Camera: RaspberryPi RP_ov5647
ISO speed: 500
Shutter: 1/8.0 sec
Aperture: f/2.9
Focal length: 3.6 mm
Embedded ICC profile: no
Number of raw images: 1
Thumb size:    64 x 48
Full size:    240 x 180
Image size:   240 x 180
Output size:  240 x 180
Raw colors: 3
Filter pattern: RG/GB
Daylight multipliers: 1.000000 1.000000 1.000000
Finally, I've tried this on two different camera modules (v1.3 NoIR and v1.3 chinese clone) so i think that it's unlikely to be a camera sensor fault causing the trouble.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Nov 04, 2016 11:04 pm
by 6by9
Dcraw is now up to 9.27 at Dave Coffin's site - https://www.cybercom.net/~dcoffin/dcraw/ The Debian/Raspbian repos are always a fair way behind because they only take "stable" releases.

Can you post your capture to somewhere like dropbox or pastebin please so I can analyse the raw image?
I think that dcraw is taking the wrong width/height as it is assuming that the JPEG is the same size as the raw, which it isn't with you having specified "-w 240 -h 180" in your raspistill line. That ought to be fixed.
It does look like there is an issue with processing the OV5647 images too as I also get noise. I'm pretty sure it was working a couple of months back, and testing again with jbeale's test RAW captures they are fine, so I'm not quite sure what's going on. I'll have to investigate.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Nov 04, 2016 11:25 pm
by imadaily
I've tried again using no flags for the width and height so the jpg portion should be the full size and it still comes out jumbled from dcraw

I've uploaded 2 files:
the original taken with 240*180 resolution
https://drive.google.com/open?id=0B4FGr ... Ec3czl2elk
and

Code: Select all

raspistill -n -r -o noirtest2.jpg
https://drive.google.com/open?id=0B4FGr ... i15cVJ4anM

Many thanks for looking into this,

Edit/update:

I've just tried one of the old (~may 2013) test images: raw-full-330pm.jpg
Halfway down the page on viewtopic.php?f=43&t=44918

On the pi, using your version dcraw, it gave me the same scrambled pattern as my failed images.
Using the apt-get install dcraw version (v9.21) successfully produces a tif image.
on windows (dcraw v9.27) also works.

To me this suggests that I may have done something wrong when setting up your version, however none them work with any pictures that I take so the original problem probably still exists.

I may try using an old version of raspbian and see if pictures taken with that work (though my oldest pi is a 2B so i think i'm limited to using raspbian versions released after feb 2015)

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Sun Nov 20, 2016 6:54 pm
by imadaily
Hi 6by9,
Did you get a chance to look into this?

I've still had no luck with any picture I've taken :(

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Sun Nov 20, 2016 10:05 pm
by 6by9
imadaily wrote:Hi 6by9,
Did you get a chance to look into this?

I've still had no luck with any picture I've taken :(
Not as yet - sorry. It is on the list.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Fri Feb 03, 2017 2:23 pm
by 6by9
I've just had a look. It seems that the EXIF model changed from "ov5647" to "RP_OV5647" to "RP_ov5647". The first two were covered, the third wasn't :(

Seeing as waveform80 has worked out all the file offsets for the different camera modes, I'm doing an update to dcraw that should be able to find the header off any of the camera modes and do the right thing. Hopefully I'll be able to make it with all of the raw images that I can lay my hands on.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Mon Feb 06, 2017 1:03 pm
by 6by9
I've just pushed an update to https://github.com/6by9/RPiTest/tree/master/dcraw which seems to read raw images from any of the camera modes on either sensor quite happily. It also bumps the underlying version of DCRaw to 9.27.

If people find issues in decoding raw images with that then please let me know. Otherwise I'll see if I can get this merged into the official version of dcraw.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Mon Feb 06, 2017 1:45 pm
by PiGraham
imadaily wrote:I have recently removed the bayer filter layer from a pi camera and am now trying to access the image data before it is processed through the traditional route (debayered,interpolated,compressed to jpg) as that would limit the benefits of having a modified sensor.

I am however struggling to get the RAW into a useful format

to capture an image I've been using:

Code: Select all

raspistill -w 64 -h 48 -n -r -o rawtest
The file size is as expected: 6.13MB for low res jpg + RAW (and was 9.39MB for full size jpg with RAW)

Trying to decode it in DCRaw:

Code: Select all

pi@Pi-2B:~ $ dcraw -v rawtest
Cannot decode file rawtest

pi@Pi-2B:~ $ dcraw -v -i rawtest

Filename: rawtest
Timestamp: Fri Nov  4 01:13:28 2016
Camera: RaspberryPi RP_ov5647
ISO speed: 500
Shutter: 1/8.0 sec
Aperture: f/2.9
Focal length: 3.6 mm
Embedded ICC profile: no
Number of raw images: 0
Thumb size:    64 x 48
Full size:   2592 x 1944
DCRaw isn't able to read the RAW data and seems to report that the .jpg doesn't contain any raw images yet seems to correctly identify the resolution of the RAW data...

I think it's probably something really simple that I've missed, any help would be appreciated.

Thanks,

Finally If anyone would like more info or has questions on the debayering of the sensor let me know and i'll answer them or do a brief write-up.
debayering bru.jpg
If you have literally removed the bayer filter then your raw data is greyscale pixel data. Usually dcraw is used to process raw beyer data into RGB, but since you have no colour information in your raw data you don't want to do that.
Is this is for astronomy?
I've worked with a debayered Canon 5DIII and used imajeJ to read the raw CR2 file as greyscale pixel data. There are plugins to help with that. Most imaging programs that will read RAW data will be no use because they do bayer -> RGB processing
It's essential that no colour processing is performed or you will corrupt your greyscale data.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Mon Feb 06, 2017 5:06 pm
by imadaily
Thanks 6by9, I'll test it this evening.

PiGraham,
DCRaw can be set to convert from a raw file into a greyscale tiff without applying any debayering filters, which is what i am after.

I hadn't thought to try ImageJ, though a very quick look suggested using a plugin called "dcraw " to open raw images, at which point I may as well be using dcraw on its own?

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Tue Feb 07, 2017 8:41 am
by PiGraham
imadaily wrote:Thanks 6by9, I'll test it this evening.

PiGraham,
DCRaw can be set to convert from a raw file into a greyscale tiff without applying any debayering filters, which is what i am after.

I hadn't thought to try ImageJ, though a very quick look suggested using a plugin called "dcraw " to open raw images, at which point I may as well be using dcraw on its own?
If DCRaw can read the pixel data and not debayer it then OK, but getting DCRaw to read the data seems to be your problem.

ImageJ does have a DCraw plug in, but it can also read raw bytes from a file as an image, which is perhaps what you need to do.

Just a thought, maybe numpy or pillow could do that. Read byte data from a specified offset in a file as a specified width and height array and then treat it as an image to process or save it it in standard image formats. Actually, writing your own function to do that should be quite straightforward.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Tue Feb 07, 2017 5:31 pm
by 6by9
PiGraham wrote:If DCRaw can read the pixel data and not debayer it then OK, but getting DCRaw to read the data seems to be your problem.

ImageJ does have a DCraw plug in, but it can also read raw bytes from a file as an image, which is perhaps what you need to do.

Just a thought, maybe numpy or pillow could do that. Read byte data from a specified offset in a file as a specified width and height array and then treat it as an image to process or save it it in standard image formats. Actually, writing your own function to do that should be quite straightforward.
https://picamera.readthedocs.io/en/late ... a-captures
And https://picamera.readthedocs.io/en/late ... bayerarray offers some automatic extraction of the raw and optional demosaic.

Do be aware that the raw10 packing is a pain to handle. It's packed as 5 pixels in 4 bytes, MS 8bits of each of the four pixels in the first 4 bytes, then the LS 2bits of those pixels packed into the 5th byte. See the PiCamera code for more detail of extracting it.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Tue Feb 07, 2017 5:37 pm
by PiGraham
6by9 wrote:Do be aware that the raw10 packing is a pain to handle. It's packed as 5 pixels in 4 bytes, MS 8bits of each of the four pixels in the first 4 bytes, then the LS 2bits of those pixels packed into the 5th byte. See the PiCamera code for more detail of extracting it.
Thanks, good to know. So, does DCRaw handle that, or is something else required?

From your link I see there is example code to get the pixel data into a numpy array. That would be easy to adapt to get a 2D array of greayscale pixel values.
This (heavily commented) example script causes the camera to capture an image including the raw Bayer data. It then proceeds to unpack the Bayer data into a 3-dimensional numpy array

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Tue Feb 07, 2017 6:03 pm
by 6by9
PiGraham wrote:
6by9 wrote:Do be aware that the raw10 packing is a pain to handle. It's packed as 5 pixels in 4 bytes, MS 8bits of each of the four pixels in the first 4 bytes, then the LS 2bits of those pixels packed into the 5th byte. See the PiCamera code for more detail of extracting it.
Thanks, good to know. So, does DCRaw handle that, or is something else required?
https://github.com/6by9/RPiTest/blob/ma ... aw.c#L1971

That updated version of dcraw also does the right thing for finding the raw in the extended JPEG file regardless of sensor mode.

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Thu Feb 09, 2017 3:24 am
by imadaily
Many thanks 6by9, it's all working well :D

It took me a whole day to realise that by running:

Code: Select all

dcraw -v -rawtest
rather than

Code: Select all

./dcraw -v -rawtest

I was using the previously installed version, not the new one *Head-desk*

Re: Trouble with decoding RAW output (from debayered pi cam)

Posted: Thu Feb 09, 2017 8:14 am
by 6by9
Ok, I'll try making contact with Dave Coffin to get it merged into the main upstream dcraw.