Получение RGB оценивает за каждый пиксель из необработанного изображения в C

переключатель оператор, конечно, намного более симпатичный затем все они if's и else's.

5
задан 29 October 2009 в 06:54
поделиться

3 ответа

Assuming the image is w * h pixels, and stored in true "packed" RGB format with no alpha component, each pixel will require three bytes.

In memory, the first line of the image might be represented in awesome ASCII graphics like this:

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

Here, each Rn Gn and Bn represents a single byte, giving the red, green or blue component of pixel n of that scanline. Note that the order of the bytes might be different for different "raw" formats; there's no agreed-upon world standard. Different environments (graphics cards, cameras, ...) do it differently for whatever reason, you simply have to know the layout.

Reading out a pixel can then be done by this function:

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];
}

Notice how the height of the image is not needed for this to work, and how the function is free from bounds-checking. A production-quality function might be more armor-plated.

Update If you're worried this approach will be too slow, you can of course just loop over the pixels, instead:

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. */
  }
}
7
ответ дан 18 December 2019 в 14:48
поделиться

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

That's the general procedure, for your current format you'll have to check the details.

1
ответ дан 18 December 2019 в 14:48
поделиться

Ни один из опубликованных до сих пор методов, вероятно, не будет работать с файлом "raw" камеры. Форматы файлов для необработанных файлов являются собственностью каждого производителя и могут содержать данные экспозиции, калибровочные константы и информацию о балансе белого в дополнение к данным пикселей, которые, вероятно, будут в упакованном формате, где каждый пиксель может занимать более одного байт, но меньше двух.

Я уверен, что существуют программы-конвертеры необработанных файлов с открытым исходным кодом, с которыми вы могли бы проконсультироваться, чтобы узнать, какие алгоритмы использовать, но я не знаю ни одной из них, придуманной мной.


Просто подумал о дополнительном осложнении. Необработанный файл не хранит значения RGB для каждого пикселя. Каждый пиксель записывает только один цвет. Два других цвета должны быть интерполированы из соседних пикселей. Вы' Определенно будет лучше найти программу или библиотеку, которая работает с вашей камерой.

5
ответ дан 18 December 2019 в 14:48
поделиться
Другие вопросы по тегам:

Похожие вопросы: