2

I need to get the color of a pixels in a specifict region of image. Im using this script in python:

import cv2

image = cv2.imread('abc.jpg')

color = image[100,50]

print(color) # gives me the RGB color (12,156,222)

and if a need to get the hex of it:

hex = (color[0] << 16) + (color[1] << 8) + (color[2])

my question is: There is a way to tell me what color is it? (12,156,222) thank you.

Jasar Orion
  • 546
  • 4
  • 18
  • "color" meaning as a hue in degrees on a color wheel, or color described as a word? – Christoph Rackwitz Nov 29 '20 at 18:28
  • Not all rgb colors have names. You would have to create a list of color names and their associated rgb values and find the closest named color to your rgb values. See the CSS list of named colors. – fmw42 Nov 29 '20 at 19:04

2 Answers2

0

You can look it up at: https://shallowsky.com/colormatch/index.php?r=12&g=156&b=222

[spoiler alert: "DeepSkyBlue3"]

In general, you want to start with

  1. a list of name → R,G,B values, and
  2. a distance function

Then it's a matter of computing distance between each of those and your target value. Return the corresponding name for whichever list entry has minimum distance to target.

A previous SO answer offers such an algorithm: Python - Find similar colors, best way

Using Euclidean distance (L2 norm) in some random color space is less than principled, though often sufficient. If you want to sweat the details, consider relying on https://python-colormath.readthedocs.io. (You might even want to discuss maintainership with the author.)

If "perceptually similar" matters to you, definitely use Lab: https://en.wikipedia.org/wiki/CIELAB_color_space

J_H
  • 9,521
  • 1
  • 16
  • 31
  • its to advanced for me :( could you help me or give me some python example? – Jasar Orion Nov 30 '20 at 11:30
  • I cited a brief python solution: https://stackoverflow.com/questions/8863810/python-find-similar-colors-best-way#62675780 – J_H Dec 01 '20 at 16:33
0

I found another way to solve this problem using webcolor i was able to detect the closest color

import webcolors
import time
start = time.time()
def closest_colour(requested_colour):
    min_colours = {}
    for key, name in webcolors.CSS3_HEX_TO_NAMES.items():
        r_c, g_c, b_c = webcolors.hex_to_rgb(key)
        rd = (r_c - requested_colour[0]) ** 2
        gd = (g_c - requested_colour[1]) ** 2
        bd = (b_c - requested_colour[2]) ** 2
        min_colours[(rd + gd + bd)] = name
    return min_colours[min(min_colours.keys())]

def get_colour_name(requested_colour):
    try:
        closest_name = actual_name = webcolors.rgb_to_name(requested_colour)
    except ValueError:
        closest_name = closest_colour(requested_colour)
        actual_name = None
    return actual_name, closest_name

requested_colour = (255, 0, 0)
actual_name, closest_name = get_colour_name(requested_colour)

print("Actual colour name:", actual_name, ", closest colour name:", closest_name)
print("Tempo: ", time.time() - start)

if anyone have another bester method please send here ^^

Jasar Orion
  • 546
  • 4
  • 18