# RGB woes

Hello OF People,

I stumbled across an interesting problem and I’d thought I get directions from here. Now I do have an image in photoshop and I played with its colors and I want to have the exact same color formation with my camera through OF. Now what I did was, I sampled the darkest pixel in the image and also sampled the brightness pixel of this image.

The values are below:
brightest
R178 G149 B88

darkest
R17 G8 B2

I assumed I need to scale down my RGB values I am getting from the camera just like this brightest for the R should 178 darkest should be 17, brightest G should be 149, darkest R should be 8 and brightest B should be 88 and darkest should be 2.

Now I am getting R-G-B values with getPixel() and thanks to Jeremy, I used the below function to scale down the values:

``````

int testApp::scale(int value, int input_min, int input_max, int output_min, int output_max) {
return ((value - input_min) * 100 / (input_max - input_min) * (output_max - output_min) + output_min)/100;
}

``````

basically what I have is this:

``````

for (int i = 0; i < camWidth; i++){
for (int j = 0; j < camHeight; j++){
colorPixels[(j*camWidth+i)*3 + 0] = scale(((pixels[(j*camWidth+i)*3 + 0]+pixels[(j*camWidth+i)*3 + 1]+pixels[(j*camWidth+i)*3 + 2])/3), 0, 255, 17, 178);
colorPixels[(j*camWidth+i)*3 + 1] = scale(((pixels[(j*camWidth+i)*3 + 0]+pixels[(j*camWidth+i)*3 + 1]+pixels[(j*camWidth+i)*3 + 2])/3), 0, 255, 8, 149);
colorPixels[(j*camWidth+i)*3 + 2] = scale(((pixels[(j*camWidth+i)*3 + 0]+pixels[(j*camWidth+i)*3 + 1]+pixels[(j*camWidth+i)*3 + 2])/3), 0, 255, 2, 88);
}
}

``````

unfortunately the color I am getting is similar but not the exact formation that I am looking for. Actually, when I take a screenshot and put this in photoshop I am still getting darkest areas with 0,0,0. which should be 17,8,2. no? I am wondering if I am doing anything wrong with my approach to the problem.

I would appreciate any directions!

ilteris.

i’m not so convinced about your scale() function. in my mind it should be this:

``````
float value_percent = (value-input_min) / (input_max-input_min);
float output = value_percent * (output_max-output_min) + output_min;
return output;

``````

i can’t figure out whether this is what the scale() function is doing already: there’s too many brackets, and i’m not sure what the 100’s are doing there.

also, are you sure you haven’t mixed up the input min/max and output min/max?

Hey, might the issue be that your camera is sending out TV colors (which are clamped a few color values on either end) instead of PC monitor colors (which are 0-255).

hey damian,
they are there for converting those numbers to integers. because I don’t want to get any kind of floating numbers in there because as you can imagine pixels are unsigned integers.

I tried it with your code but unfortunately nothing shows up on my screen. so I am assuming it’s because the numbers are floats. what do you think?

thanks
ilteris.

[quote author=“damian”]i’m not so convinced about your scale() function. in my mind it should be this:

``````
float value_percent = (value-input_min) / (input_max-input_min);
float output = value_percent * (output_max-output_min) + output_min;
return output;

``````

i can’t figure out whether this is what the scale() function is doing already: there’s too many brackets, and i’m not sure what the 100’s are doing there.

also, are you sure you haven’t mixed up the input min/max and output min/max?[/quote]

hey shotgunninja,

the camera is a firewire unibrain fire-i vga camera. Interesting you brought this up. Have you heard anything about this specific camera? I am also having a hard time focusing. I can focus from the ring on the lens but it never gets sharp enough.

any thoughts.

best,
ilteris.

right, yes, the black will probably be because it’s all integer math, so that when i go (value-input_min)/(input_max-input_min) it’ll be something like 185/255, which is 0 in integer math. try this instead:

``````
float value_percent = (float)(value-input_min) / (input_max-input_min);
float output = value_percent * (output_max-output_min) + output_min;
return output;

``````

normally you don’t have to worry about doing float->int conversions, since eventually the compiler will just force the float to be an int when it needs to be. also, the way you had it, multiplying by 100 will prematurely cast it to an int with a precision of 0…100, so you’re cutting down from 8 bits per colour component to about 6-and-a-half bits per colour component.

the one to watch out for is making sure you cast ints to floats when you need to, especially if you’re dealing with numbers between 0 and 1 (which is a good idea usually, because then you can apply sqrt() or pow() or whatever funky functions to it to change the curve if you want to).

dude, you rock!