How to match similar colours in Java using getRGB

2019-04-27 12:33发布

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?

3条回答
乱世女痞
2楼-- · 2019-04-27 12:36

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

查看更多
beautiful°
3楼-- · 2019-04-27 12:38

are you using this getRGB Method? If yes I would compare the array elements 1 to 3 (RGB) with each other by subtracting them and checking if the absolute value of this is below a given threshold (e.g. 10). So it would be something like

if(Math.abs(firstPixel[1]-secondPixel[1])<10) 
   redIsSimilar = true; 
else 
   redIsSimilar=false;

without if statement:

redIsSimilar = Math.abs(firstPixel[1]-secondPixel[1]) < 10;

Of course you need to copy paste this for green and blue as well. Hope this helps, Christoph

查看更多
Evening l夕情丶
4楼-- · 2019-04-27 12:41

For color similarity, let DR=R1-R2, DG=G1-G2, DB=B1-B2, then calculate DR×DR+DG×DG+DB×DB and take the square root of that, and compare it to some threshold value. Of course, this can be optimized by simply skipping the square root part and just squaring the threshold.

But you might be better off if you manage to somehow get OSX to render a PNG containing your image on-screen, then read that part of the screen, and then look for an exact match against that.

查看更多
登录 后发表回答