We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › Compare pixels color value, and find closest
Page Index Toggle Pages: 1
Compare pixels color value, and find closest (Read 2189 times)
Compare pixels color value, and find closest
Apr 6th, 2009, 3:21am
 
Hi all,

I'm actually working on a project where I need to grab the pixels from a webcam and turn it to kind of "8 colors lo-fi screen".

Basically I have the eight colors in an array and I'm comparing pixel by pixel the camera data, in order to find the closest color of the array in each pixel.

I can't figure out how to find the color value of a color.

I tried to make this, getting distance value, for each pixel and each color in the array, and get the tiniest one at the end, but the result is kind of weird.

Any solution ?

I hope to be clear.

Martial


Code:
int compare( int colour1, int colour2 ) 
{

int currR = (colour1 >> 16) & 0xFF;
int currG = (colour1 >> 8) & 0xFF;
int currB = colour1 & 0xFF;

int currR2 = (colour2 >> 16) & 0xFF;
int currG2 = (colour2 >> 8) & 0xFF;
int currB2 = colour2 & 0xFF;

int distance = 0;
distance += Math.pow(currR - currR2, 2);
distance += Math.pow(currG - currG2, 2);
distance += Math.pow(currB - currB2, 2);
return distance ;
}



Get the source here : http://www.164-prod.com/lab/8colors/eightColorProto.pde

Thanks !

Martial
Re: Compare pixels color value, and find closest
Reply #1 - Apr 6th, 2009, 3:37am
 
third time this question has been asked. am beginning to think it's homework...

but wait, you've posted code, that always helps 8)

you're doing it roughly the way i would. but i think you have a problem in getClosestColor():

Code:

if ( distance < oldDist ) {
colorTo = colorTarget;
}
oldDist = distance;


i think you need to remember distance ONLY if it's less than the previous least distance, ie you need to move that last line into the block for the condition.
Re: Compare pixels color value, and find closest
Reply #2 - Apr 6th, 2009, 3:46am
 
also:

http://processing.org/reference/dist_.html

there's a built-in distance method, which will save you 4 lines of code.

i also think that given your target palette there'll be a faster way of working out the target colour for each pixel, one that doesn't need you to check 8 values every time. basically if your r, g or b component is > 128 then map it to FF, if it's less map it to 00. your target palette is the corners of the colour cube so you just need to work out which octant your source colour is in and then map it to the corner. kind of a specialised solution though, won't work if you change the palette.
Re: Compare pixels color value, and find closest
Reply #3 - Apr 22nd, 2009, 1:59pm
 
Hi!

Sorry for the delay, I did not received any notification, don't know why.

Well, thanks! It works. Why did I not think that before ?

doh!

Thanks for all !!
Page Index Toggle Pages: 1