-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathcolor_space.py
51 lines (40 loc) · 1.48 KB
/
color_space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy
import skimage.io
import skimage.color
def convert_color(I, name):
if len(I.shape) != 3:
I = skimage.color.gray2rgb(I)
converters = {'rgb' : lambda I: I,
'lab' : to_Lab,
'rgi' : to_rgI,
'hsv' : to_HSV,
'nrgb' : to_nRGB,
'hue' : to_Hue}
return converters[name](I)
def to_grey(I):
grey_img = (255 * skimage.color.rgb2grey(I)).astype(numpy.uint8)
return numpy.dstack([grey_img, grey_img, grey_img])
def to_Lab(I):
lab = skimage.color.rgb2lab(I)
l = 255 * lab[:, :, 0] / 100 # L component ranges from 0 to 100
a = 127 + lab[:, :, 1] # a component ranges from -127 to 127
b = 127 + lab[:, :, 2] # b component ranges from -127 to 127
return numpy.dstack([l, a, b]).astype(numpy.uint8)
def to_rgI(I):
rgi = I.copy()
rgi[:, :, 2] = to_grey(I)[:, :, 0]
return rgi
def to_HSV(I):
return (255 * skimage.color.rgb2hsv(I)).astype(numpy.uint8)
def to_nRGB(I):
_I = I / 255.0
norm_I = numpy.sqrt(_I[:, :, 0] ** 2 + _I[:, :, 1] ** 2 + _I[:, :, 2] ** 2)
norm_r = (_I[:, :, 0] / norm_I * 255).astype(numpy.uint8)
norm_g = (_I[:, :, 1] / norm_I * 255).astype(numpy.uint8)
norm_b = (_I[:, :, 2] / norm_I * 255).astype(numpy.uint8)
return numpy.dstack([norm_r, norm_g, norm_b])
def to_Hue(I):
I_h = to_HSV(I)[:, :, 0]
return numpy.dstack([I_h, I_h, I_h])