-
Notifications
You must be signed in to change notification settings - Fork 0
/
intensity_transformation.py
53 lines (40 loc) · 1.58 KB
/
intensity_transformation.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
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
def plot(subplot, image, title):
plt.subplot(subplot)
plt.imshow(image, cmap='gray')
plt.title(title)
plt.axis(False)
def gamma_transform(grayscale_image, c, gamma):
return rescale_image(c * (grayscale_image ** gamma))
def rescale_image(grayscale_image):
max_val = np.max(grayscale_image)
res = (grayscale_image / max_val) * 255
return res
def gamma_mapping(n, c, gamma):
return c * (n ** gamma)
def rescale_gamma_mapping(cur_val, max_mapped_val, start, end):
return start + (cur_val / max_mapped_val) * (end - start)
def range_gamma_transform(grayscale_image, start, end, c, gamma):
mask = (grayscale_image >= start) & (grayscale_image <= end)
max_val = np.max(grayscale_image[mask])
max_mapped_val = gamma_mapping(max_val, c, gamma)
return np.where(
mask,
rescale_gamma_mapping(
gamma_mapping(grayscale_image, c, gamma),
max_mapped_val, start, end
),
grayscale_image
)
image = cv.imread('images/Buildings.jpg', cv.IMREAD_GRAYSCALE)
gt_image = gamma_transform(image, 1, 0.3)
rgt_image = range_gamma_transform(image, 0, 100, 1, 0.2)
composed_rgt_image = range_gamma_transform(rgt_image, 120, 255, 1, 5)
cv.imwrite('images/OutputBuildings.jpg', composed_rgt_image)
plot(221, image, 'Orginal Image')
plot(222, gt_image, 'Gamma Transformation with y = 0.3')
plot(223, rgt_image, 'Intensity Range [0, 100] Transformed with y = 0.2')
plot(224, composed_rgt_image, '[0, 100] with y = 0.2 and [120, 255] with y = 5')
plt.show()