Get RAW image data (Bayer pixel array) from other formats.
-
- Posts: 5
- Joined: 2016-04-12T10:26:02-07:00
- Authentication code: 1151
Get RAW image data (Bayer pixel array) from other formats.
I am testing various demosaicing algorithms in embedded platforms and I need .raw (Bayer Color Filter Array) images as input.
I would like to extract these images from .png ones but also from .cr2.
Some of the .png files that I would like to use can be found here http://r0k.us/graphics/kodak/.
For .cr2 to .raw , I am using dcraw -D (no demosaicing) -4 to get a 16bit Bayer image. The output of this command however is .pgm and because of that the image file contains some metadata. Is there a way to convert the .pgm file to .raw with Image Magick and get a pixel array image suitable for demosaicing (no metadata, the size of the file in bytes must be (Width x Height x Bits Per Pixel )/ 8 ).
For .png to .raw I found this post http://stackoverflow.com/questions/2912 ... bit-to-raw , but the command stream -map r -storage-type short image.png image.raw saves only the red channel (map -r) to the .raw file and this image is not suitable for demosaicing (the .raw file should contain one of the Bayer patterns, thus all of the R G B components, one in each pixel).
How can I get .raw files with IM?
Thanks a lot!!!
I would like to extract these images from .png ones but also from .cr2.
Some of the .png files that I would like to use can be found here http://r0k.us/graphics/kodak/.
For .cr2 to .raw , I am using dcraw -D (no demosaicing) -4 to get a 16bit Bayer image. The output of this command however is .pgm and because of that the image file contains some metadata. Is there a way to convert the .pgm file to .raw with Image Magick and get a pixel array image suitable for demosaicing (no metadata, the size of the file in bytes must be (Width x Height x Bits Per Pixel )/ 8 ).
For .png to .raw I found this post http://stackoverflow.com/questions/2912 ... bit-to-raw , but the command stream -map r -storage-type short image.png image.raw saves only the red channel (map -r) to the .raw file and this image is not suitable for demosaicing (the .raw file should contain one of the Bayer patterns, thus all of the R G B components, one in each pixel).
How can I get .raw files with IM?
Thanks a lot!!!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Get RAW image data (Bayer pixel array) from other formats.
The dcraw option "-T" creates tiff instead of ppm, if you prefer that.
IM can convert tiff or ppm or many other formats to, well, many other formats. If you want a raw binary file with no metadata, use the prefix "gray:".
IM can convert tiff or ppm or many other formats to, well, many other formats. If you want a raw binary file with no metadata, use the prefix "gray:".
Code: Select all
convert x.tiff gray:x.bin
snibgo's IM pages: im.snibgo.com
-
- Posts: 5
- Joined: 2016-04-12T10:26:02-07:00
- Authentication code: 1151
Re: Get RAW image data (Bayer pixel array) from other formats.
Thanks for your reply!
The gray option gives me the raw data of the image correctly, but does it contain the color information?
For example, when I use this command for a .bmp image, I get the raw image in gray scale but the demosaicing gives me the same gray image, which leads to the assumption that the output of the gray: option does not contain the Red Green and Blue values , one in each pixel, and it is just a gray image.
Please correct me if I am wrong.
Here is an image suitable for demosaicing : https://www.dropbox.com/s/13oxpciozpfjq6z/CFA.png?dl=0
As you can see, the Bayer Color Filter Array is visible as a mosaic RGB pattern.
In addition, in this link https://courses.cs.washington.edu/cours ... aicing.pdf
the red green and blue subsampled components are subtracted from the lighthouse image and then combined to get the Lighthouse Bayer CFA Image which is then used for interpolation. Is there a way to do this specific process with Image Magick ?
Thanks again !
The gray option gives me the raw data of the image correctly, but does it contain the color information?
For example, when I use this command for a .bmp image, I get the raw image in gray scale but the demosaicing gives me the same gray image, which leads to the assumption that the output of the gray: option does not contain the Red Green and Blue values , one in each pixel, and it is just a gray image.
Please correct me if I am wrong.
Here is an image suitable for demosaicing : https://www.dropbox.com/s/13oxpciozpfjq6z/CFA.png?dl=0
As you can see, the Bayer Color Filter Array is visible as a mosaic RGB pattern.
In addition, in this link https://courses.cs.washington.edu/cours ... aicing.pdf
the red green and blue subsampled components are subtracted from the lighthouse image and then combined to get the Lighthouse Bayer CFA Image which is then used for interpolation. Is there a way to do this specific process with Image Magick ?
Thanks again !
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Get RAW image data (Bayer pixel array) from other formats.
Ordinary colour images contain 3 channels per pixel. Ordinary grayscale images contain one channel per pixel.Wrathchild wrote:... the output of the gray: option does not contain the Red Green and Blue values , one in each pixel, and it is just a gray image.
Bayer (mosaiced) images contain one channel per pixel. So they can be processed exactly like grayscale images. You can view them as grayscale. When enlarged, thay have a characteristic chequer-board pattern.
The single value in each pixel represents either red or green or blue. You can convert it into a colour image (3 channels/pixel) where the appropriate channel takes the original value and the other two values are zero, or intepolaed, or whatever.
My "Demosaicing" page shows how demosaicing can be done in IM.
The opposite process, making a Bayer image from an ordinary colour image, is possible.
snibgo's IM pages: im.snibgo.com
-
- Posts: 5
- Joined: 2016-04-12T10:26:02-07:00
- Authentication code: 1151
Re: Get RAW image data (Bayer pixel array) from other formats.
So the command convert img1.bmp gray:img1.raw will give me the Bayer image from the .pgm, right?
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Get RAW image data (Bayer pixel array) from other formats.
I don't know what img1.bmp is.
snibgo's IM pages: im.snibgo.com
-
- Posts: 5
- Joined: 2016-04-12T10:26:02-07:00
- Authentication code: 1151
Re: Get RAW image data (Bayer pixel array) from other formats.
First of all, I would like to thank you for being patient and helpful.
The img1.bmp https://www.dropbox.com/s/8y9mfpf2ayh12z5/img1.bmp?dl=0 is a truecolor (RGB) image , with 8bits per channel, thus 24bits per pixel.
With the above command, I get a grayscale image , 8bpp with size WidthxHeight bytes. Does this image contain one channel, either R, G or B, in each pixel?
The image viewing program that came with the dev kit I am working with, gives me an option to apply a Bayer pattern (demosaicing ) to the image. When I check that box for other, Bayer CFA images, I get the correct demosaiced image. But that does not work with the images that I get with the :gray option.
Thank you. I am sorry if my questions seem foolish. I am new to image processing and I really need to be able to get such images in order to focus on my main work.
The img1.bmp https://www.dropbox.com/s/8y9mfpf2ayh12z5/img1.bmp?dl=0 is a truecolor (RGB) image , with 8bits per channel, thus 24bits per pixel.
With the above command, I get a grayscale image , 8bpp with size WidthxHeight bytes. Does this image contain one channel, either R, G or B, in each pixel?
The image viewing program that came with the dev kit I am working with, gives me an option to apply a Bayer pattern (demosaicing ) to the image. When I check that box for other, Bayer CFA images, I get the correct demosaiced image. But that does not work with the images that I get with the :gray option.
Thank you. I am sorry if my questions seem foolish. I am new to image processing and I really need to be able to get such images in order to focus on my main work.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Get RAW image data (Bayer pixel array) from other formats.
img1.bmp is a 3-channel photograph. It may have been made by demosaicing a Bayer (mosaiced) image.
If you want to do the inverse process, to make a Bayer (mosaiced) image from this, it takes a little work. Mosaicing removes two-thirds of the data from the image. For a start, you need to specify the Bayer filter pattern. Perhaps:
Then separate the three channels, and composite the four channels (we use green twice) "Over" with masks that are appropriate for the filter pattern. The result will be a grayscale image (ie equal values in all channels), so any channel will represent the Bayer image, and writing this as "gray:img1.raw" will write the binary values, one value per pixel.
Here is a Windows BAT script that mosaics an image, making a single-channel Bayer image. The result is in mos.png. The final conversion to low-quality JPG is merely for showing in this post.
parrots_mos.jpg is:
(I've only just written this script, for this post, so it isn't thoroughly tested.)
If you want to do the inverse process, to make a Bayer (mosaiced) image from this, it takes a little work. Mosaicing removes two-thirds of the data from the image. For a start, you need to specify the Bayer filter pattern. Perhaps:
Code: Select all
R G
G B
Here is a Windows BAT script that mosaics an image, making a single-channel Bayer image. The result is in mos.png. The final conversion to low-quality JPG is merely for showing in this post.
Code: Select all
set SRC=img1.bmp
set WW=
for /F "usebackq" %%L in (`%IM%identify ^
-format "WW=%%w\nHH=%%h" ^
%SRC%`) do set %%L
if "%WW%"=="" exit /B 1
rem Assume we have even number of pixels in both dimensions.
%IM%convert ^
( xc:White xc:Black +append ) ^
( xc:Black xc:Black +append ) ^
-append ^
+write mpr:SQR +delete ^
-size %WW%x%HH% tile:mpr:SQR ^
+write msk_R.png ^
-roll +1+0 +write msk_G0.png ^
-roll +0+1 +write msk_B.png ^
-roll -1+0 +write msk_G1.png ^
NULL:
%IM%convert ^
%SRC% ^
-separate ^
+write info: ^
( -clone 0,1 ^
msk_G0.png ^
-composite ^
) ^
( -clone 3,1 ^
msk_G1.png ^
-composite ^
) ^
( -clone 4,2 ^
msk_B.png ^
-composite ^
) ^
+write info: ^
-delete 0-4 ^
mos.png
%IM%convert mos.png -quality 40 parrots_mos.jpg
(I've only just written this script, for this post, so it isn't thoroughly tested.)
snibgo's IM pages: im.snibgo.com
-
- Posts: 5
- Joined: 2016-04-12T10:26:02-07:00
- Authentication code: 1151
Re: Get RAW image data (Bayer pixel array) from other formats.
I use ImageMagick in Ubuntu 14.04. How can I run the above script?
Also, I read somewhere that a solution would be to drop 2 values of each pixel, according to the Bayer Pattern we want.
Could this be more easily done with IM ?
Also, I read somewhere that a solution would be to drop 2 values of each pixel, according to the Bayer Pattern we want.
Could this be more easily done with IM ?
Last edited by Wrathchild on 2016-04-13T07:27:05-07:00, edited 2 times in total.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Get RAW image data (Bayer pixel array) from other formats.
It has only two convert commands. You will have to translate them to bash syntax: replace each caret ^ with a backslash, escape each parenthesis, etc.
snibgo's IM pages: im.snibgo.com
Re: Get RAW image data (Bayer pixel array) from other formats.
The process to convert from RAW to RGB or other more conventional formats is, unfortunately, a one-way process. The de-mosaiciking algorithm that is used to interpolate the missing spatial data can be implemented in many different ways and every digital camera uses proprietary algorithms. A lot of other decisions - white balance, color tone, etc - are also applied in the camera while converting from raw/Bayer to RGB or YCC output which is then compressed to JPEG.
This is why professional photographers prefer to keep 'RAW' images because they still have many choices on how to process the image.
So trying to reverse-engineer from a processed JPEG or bitmap back to Bayer isn't meaningful without knowledge of how the original Bayer was processed. If you are just interested in manipulating image formats I guess its OK, but if you want to use Bayer images for any research experiments or engineering work you should start with the raw/Bayer image obtained from a camera or smartphone. Note that Android now offers RAW support and these images are available from many recent smartphones so it isn't too difficult to get RAW images these days ...
Hope that helps.
This is why professional photographers prefer to keep 'RAW' images because they still have many choices on how to process the image.
So trying to reverse-engineer from a processed JPEG or bitmap back to Bayer isn't meaningful without knowledge of how the original Bayer was processed. If you are just interested in manipulating image formats I guess its OK, but if you want to use Bayer images for any research experiments or engineering work you should start with the raw/Bayer image obtained from a camera or smartphone. Note that Android now offers RAW support and these images are available from many recent smartphones so it isn't too difficult to get RAW images these days ...
Hope that helps.