Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match similar colours in Java using getRGB

I am taking screenshots of the screen using robot and then searching for smaller images within those screenshots. This works on Windows but not OS X because of gamma correction. The best solution I can come up with is to simply match similar colours instead of exact color matches.

My fear is that matching similar colours will mean going beyond getRGB therefore will slow down my program (because it's taking multiple screenshots and comparing them to a smaller image to search for a match very quickly).

My question is, lets say I had BufferedImage Screenshot and BufferedImage smallImage, how would I go about determining if Screenshot.getRGB(1,1) and smallImage.getRGB(1,1) are similar colours?

like image 833
David Zorychta Avatar asked Dec 31 '11 14:12

David Zorychta


People also ask

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.

How do you find the difference between two colors?

In order to measure the difference between two colors, the difference is assigned to a distance within the color space. In an equidistant-method color space, the color difference ∆E can be determined from the distance between the color places: ΔE = √ (L*₁-L*₂)² + (a*₁-a*₂)² + (b*₁-b*₂)².


1 Answers

There's an interesting paper on exactly this problem:

A New Perceptually Uniform Color Space with Associated Color Similarity Measure for Content-Based Image and Video Retrieval by M. Sarifuddin and Rokia Missaoui

You can find this easily using Google or in particular Google Scholar.

To summarise, some color spaces (e.g. RGB, HSV, Lab) and distance measures (such as Geometric mean and Euclidean distance) are better representations of human perception of color similarity than others. The paper talks about a new color space, which is better than the rest, but it also provides a good comparison of the common existing color spaces and distance measures. Qualitatively*, it seems the best measure for perceptual distance using commonly available color spaces is : the HSV color space and a cylindrical distance measure.

*At least, according to Figure 15 in the referenced paper.

The cylindrical distance measure is (in Latex notation):

D_{cyl} = \sqrt{\Delta V^{2}+S_1^{2}+S_2^{2}-2S_1S_2cos(\Delta H)}

Also note there are some similar questions that address the same issue:

finding similar colors programatically

"Distance" between colours in PHP

Finally, in Java, there's any easy way to convert from RGB values into other color spaces:

ColorSpace.fromRGB

like image 165
Tim Gee Avatar answered Sep 21 '22 13:09

Tim Gee