How to match similar colours in Java using getRGB

三世轮回 提交于 2019-12-05 06:16:23
Tim Gee

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

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.

ChristophMa

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!