-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconverter.py
48 lines (36 loc) · 1.59 KB
/
converter.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
import cv2
import numpy as np
import math
def RGB_TO_HSI(img):
with np.errstate(divide='ignore', invalid='ignore'):
#Load image with 32 bit floats as variable type
bgr = np.float32(img)/255
#Separate color channels
blue = bgr[:,:,0]
green = bgr[:,:,1]
red = bgr[:,:,2]
#Calculate Intensity
def calc_intensity(red, blue, green):
return np.divide(blue + green + red, 3)
#Calculate Saturation
def calc_saturation(red, blue, green):
minimum = np.minimum(np.minimum(red, green), blue)
saturation = 1 - (3 / (red + green + blue + 0.001) * minimum)
return saturation
#Calculate Hue
def calc_hue(red, blue, green):
hue = np.copy(red)
for i in range(0, blue.shape[0]):
for j in range(0, blue.shape[1]):
hue[i][j] = 0.5 * ((red[i][j] - green[i][j]) + (red[i][j] - blue[i][j])) / \
math.sqrt((red[i][j] - green[i][j])**2 +
((red[i][j] - blue[i][j]) * (green[i][j] - blue[i][j])))
hue[i][j] = math.acos(hue[i][j])
if blue[i][j] <= green[i][j]:
hue[i][j] = hue[i][j]
else:
hue[i][j] = ((360 * math.pi) / 180.0) - hue[i][j]
return hue
#Merge channels into picture and return image
hsi = cv2.merge((calc_hue(red, blue, green), calc_saturation(red, blue, green), calc_intensity(red, blue, green)))
return hsi