Hi there,

We have a tiny problem, and hopefully you guys can help us out. Here’s what we need:

• Load an image (500 x 500 px)
• Read RGB values for every pixel in the picture
• Calculate the average of the RGB values
• Write the RGB average value to a text file

So, we’re not sure how to do this - do you have any ideas? Any help would be appreciated.

Best regards,
Peter

check imageLoaderExample half the job is done .

Yeah, that’s true. I tried using getPixels() to grab the RGB values, but the problem is that it writes out the interlaced RGB value, and I need the seperate R, G and B values. How can I do that?

hi, this link may be helpful to you. It discusses how to extract RGB values…
http://forum.openframeworks.cc/t/learning-from-app-examples/2778/3

and this http://wiki.openframeworks.cc/index.php-…-ver-pixels in wiki

Thanks, dude!
That’s awesome. It works like a charm!

The images that I load are 16 bit, and thus the values printed should also be 16 bit RGB values. Is that possible?

[quote author=“pshoeg”]Thanks, dude!
That’s awesome. It works like a charm!

The images that I load are 16 bit, and thus the values printed should also be 16 bit RGB values. Is that possible?[/quote]

hmm…

check out this code (copied from the wiki)

``````

ofTexture* invert_color_image(ofImage* im) {
int bytes_per_pixel = im->bpp / 8;
if (im->type != OF_IMAGE_COLOR || bytes_per_pixel != 3)
// This function only works on RGB images.
return NULL;

int w = im->width;
int h = im->height;

// (t) will hold the result of the processing that we do on (im).
ofTexture* t = new ofTexture();
t->allocate(w, h, GL_RGB);

unsigned char* im_pixels = im->getPixels(); // These are the pixel data of im,
unsigned char* p = new unsigned char[w*h*bytes_per_pixel]; // and here's where we keep our processed pixel data.
for (int i = 0; i < w*h; i++) {
int base = i * bytes_per_pixel;

// Assuming the pixels come in RGB order, here we grab the intensity
// (a value in the range [0,255]) of each color channel of the image.
unsigned char r = im_pixels[base + 0];
unsigned char g = im_pixels[base + 1];
unsigned char b = im_pixels[base + 2];

p[base + 0] = 255 - r; // Invert R.
p[base + 1] = 255 - g; // Invert G.
p[base + 2] = 255 - b; // Invert B.
}
t->loadData(p, w, h, GL_RGB); // This copies our processed pixel data into a texture that can be displayed.
delete [] p; // Because loadData performed a copy, we no longer need to store the processed pixel data.

return t;
}

``````

As you see r,g,b are represented by unsigned char (http://en.wikipedia.org/wiki/Integer-%2-…-science%29). So, that makes them 8 bit wide.

But, the bits used to represent the integral data types may differ on different cpu architectures.