Finding the closest RGB color

I was told to use the distance formula to find if the color matches another, so I,

struct RGB_SPACE { float R, G, B; }; RGB_SPACE p = (255, 164, 32); //pre-defined RGB_SPACE u = (192, 35, 111); //user defined long distance = static_cast<long>(pow(uR - pR, 2) + pow(uG - pG, 2) + pow(uB - pB, 2)); 

this only gives distance, but how can I find out if the color matches the custom value by at least 25%?

I'm not just sure, but I have an idea to check each color value to see if the difference is 25%. eg.

 float R = uR/pR * 100; float G = uG/pG * 100; float B = uB/pB * 100; if (R <= 25 && G <= 25 && B <= 25) { //color matches with pre-defined color. } 
+5
source share
3 answers

I would suggest not checking the RGB space. If you have (0,0,0) and (100,0,0), they are similar in cababungas formula (and also according to casablanca, which considers too many colors to be similar). However, they LOOK very differently.

HSL and HSV color models are based on a human interpretation of colors, and then you can easily determine the distance for hue, saturation, and brightness independently of each other (depending on what “looks like” means in your case).

+7
source

“At least 25% matches” is not a well-defined problem. Matches at least 25% of what, and by what metric? There are many possible options. If you are comparing RGB colors, distance metrics derived from vector norms are obvious. The three most important of them are:

  • 1-norm or Manhattan distance: distance = abs (r1-r2) + abs (g1-g2) + abs (b1-b2)
  • 2-normal or Euclidean distance: distance = sqrt (pow (r1-r2, 2) + pow (g1-g2, 2) + pow (b1-b2, 2)) (you calculate the square of this, which is fine - you can avoid sqrt if you just check the threshold by squaring the threshold too)
  • Infinity-norm: distance = max (abs (r1-r2), abs (g1-g2), abs (b1-b2))

There are many other possibilities, of course. You can check if they are at some distance from each other: if you want to allow a difference of 25% (over the range of possible RGB values) in one color channel, the threshold values ​​used for 3 methods are 3/4 * 255 , sqrt (3) / 4 * 255 and 255/4, respectively. This is a very crude metric.

The best way to measure the distance between colors is to convert your colors into a perceived single color space, such as CIELAB and compare there; there is a pretty good Wikipedia article on this. This may be redundant depending on your intended application, but these are color spaces where the measured distances have the best correlation with the distances perceived by the human visual system.

+4
source

Please note that the maximum possible distance is between (255, 255, 255) and (0, 0, 0), which are at a distance of 3 * 255^2 . Obviously, these two colors correspond to the smallest (0% match), and they are at a distance of 100% from each other. Then at least a 25% match means a distance of less than 75%, i.e. 3/4 3 / 4 * 3 * 255^2 = 9 / 4 * 255 * 255 . So you can just check if there is:

 distance <= 9 / 4 * 255 * 255 
+2
source

Source: https://habr.com/ru/post/905978/


All Articles