python圖片由RGB空間轉(zhuǎn)成LAB空間的實(shí)現(xiàn)方式
python圖片由RGB空間轉(zhuǎn)成LAB空間
RGB轉(zhuǎn)Lab顏色空間
RGB顏色空間不能直接轉(zhuǎn)換為L(zhǎng)ab顏色空間,需要借助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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python流程控制 while循環(huán)實(shí)現(xiàn)解析
這篇文章主要介紹了Python流程控制 while循環(huán)實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
Python和Matlab實(shí)現(xiàn)蝙蝠算法的示例代碼
蝙蝠算法是一種搜索全局最優(yōu)解的有效方法,本文主要介紹了Python和Matlab實(shí)現(xiàn)蝙蝠算法的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
python實(shí)現(xiàn)根據(jù)給定坐標(biāo)點(diǎn)生成多邊形mask的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)根據(jù)給定坐標(biāo)點(diǎn)生成多邊形mask的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python輸入輸出從鍵盤(pán)到文件實(shí)戰(zhàn)全面指南
這篇文章主要為大家介紹了Python輸入輸出從鍵盤(pán)到文件實(shí)戰(zhàn)全面指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
python科學(xué)計(jì)算之narray對(duì)象用法
今天小編就為大家分享一篇python科學(xué)計(jì)算之narray對(duì)象用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

