Getting RGB values ​​for each pixel from a raw image in C

I want to read the RGB values ​​for each pixel from a raw image. Can someone tell me how to do this? Thanks for the help!

the format of my raw .CR2 image that comes from the camera.

+5
source share
3 answers

None of the methods published so far will most likely work with a raw camera file. The file formats for raw files are the property of each manufacturer and may contain exposure data, calibration constants, and white balance information in addition to pixel data, which is likely to be in a packed format where each puxel occupies more than one byte but less than two.

I'm sure there are open source programs for open source files that you could consult to find out which algorithms to use, but I don't know anything out of my head.


. RGB . . . , .

+5

, w * h "" RGB -, .

​​ ASCII :

   R0 G0 B0 R1 G1 B1 R2 G2 B2 ... R(w-1) G(w-1) B(w-1)

Rn Gn Bn , , n . , "" ; . ( , ,...) - - , .

:

typedef unsigned char byte;
void get_pixel(const byte *image, unsigned int w,
               unsigned int x,
               unsigned int y,
               byte *red, byte *green, byte *blue)
{
    /* Compute pointer to first (red) byte of the desired pixel. */
    const byte * pixel = image + w * y * 3 + 3 * x;
    /* Copy R, G and B to outputs. */
    *red = pixel[0];
    *green = pixel[1];
    *blue = pixel[2];
}

, , . .

, , , , :

unsigned int x, y;
const byte *pixel = /* ... assumed to be pointing at the data as per above */

for(y = 0; y < h; ++y)
{
  for(x = 0; x < w; ++x, pixel += 3)
  {
    const byte red = pixel[0], green = pixel[1], blue = pixel[2];

    /* Do something with the current pixel. */
  }
}
+4

A RAW image is an uncompressed format, so you just need to indicate where your pixel is (skipping any possible header and then adding a pixel size times the number of columns times the number of rows and the number of columns) and then read any binary data giving a meaningful format to the data layout (with masks and shifts, you know).

In the general procedure for your current format, you will need to check the details.

+1
source

All Articles