好吧,我正在进行图像处理,以识别图像的颜色变化并能够将数据绘制在直方图中。为此,我使用RGB颜色空间中的皮肤斑点图像。下面的代码可以获取每个像素的颜色,并使用color.rgb2lab转换为HSV。但是由于要转换为L * a * b *,因为它更接近人类的视觉,因此在python库中没有转换为L * a * b *。这样,通过RGB分离的像素,如何将这些像素转换为LAB颜色?
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.pyplot as plt
import colorsys
from PIL import Image
# (1) Import the file to be analyzed!
img_file = Image.open("IMD006.png")
img = img_file.load()
# (2) Get image width & height in pixels
[xs, ys] = img_file.size
max_intensity = 100
hues = {}
# (3) Examine each pixel in the image file
for x in xrange(0, xs):
for y in xrange(0, ys):
# (4) Get the RGB color of the pixel
[r, g, b] = img[x, y]
# (5) Normalize pixel color values
r /= 255.0
g /= 255.0
b /= 255.0
# (6) Convert RGB color to HSV
[h, s, v] = colorsys.rgb_to_hsv(r, g, b)
# (7) Marginalize s; count how many pixels have matching (h, v)
if h not in hues:
hues[h] = {}
if v not in hues[h]:
hues[h][v] = 1
else:
if hues[h][v] < max_intensity:
hues[h][v] += 1
答案 0 :(得分:3)
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
def rgb_to_cielab(a):
"""
a is a pixel with RGB coloring
"""
a1,a2,a3 = a/255
color1_rgb = sRGBColor(a1, a2, a3);
color1_lab = convert_color(color1_rgb, LabColor);
return color1_lab
rgb_to_cielab(np.array([255,0,255]))
输出:LabColor(lab_l = 60.32364943499053,lab_a = 98.23532017664644,lab_b = -60.83501679458592)
答案 1 :(得分:2)
您可以使用内置的色彩管理系统通过PIL / Pillow进行操作,并构建如下所示的变换:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image, ImageCms
# Open image and discard alpha channel which makes wheel round rather than square
im = Image.open('colorwheel.png').convert('RGB')
# Convert to Lab colourspace
srgb_p = ImageCms.createProfile("sRGB")
lab_p = ImageCms.createProfile("LAB")
rgb2lab = ImageCms.buildTransformFromOpenProfiles(srgb_p, lab_p, "RGB", "LAB")
Lab = ImageCms.applyTransform(im, rgb2lab)
Lab
现在是您在Lab色空间中的图像。如果继续进行以下操作,并将以下几行添加到上述代码的末尾,则可以将Lab图像拆分为其组成通道,并将它们分别保存为灰度图像以进行检查。
# Split into constituent channels so we can save 3 separate greyscales
L, a, b = Lab.split()
L.save('L.png')
a.save('a.png')
b.save('b.png')
因此,如果您从这张图片开始:
您将以L
频道的身份获得此信息:
此为a
频道:
这是b
频道:
暂时不科学,a
通道在图像为绿色时应为负/低,在图像为品红色时应为高/正。 b
通道在图像为蓝色的情况下应为负/低,在图像为黄色的情况下应为高/正,对我来说看起来不错!关于L
通道,从RGB到灰度的公式是(不在我的脑海):
L = 0.2*R + 0.7*G + 0.1*B
因此,您希望L
通道在图像为绿色时会更亮,而在图像为蓝色时会最暗。
或者,您也可以使用scikit-image模块执行此操作,甚至可能更简单:
import numpy as np
from skimage import color, io
# Open image and make Numpy arrays 'rgb' and 'Lab'
rgb = io.imread('image.png')
Lab = color.rgb2lab(rgb)
我不确定缩放比例是100%,但我怀疑L
通道的浮动范围是0..100,并且a
和b
也是浮动范围范围-128 .. + 128,尽管我可能错了!
在上面的色轮图像中,我为每个通道获得了以下最小值/最大值:
Lab[:,:,0].min() # L min
32.29567256501352
Lab[:,:,0].max() # L max
97.13950703971322
Lab[:,:,1].min() # a min
-86.18302974439501
Lab[:,:,1].max() # a max
98.23305386311316
Lab[:,:,2].min() # b min
-107.85730020669489
Lab[:,:,2].max() # b max
94.47812227647823
答案 2 :(得分:1)
我已经像你一样看到这个问题 3 个月了,这是我的解决方案
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.pyplot as plt
import colorsys
from PIL import Image
from past.builtins import xrange
img_file = Image.open("F:/coding/Project/FDD/neo5.png")
img = img_file.load()
[xs, ys] = img_file.size
max_intensity = 100
hues = {}
for x in xrange(0, xs):
for y in xrange(0, ys):
[r, g, b] = img[x, y]
r /= 255.0
g /= 255.0
b /= 255.0
[h, s, v] = colorsys.rgb_to_hsv(r, g, b)
if h not in hues:
hues[h] = {}
if v not in hues[h]:
hues[h][v] = 1
else:
if hues[h][v] < max_intensity:
hues[h][v] += 1
h_ = []
v_ = []
i = []
colours = []
for h in hues:
for v in hues[h]:
h_.append(h)
v_.append(v)
i.append(hues[h][v])
[r, g, b] = colorsys.hsv_to_rgb(h, 1, v)
colours.append([r, g, b])
fig = plt.figure()
ax = p3.Axes3D(fig)
ax.scatter(h_, v_, i, s=5, c=colours, lw=0)
ax.set_xlabel('Hue')
ax.set_ylabel('Value')
ax.set_zlabel('Intensity')
fig.add_axes(ax)
plt.show()
答案 3 :(得分:0)
使用cv2,您可以轻松实现此转换。 RGB-> LAB,LAB-> RGB。
import numpy as np
import cv2
img = cv2.imread('1.jpg')
LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
cv2.imwrite('L.png', LAB[:,:,0])
cv2.imwrite('a.png', LAB[:,:,1])
cv2.imwrite('b.png', LAB[:,:,2])
BGR = cv2.cvtColor(LAB, cv2.COLOR_LAB2BGR)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite('new.png', BGR)