Sign in to follow this  
frogtag

Reading the RGB values of a pixel

Recommended Posts

frogtag    121
Can anyone help me with how i can read the RGB values of a pixel of a picture? What i want to do is... 1) Load a picture, the formats not specific as i don't know whats best. 2) Read a pixels RGB values, i've know idea how this would be read, line by line or co-ordinate specific.

Share this post


Link to post
Share on other sites
relsoft    255
This is in 32 bit...


r = pixel shr 16
g = pixel shr 8 and 255
b = pixel and 255




or


r = pixel >> 16 & 255
g = pixel >> 8 & 255
b = pixel & 255


Share this post


Link to post
Share on other sites
benryves    1999
You would need to tell us which API/language you were intending on using.

In C# it's all very easy (though this method is slow);

using (Bitmap B = new Bitmap("someimage.jpg")) {

Color C = B.GetPixel(10, 10); // Get the pixel colour at (10,10)

int Red = C.R;
int Green = C.G;
int Blue = C.B;
int Alpha = C.A;

}



If you intend on reading lots of pixels you'll end up having to manually work with the raw data, which depends on the pixel format of the image. relsoft's method works for images using 32-bit RGB/ARGB values; if the bits were packed differently (eg 15/16-bit graphics, or worse palettised images) you'd need to work around that. Depending on which graphics API you decide to use, you might be able to just convert the image to 32-bit RGB/ARGB which is the "typical" (and easiest to manipulate) format.

Share this post


Link to post
Share on other sites
frogtag    121
I'm not quite sure what your code is relsoft. I've not had much experience with this before and so your code is too sparse that it makes no sense to me, can you put it in context like benryves did?

I see it as a mixture of the two, like follows...?????

using (Bitmap B = new Bitmap("someimage.jpg")) {

Color C = B.glReadPixel(10, 10); // Get the pixel colour at (10,10)

int C.r = pixel >> 16 & 255
int C.g = pixel >> 8 & 255
int C.b = pixel & 255
}

And that makes no logical sense in the history of programming or sanity!

Share this post


Link to post
Share on other sites
relsoft    255
Okay, if your bmp is already loaded on a GL context ( A texture) you could just read each pixel in the GL buffer via glreadpixels.


glEnable(GL_TEXTURE_2D)
glBindtexture(GL_TEXTURE_2D, texture_id)
glReadPixels(0, 0, wid, hei, GL_RGBA, GL_UNSIGNED_BYTE, memoryaddress)



Sometimes you can even do it directly, w/o GL at all.

Say you have a memory chunk that you loaded you image. For clarity I'l use your code:


using (Bitmap B = new Bitmap("someimage.jpg")) {

Color C = B.GetPixel(10, 10); // Get the pixel colour at (10,10)

char Red = C >> 16 & 255;
char Green = C >> 8 & 255;
char Blue = C & 255 ;
char Alpha = C >> 24 & 255;

}



Share this post


Link to post
Share on other sites
benryves    1999
For clarity, the code I posted was the .NET way of doing things which therefore wraps up a lot of the mucky details for you.

If you have an image loaded into memory (let's say you have a pointer to its data) you need to know several things; the pixel format, the width, the height, and the stride of the image.

The width and height should be fairly self-explanatory. The pixel format indicates how each pixel is defined; for a 32-bit image that's one byte alpha, one byte red, one byte green blue, one byte green (sometimes alpha is ignored, so you have a dud byte there).

The stride of the image is the width of the image in bytes (the size of a scanline). This is not always the same as the width multiplied by the nuber of bytes per pixel; it is often rounded up to the nearest four or eight bytes, for example (and the extra data is ignored when drawing the image).

Therefore, to get the location of a pixel within a buffer in memory you'd calculate it like this:

PixelAddress = (y * Stride) + (x * BytesPerPixel)


This assumes that the image is stored with the first byte of data corresponding to the top-left pixel. Some formats store the bottom-right first (so the image grows "up" rather than "down"), for which you'd need to adjust the y coordinate first.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this