比较颜色相似度,RGB空间转Lab空间

import numpy as np
from colormath.color_objects import LabColor, sRGBColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000

np.set_printoptions(np.inf)


class Color(object):
    def __init__(self, color_file = 'color_list.txt'):
        self.color_name_en = []
        self.color_name_zh = []
        self.color_HEX = []
        self.color_RGB = []
        self.color_Lab = []

        self.readColorFile(color_file)


    def readColorFile(self, color_file):
        with open(color_file, 'r', encoding='utf-8') as f:
            colors = f.readlines()

        for color in colors:
            lc = color.split('\t')

            self.color_name_en.append(lc[0].strip())
            self.color_name_zh.append(lc[1].strip())
            self.color_HEX.append(lc[2].strip())

            rgb = lc[3].strip().split(',')
            rgb = [int(v) for v in rgb]
            self.color_RGB.append(rgb)

        self.rgbToLab()


    def rgbToLab(self):
        for rgb in self.color_RGB:
            srgb = sRGBColor(*rgb)
            lab = convert_color(srgb, LabColor)
            self.color_Lab.append(lab)


    def getMinCIE2000Distance(self, rgb):
        srgb = sRGBColor(*rgb)
        tlab = convert_color(srgb, LabColor)

        min_index = -1
        min_delta_e = np.inf
        for idx, lab in enumerate(self.color_Lab):
            delta_e = delta_e_cie2000(tlab, lab)
            if delta_e < min_delta_e:
                min_delta_e = delta_e
                min_index = idx

        return min_delta_e, min_index


    def calculateRGBDistance(self, rgb=(0,0,0)):
        array_rgb = np.array(self.color_RGB)
        print(array_rgb)
        print(array_rgb.shape)

        temp1_rgb = (rgb - array_rgb) / 255
        temp2_rgb = temp1_rgb[:,0]*temp1_rgb[:,0] + temp1_rgb[:,1]*temp1_rgb[:,1] + temp1_rgb[:,2]*temp1_rgb[:,2]


    def __getitem__(self, index):
        info = f"英文代码 : {self.color_name_en[index]}\n"
        info += f"形象颜色 : {self.color_name_zh[index]}\n"
        info += f"HEX格式 : {self.color_HEX[index]}\n"
        info += f"RGB格式 : {self.color_RGB[index]}\n"
        info += f"Lab格式 : {self.color_Lab[index]}\n\n"

        return info



    def __len__(self):
        assert len(self.color_name_en) == len(self.color_name_zh)
        assert len(self.color_name_en) == len(self.color_HEX)
        assert len(self.color_name_en) == len(self.color_RGB)
        return len(self.color_name_en)



if __name__ == '__main__':
    c = Color()
    rgb = (123, 145, 111)
    delta_e, index = c.getMinCIE2000Distance(rgb)
    print(delta_e, index)
    print(c[index])

CIELAB色差计算_lanmengyiyu的博客-CSDN博客_lab色差

【图像处理】颜色距离_Denny#的博客-CSDN博客_颜色距离公式 

色差计算(颜色之间的相似度计算)_程序猿也可以很哲学的博客-CSDN博客_色差计算

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值