Harsh Wardhan
Harsh Wardhan

Reputation: 2148

Convert RGB triplets to LAB triplets using skimage.color.rgb2lab()

I can convert an image from RGB color space to LAB color space using skimage.color.rgb2lab(). But when I try converting a single RGB triplet to LAB triplet

rgb_color = [0.1,0.2,0.3]
lab_color = color.rgb2lab(rgb_color)

I get the following error:

ValueError: the input array must be have a shape == (.., ..,[ ..,] 3)), got (3)

What is the correct way to do it? I'm using Python 2.7.

Upvotes: 8

Views: 12304

Answers (1)

dbort
dbort

Reputation: 1004

rgb2lab() expects a 3D (or 4D) image; you're passing it a 1D list of numbers.

Try giving it a one-pixel image:

>>> from skimage import color
>>> rgb_color = [[[0.1,0.2,0.3]]]  # Note the three pairs of brackets
>>> lab_color = color.rgb2lab(rgb_color)
>>> lab_color
array([[[ 20.47616557,  -0.65320961, -18.63011548]]])

Upvotes: 12

Related Questions