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?
To compare two Color objects you can use the equals() method : Color « 2D Graphics « Java Tutorial. 16.10. 1.
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*₂)².
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With