最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python圖片由RGB空間轉(zhuǎn)成LAB空間的實(shí)現(xiàn)方式

 更新時間:2023年10月12日 14:56:11   作者:Fly~~  
這篇文章主要介紹了python圖片由RGB空間轉(zhuǎn)成LAB空間的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

python圖片由RGB空間轉(zhuǎn)成LAB空間

RGB轉(zhuǎn)Lab顏色空間

RGB顏色空間不能直接轉(zhuǎn)換為Lab顏色空間,需要借助XYZ顏色空間,把RGB顏色空間轉(zhuǎn)換到XYZ顏色空間,之后再把XYZ顏色空間轉(zhuǎn)換到Lab顏色空間。

RGB與XYZ顏色空間有如下關(guān)系:

其中m如下:

XYZ到LAB的轉(zhuǎn)換公式如下

X_(1,) Y_(1,) Z_(1,)分別是X,Y,X線性歸一化之后的值,其中f(x)如下:

python實(shí)現(xiàn)

import numpy as np
import cv2
# region 輔助函數(shù)
# RGB2XYZ空間的系數(shù)矩陣
M = np.array([[0.412453, 0.357580, 0.180423],
              [0.212671, 0.715160, 0.072169],
              [0.019334, 0.119193, 0.950227]])
# im_channel取值范圍:[0,1]
def f(im_channel):
    return np.power(im_channel, 1 / 3) if im_channel > 0.008856 else 7.787 * im_channel + 0.137931
def anti_f(im_channel):
    return np.power(im_channel, 3) if im_channel > 0.206893 else (im_channel - 0.137931) / 7.787
# endregion
# region RGB 轉(zhuǎn) Lab
# 像素值RGB轉(zhuǎn)XYZ空間,pixel格式:(B,G,R)
# 返回XYZ空間下的值
def __rgb2xyz__(pixel):
    b, g, r = pixel[0], pixel[1], pixel[2]
    rgb = np.array([r, g, b])
    # rgb = rgb / 255.0
    # RGB = np.array([gamma(c) for c in rgb])
    XYZ = np.dot(M, rgb.T)
    XYZ = XYZ / 255.0
    return (XYZ[0] / 0.95047, XYZ[1] / 1.0, XYZ[2] / 1.08883)
def __xyz2lab__(xyz):
    """
    XYZ空間轉(zhuǎn)Lab空間
    :param xyz: 像素xyz空間下的值
    :return: 返回Lab空間下的值
    """
    F_XYZ = [f(x) for x in xyz]
    L = 116 * F_XYZ[1] - 16 if xyz[1] > 0.008856 else 903.3 * xyz[1]
    a = 500 * (F_XYZ[0] - F_XYZ[1])
    b = 200 * (F_XYZ[1] - F_XYZ[2])
    return (L, a, b)
def RGB2Lab(pixel):
    """
    RGB空間轉(zhuǎn)Lab空間
    :param pixel: RGB空間像素值,格式:[G,B,R]
    :return: 返回Lab空間下的值
    """
    xyz = __rgb2xyz__(pixel)
    Lab = __xyz2lab__(xyz)
    return Lab
if __name__ == '__main__':
    img = cv2.imread(r'2020_94470.jpg')
    w = img.shape[0]
    h = img.shape[1]
    img_new = np.zeros((w, h, 3))
    lab = np.zeros((w, h, 3))
    for i in range(w):
        for j in range(h):
            Lab = RGB2Lab(img[i, j])
            lab[i, j] = (Lab[0], Lab[1], Lab[2])
        cv2.imwrite(r'00000122_1.jpg', lab)

比較顏色相似度,RGB空間轉(zhuǎn)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])

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python Excel處理庫openpyxl使用詳解

    Python Excel處理庫openpyxl使用詳解

    openpyxl是一個第三方庫,可以處理xlsx格式的Excel文件。這篇文章主要介紹了Python Excel處理庫openpyxl使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 使用Python實(shí)現(xiàn)在PDF中添加空白頁面的方法

    使用Python實(shí)現(xiàn)在PDF中添加空白頁面的方法

    在日常辦公和數(shù)據(jù)處理中,PDF文件因其格式穩(wěn)定性被廣泛使用,本文將介紹Python如何使用Spire.PDF for Python實(shí)現(xiàn)為PDF添加空白頁面,感興趣的小伙伴可以了解下
    2025-11-11
  • python使用webdriver爬取微信公眾號

    python使用webdriver爬取微信公眾號

    這篇文章主要為大家詳細(xì)介紹了python使用webdriver爬取微信公眾號信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字

    python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字

    這篇文章主要介紹了python如何實(shí)現(xiàn)圖片轉(zhuǎn)文字問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 使用Keras畫神經(jīng)網(wǎng)絡(luò)準(zhǔn)確性圖教程

    使用Keras畫神經(jīng)網(wǎng)絡(luò)準(zhǔn)確性圖教程

    這篇文章主要介紹了使用Keras畫神經(jīng)網(wǎng)絡(luò)準(zhǔn)確性圖教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • pyqt5 實(shí)現(xiàn) 下拉菜單 + 打開文件的示例代碼

    pyqt5 實(shí)現(xiàn) 下拉菜單 + 打開文件的示例代碼

    今天小編就為大家分享一篇pyqt5 實(shí)現(xiàn) 下拉菜單 + 打開文件的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • np.unique()的具體使用

    np.unique()的具體使用

    本文主要介紹了np.unique()的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 分享13個好用到起飛的Python技巧

    分享13個好用到起飛的Python技巧

    編程是有技巧的,能寫的出程序的人很多,但能寫的又快又好是有技巧的,下面這篇文章主要給大家介紹了13個好用到起飛的Python技巧,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • Python實(shí)現(xiàn)歷史記錄功能(實(shí)際案例)

    Python實(shí)現(xiàn)歷史記錄功能(實(shí)際案例)

    很多應(yīng)用程序都有瀏覽用戶的歷史記錄的功能,瀏覽器可以查看最近訪問過的網(wǎng)頁,現(xiàn)在我們制作了一個簡單的猜數(shù)字的小游戲,添加歷史記錄功能,顯示用戶最近猜過的數(shù)字,如何實(shí)現(xiàn)呢?跟隨小編一起看看吧
    2022-04-04
  • Python讀寫JSON文件的操作詳解

    Python讀寫JSON文件的操作詳解

    JSON數(shù)據(jù)類型最常用的應(yīng)用場景就是API或?qū)?shù)據(jù)保存到 .json穩(wěn)當(dāng)數(shù)據(jù)中。使用Python處理這些數(shù)據(jù)會變得非常簡單,本文將詳細(xì)講解Python如何讀寫JSON文件的,需要的可以參考一下
    2022-04-04

最新評論

阜阳市| 南华县| 巴彦淖尔市| 申扎县| 西平县| 梁平县| 嘉义县| 富阳市| 汝阳县| 甘德县| 贡嘎县| 特克斯县| 北宁市| 鄂尔多斯市| 曲靖市| 田阳县| 德庆县| 汤原县| 万全县| 罗江县| 东港市| 纳雍县| 桦川县| 马龙县| 沭阳县| 裕民县| 宁明县| 时尚| 电白县| 阳泉市| 堆龙德庆县| 革吉县| 治县。| 皮山县| 铜陵市| 丁青县| 苍南县| 盐边县| 区。| 晋中市| 平舆县|