Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding similar colors programmatically

Tags:

java

colors

I have a buffered image in java and I want to record how similar each pixel is to another based on the color value. so the pixels with 'similar' colors will have a higher similarity value. for example red and pink will have a similarity value 1000 but red and blue will have something like 300 or less.

how can I do this. when I get the RGB from a buffered Image pixel it returns a negative integer I am not sure how to implement this with that.

like image 609
anon Avatar asked Nov 12 '09 21:11

anon


People also ask

How do you compare two colors?

The most common method would be a visual color comparison by looking at two physical color samples side by side under a light source. Color is very relative, so you can compare colors in terms of the other color across dimensions such as hue, lightness and saturation (brightness).

How do you compare two colors in Java?

To compare two Color objects you can use the equals() method : Color « 2D Graphics « Java Tutorial. 16.10. 1.


2 Answers

First, how are you getting the integer value?

Once you get the RGB values, you could try

((r2 - r1)2 + (g2 - g1)2 + (b2 - b1)2)1/2

This would give you the distance in 3D space from the two points, each designated by (r1,g1,b1) and (r2,g2,b2).

Or there are more sophisticated ways using the HSV value of the color.

like image 105
lavinio Avatar answered Oct 28 '22 20:10

lavinio


HSL is a bad move. L*a*b is a color space designed to represent how color is actually percieved, and is based on data from hundreds of experiments involving people with real eyes looking at different colors and saying "I can tell the difference between those two. But not those two".

Distance in L*a*b space represents actual percieved distance according to the predictions derived from those experiments.

Once you convert into L*a*b you just need to measure linear distance in a 3D space.

like image 21
Breton Avatar answered Oct 28 '22 19:10

Breton