-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_stretch.py
57 lines (48 loc) · 2 KB
/
lab_stretch.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
51
52
53
54
55
56
57
import cv2
from skimage.color import rgb2hsv,hsv2rgb
import numpy as np
from skimage.color import rgb2lab, lab2rgb
import math
e = math.e
def global_stretching(img_L,height, width):
length = height * width
R_rray = (np.copy(img_L)).flatten()
R_rray.sort()
I_min = int(R_rray[int(length / 100)])
I_max = int(R_rray[-int(length / 100)])
array_Global_histogram_stretching_L = np.zeros((height, width))
for i in range(0, height):
for j in range(0, width):
if img_L[i][j] < I_min:
p_out = img_L[i][j]
array_Global_histogram_stretching_L[i][j] = 0
elif (img_L[i][j] > I_max):
p_out = img_L[i][j]
array_Global_histogram_stretching_L[i][j] = 100
else:
p_out = int((img_L[i][j] - I_min) * ((100) / (I_max - I_min)))
array_Global_histogram_stretching_L[i][j] = p_out
return (array_Global_histogram_stretching_L)
def global_Stretching_ab(a,height, width):
array_Global_histogram_stretching_L = np.zeros((height, width), 'float64')
for i in range(0, height):
for j in range(0, width):
p_out = a[i][j] * (1.3 ** (1 - math.fabs(a[i][j] / 128)))
array_Global_histogram_stretching_L[i][j] = p_out
return (array_Global_histogram_stretching_L)
def lab_stretching(sceneRadiance):
sceneRadiance = np.clip(sceneRadiance, 0, 255)
sceneRadiance = np.uint8(sceneRadiance)
height = len(sceneRadiance)
width = len(sceneRadiance[0])
img_lab = rgb2lab(sceneRadiance)
L, a, b = cv2.split(img_lab)
img_L_stretching = global_stretching(L, height, width)
img_a_stretching = global_Stretching_ab(a, height, width)
img_b_stretching = global_Stretching_ab(b, height, width)
labArray = np.zeros((height, width, 3), 'float64')
labArray[:, :, 0] = img_L_stretching
labArray[:, :, 1] = img_a_stretching
labArray[:, :, 2] = img_b_stretching
img_rgb = lab2rgb(labArray) * 255
return img_rgb