Decode CR2 with Magick++

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Zeranoe
Posts: 5
Joined: 2017-08-06T14:19:21-07:00
Authentication code: 1151

Decode CR2 with Magick++

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Decode CR2 with Magick++

Post 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?
snibgo's IM pages: im.snibgo.com
Zeranoe
Posts: 5
Joined: 2017-08-06T14:19:21-07:00
Authentication code: 1151

Re: Decode CR2 with Magick++

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Decode CR2 with Magick++

Post 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.
snibgo's IM pages: im.snibgo.com
Zeranoe
Posts: 5
Joined: 2017-08-06T14:19:21-07:00
Authentication code: 1151

Re: Decode CR2 with Magick++

Post 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
Post Reply