1
$\begingroup$

I have a module that estimates the color of an object and returns an RGB value in this format: (40, 48, 68) which corresponds to this color:

enter image description here

Now I have to classify it to one of 8 different color classes as defined by a human:

Black, Brown, Blue, White, Red, Yellow, Green, Orange

For instance, the color above most resembles a Black color. And we can assume we have the RGB value of the preset colors.

Without use of a color classifier, what is an efficient heuristic to find the best class given an RGB value? I'm looking for any ideas. Also, does it help converting it to HSV?

$\endgroup$
1
  • 2
    $\begingroup$ Using HSV rather than RGB probably helps, as HSV is more representative of how human visions works. $\endgroup$
    – Lelouch
    Commented Feb 29 at 15:18

1 Answer 1

1
$\begingroup$

Assuming your colors are tuples, you could just use Euclidean distance, no? Something along the lines of :

red = (255,0,0)
blue = (0,0,255)
green = (0,255,0)

colors = [red, blue, green]



def find_closest_color_to(c):
min_distance = 99999999999
for color in colors:
    distance = math.sqrt((c[0]-color[0])**2 + (c[1]-color[1])**2 + (c[2]-color[2])**2)
    print(f"distance = {distance}")
    if distance < min_distance:
        closest_color = color
        min_distance = distance
return closest_color
$\endgroup$
3
  • $\begingroup$ this is commonsense answer although for colors it might make a color appear close to another which may seem awkward. But is a valid proposal. $\endgroup$
    – Nikos M.
    Commented Mar 1 at 10:22
  • $\begingroup$ Yes, this is the solution I came up initially. I was thinking if there could be any other heuristics, or other color spaces like HSV to make it more accurate. $\endgroup$
    – Mary
    Commented Mar 1 at 21:20
  • $\begingroup$ So what are the problems you are having if you have already implemented this approach? If you can describe what you've done and the issues that you've encountered, that will help us help you better. Transforming to another color space is certainly possible and easy to do, but without knowing what problems you're seeing it might or might not help. $\endgroup$ Commented Mar 2 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.