Domanda

I'm using Visual Studio 2010, C++.

What I'm trying to do is retrieve pixel data (RGB colour specifically) from a loaded image and then use that in various checks. Currently, I want to loop through all the data and be able to detect when a pixel is a certain colour (specific R, G and B values).

I should note that I'm using DevIL, and I'd prefer to keep using DevIL (I've seen a bunch of suggestions to do with other tools that help with image processing).

Right now, as I understand it, the DevIL function ilgetData() retrieves a pointer that has all the RGB values of all the pixels in a single dimensional array of bytes. I don't know how to store the data gotten using this function and then to use it in a for loop.

Example code is much appreciated.

È stato utile?

Soluzione

The correct way to store the return of ilGetData() is in an IL unsigned byte array.

For example

ILubyte * bytes = ilGetData();

To iterate over it in a for loop you use

  for(int i = 0; i < size; i++)
  {
    // Print out each ILubyte one at time
    printf("%d\n", bytes[ i ]);
  }

What you probably want is a for loop that prints or does something else with the RGB values at given pixel for all pixels in the image. As Bart pointed out the ordering below is for IL_RGBA or RGBA format. If you use IL_RGB then the 4 becomes a 3, and if you use IL_BGR or IL_BGRA then red and blue values need to be switched as expected.

ILuint width,height;
width = ilGetInteger(IL_IMAGE_WIDTH);
height = ilGetInteger(IL_IMAGE_HEIGHT);

for (int i = 0; i < height; i++)
{
   for (int j = 0; j < width; j++)
   {
      printf( "%s\n", "Red Value for Pixel");
      printf( "%d\n", bytes[(i*width + j)*4 + 0]); 
      printf( "%s\n", "Green Value for Pixel");
      printf( "%d\n", bytes[(i*width + j)*4 + 1]);
      printf( "%s\n", "Blue Value for Pixel");
      printf( "%d\n", bytes[(i*width + j)*4 + 2]);
   }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top