### Author Topic: Extracting bayer planes  (Read 3217 times)

#### esordini

• Newcomer
• Posts: 9
##### Extracting bayer planes
« on: 2014 June 24 16:08:16 »
Hello all,
I have been recently playing with a Fuji X-T1 mirrorless camera.

One of the tests I planned on performing requires extracting individual color planes from a raw file. Now while it's a rather easy task with "conventional" raw file formats such as Canon's CRW o CR2, I am at a loss with Fujifilm's format, which - it turned out - uses a different, more random bayer pattern (please see attachment).

So I was wondering if there is some way to extract one single color channel (either R, G, or B) from a Fujifilm raw image by using PI's advanced scripting or pixel math features.

Emmanuele

#### Juan Conejero

• PTeam Member
• PixInsight Jedi Grand Master
• Posts: 7010
##### Re: Extracting bayer planes
« Reply #1 on: 2014 June 25 04:29:22 »
CFA separations can be done very easily with PixelMath. In the case of the X-Trans CFA, the expressions required are more complex, but perfectly manageable anyway:

Red:
dy = y() % 6;
dx = x() % 6;
\$T * (dx == 4 && (dy == 0 || dy == 2) ||
dx == 1 && (dy == 3 || dy == 5) ||
dy == 1 && (dx == 0 || dx == 2) ||
dy == 4 && (dx == 3 || dx == 5))

Green:
dy = y() % 6;
dx = x() % 6;
\$T * ((dy == 1 || dy == 4) == (dx == 1 || dx == 4 ))

Blue:
dy = y() % 6;
dx = x() % 6;
\$T * (dx == 1 && (dy == 0 || dy == 2) ||
dx == 4 && (dy == 3 || dy == 5) ||
dy == 4 && (dx == 0 || dx == 2) ||
dy == 1 && (dx == 3 || dx == 5))

with symbols dx and dy declared. The above PixelMath expressions can be used to convert a monochrome CFA image to a RGB image with separate color planes. Do the following:

- With the NewImage tool, create a new grayscale image of 6x6 pixels filled with white (fill value = 1)

- Open the PixelMath tool, uncheck the use a single RGB/K expression option, and copy the above expressions to their corresponding channels (don't include the channel names in the expressions). Write dx, dy in the Symbols field.

- Open the Destination section of PixelMath, check the Create new image option, and select Color space = RGB Color.

- Apply the PixelMath tool to the 6x6 grayscale white image. You should get exactly the X-Trans CFA pattern as a new RGB color image.

I have attached a .xpsm file with a ready to use PixelMath icon. Of course this could also be done with JavaScript. The JS source code would be simpler, but its execution would be slower because PixelMath is a highly optimized pixel-oriented language interpreter and runs multithreaded.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

#### esordini

• Newcomer
• Posts: 9
##### Re: Extracting bayer planes
« Reply #2 on: 2014 June 25 13:50:59 »
CFA separations can be done very easily with PixelMath. In the case of the X-Trans CFA, the expressions required are more complex, but perfectly manageable anyway:
[...]
I have attached a .xpsm file with a ready to use PixelMath icon. Of course this could also be done with JavaScript. The JS source code would be simpler, but its execution would be slower because PixelMath is a highly optimized pixel-oriented language interpreter and runs multithreaded.
Dear Juan,
thank you so much for your exhaustive reply. I have two more questions:

1) Upon opening a .RAF image, PI will always convert it to RGB mode. Is there a way to open it in 16-bit grayscale mode, i. e. something equivalent to "dcraw -4 -D" (*)? This would preserve the original bayer matrix. In fact, my goal is to extract each channel individually, thus obtaining three separate images.

2) What if I had to automate the procedure on a batch of images and save the individual channels for later use? Ideally, I would prefer to save them as 16-bit FITS files with the exposure time and ISO value from the source .RAF correctly set in the FITS header. Can this be done in PI?

Thanks again,
Emmanuele

(*) For details, please see here: http://chdk.setepontos.com/index.php?topic=4963.0

#### Juan Conejero

• PTeam Member
• PixInsight Jedi Grand Master
• Posts: 7010
##### Re: Extracting bayer planes
« Reply #3 on: 2014 June 25 13:58:26 »
Quote
Is there a way to open it in 16-bit grayscale mode

Sure. Do the following:

- Open the Format Explorer window.

- Double click the DSLR_RAW at the left.

- On the RAW Format Preferences dialog:
* Uncheck both white balance options.
* Check the "Create raw Bayer CFA image - monochrome" option (third checkbox in the middle section)
* Check the "No black point correction" option.

From now on all DSLR raw images will be loaded as pure raw data without interpolation. They will be 16-bit monochrome CFA frames.

Quote
2) What if I had to automate the procedure on a batch of images and save the individual channels for later use? Ideally, I would prefer to save them as 16-bit FITS files with the exposure time and ISO value from the source .RAF correctly set in the FITS header. Can this be done in PI?

This requires a script. The PixelMath instance that I described above can be embedded in a JavaScript script to apply it to a list of images. See the BatchFormatConversion script for an example script that you could use as a suitable skeleton in this case.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

#### esordini

• Newcomer
• Posts: 9
##### Re: Extracting bayer planes
« Reply #4 on: 2014 June 25 14:22:16 »
- On the RAW Format Preferences dialog:
* Uncheck both white balance options.
* Check the "Create raw Bayer CFA image - monochrome" option (third checkbox in the middle section)
* Check the "No black point correction" option.

From now on all DSLR raw images will be loaded as pure raw data without interpolation. They will be 16-bit monochrome CFA frames.
Unfortunately all I get is a solid black image. Maybe it's an issue with the RAF format? (Might very well be, because it works like a charm with CR2 files).

Thanks,
Emmanuele

#### Juan Conejero

• PTeam Member
• PixInsight Jedi Grand Master
• Posts: 7010
##### Re: Extracting bayer planes
« Reply #5 on: 2014 June 25 14:30:11 »
Are you sure it is pure black? Since it is raw data, it is normal that it be very dark, especially if it's an astronomical image.

The Fuji X-T1 camera is supported by dcraw:

http://www.cybercom.net/~dcoffin/dcraw/

Juan Conejero
PixInsight Development Team
http://pixinsight.com/

#### esordini

• Newcomer
• Posts: 9
##### Re: Extracting bayer planes
« Reply #6 on: 2014 June 25 15:07:11 »
Are you sure it is pure black?
Yes, because the K value always reads "0.0000" no matter where I hover the mouse, and the image stays the same even after a STF autostretch. Strangely enough, the same raw image

1) gets correctly converted into a color image if opened in RGB mode in PI
2) gets correctly converted into a 16-bit grayscale (PGM) image when invoking dcraw on the image with the "-4 -D" flags in a Windows shell.

I might be wrong, but my guess is that the way dcraw is called within PI kind of messes up with that particular "flavor" of the RAF format. I can let you have a sample image if you want.

Thanks,
Emmanuele