给定RGB或十六进制中的颜色,如何找到与其相似的颜色?类似地,我的意思是,它们应该用小值来区分。
发布于 2017-06-13 10:09:18
RGB颜色空间依赖于设备,且在感知上不均匀。如果您打算测量颜色的相似性,您应该首先将RGB值转换为一个独立于设备的颜色空间,例如CIELAB。然后,您可以通过适当的相似性度量来度量颜色差异,比如达美实验室。
演示
考虑一下这幅图像
让我们假设您的参考颜色是绿色和洋红色,其RGB值为[0, 160, 0]
和[120, 0, 140]
。下面的片段标识那些相对于参考颜色的Delta低于某个阈值的图像像素(分别为15
和20
)。
import numpy as np
from skimage import io
from skimage.color import rgb2lab, deltaE_cie76
rgb = io.imread('https://i.stack.imgur.com/npnrv.png')
lab = rgb2lab(rgb)
green = [0, 160, 0]
magenta = [120, 0, 140]
threshold_green = 15
threshold_magenta = 20
green_3d = np.uint8(np.asarray([[green]]))
magenta_3d = np.uint8(np.asarray([[magenta]]))
dE_green = deltaE_cie76(rgb2lab(green_3d), lab)
dE_magenta = deltaE_cie76(rgb2lab(magenta_3d), lab)
rgb[dE_green < threshold_green] = green_3d
rgb[dE_magenta < threshold_magenta] = magenta_3d
io.imshow(rgb)
发布于 2021-10-26 09:15:36
基于托内查的回答,
要具有类似的颜色检测值(而不是可视化):
import numpy as np
from skimage import io
from skimage.color import rgb2lab, deltaE_cie76
def get_pct_color(img_rgb, rgb_color, threshold=10):
img_lab = rgb2lab(img_rgb)
rgb_color_3d = np.uint8(np.asarray([[rgb_color]]))
rgb_color_lab = rgb2lab(rgb_color_3d)
delta = deltaE_cie76(rgb_color_lab, img_lab)
x_positions, y_positions = np.where(delta < threshold)
nb_pixel = img_rgb.shape[0] * img_rgb.shape[1]
pct_color = len(x_positions) / nb_pixel
return pct_color
举个例子:
img_rgb = io.imread('https://i.stack.imgur.com/npnrv.png')
green = [0, 160, 0]
get_pct_color(img_rgb, green, 10)
# 0.016131591796875
get_pct_color(img_rgb, green, 250)
# 1.0
https://stackoverflow.com/questions/44428315
复制相似问题