Page 1 of 1

Decode CR2 with Magick++

Posted: 2017-08-06T14:29:38-07:00
by Zeranoe
I'm trying to decode a raw CR2 image with Magick++ but am getting some strange results. I want to get the RGB color values for each pixel so I can loop through them and do some analysis.

To decode the CR2 I'm using:

Code: Select all

Image img;
img.read("cr2:image.CR2");
Pixels view(img);
Quantum *pixels = view.get(0,0,1,1);
cout << "R: " << 255 * QuantumScale * *pixels++ << endl;
cout << "G: " << 255 * QuantumScale * *pixels++ << endl;
cout << "B: " << 255 * QuantumScale * *pixels++ << endl;
This is just an example to get the first pixel in the image, and I will modify the code to loop through the rest once I can get this working.

For image.cr2 the program outputs:

Code: Select all

R: 5.43191
G: 0
B: 51.2179
If I modify the code to read the camera's encoded jpg, I get:

Code: Select all

R: 0
G: 0
B: 2
The RGB values appear correct for the first pixel in the image, so I'm not sure what Magick++ is doing to decode the CR2 differently. Could this be related to the Bayer filter demosaicing?

Edit: I'm using version 7.0.6.

Re: Decode CR2 with Magick++

Posted: 2017-08-06T20:43:36-07:00
by snibgo
After your "img.read", the image is not in Bayer format.

You have found that the top-left pixel of a de-bayered image is different to the top-left pixel of the in-camera JPG. Well, yes, is that surprising?

Re: Decode CR2 with Magick++

Posted: 2017-08-06T20:56:56-07:00
by Zeranoe
snibgo wrote: 2017-08-06T20:43:36-07:00 After your "img.read", the image is not in Bayer format.

You have found that the top-left pixel of a de-bayered image is different to the top-left pixel of the in-camera JPG. Well, yes, is that surprising?
I know that the decoded raw image I'm dealing with is no longer in Bayer filter format, since I assume the demosaicing happens during image.read().

It's also not surprising that it's a different pixel color, however it seems significantly different.

Re: Decode CR2 with Magick++

Posted: 2017-08-06T21:28:27-07:00
by snibgo
Are the two versions even the same size? Often the jpeg is a crop of the raw. You can save your de-bayered image, then open that and the jpeg in Gimp or other editor, and examine the pixel values.

Re: Decode CR2 with Magick++

Posted: 2017-08-07T06:07:46-07:00
by Zeranoe
The camera encoded JPEG looks slightly smaller (4368x2912) than the raw CR2 image (4386x2920).

However the CR2 to BMP image is much brighter:
Image

than the camera JPEG:
Image