-
Notifications
You must be signed in to change notification settings - Fork 10
/
Shadow_Detection.py
134 lines (89 loc) · 5.31 KB
/
Shadow_Detection.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import cv2
import gc
import numpy as np
import pandas as pd
import rasterio
from skimage.color import lab2lch, rgb2lab
from skimage.exposure import rescale_intensity
from skimage.morphology import disk
from sklearn.cluster import KMeans
def shadow_detection(image_file, shadow_mask_file, convolve_window_size = 5, num_thresholds = 3, struc_elem_size = 5):
"""
This function is used to detect shadow - covered areas in an image, as proposed in the paper
'Near Real - Time Shadow Detection and Removal in Aerial Motion Imagery Application' by Silva G.F., Carneiro G.B.,
Doth R., Amaral L.A., de Azevedo D.F.G. (2017)
Inputs:
- image_file: Path of image to be processed for shadow removal. It is assumed that the first 3 channels are ordered as
Red, Green and Blue respectively
- shadow_mask_file: Path of shadow mask to be saved
- convolve_window_size: Size of convolutional matrix filter to be used for blurring of specthem ratio image
- num_thresholds: Number of thresholds to be used for automatic multilevel global threshold determination
- struc_elem_size: Size of disk - shaped structuring element to be used for morphological closing operation
Outputs:
- shadow_mask: Shadow mask for input image
"""
if (convolve_window_size % 2 == 0):
raise ValueError('Please make sure that convolve_window_size is an odd integer')
buffer = int((convolve_window_size - 1) / 2)
with rasterio.open(image_file) as f:
metadata = f.profile
img = rescale_intensity(np.transpose(f.read(tuple(np.arange(metadata['count']) + 1)), [1, 2, 0]), out_range = 'uint8')
img = img[:, :, 0 : 3]
lch_img = np.float32(lab2lch(rgb2lab(img)))
l_norm = rescale_intensity(lch_img[:, :, 0], out_range = (0, 1))
h_norm = rescale_intensity(lch_img[:, :, 2], out_range = (0, 1))
sr_img = (h_norm + 1) / (l_norm + 1)
log_sr_img = np.log(sr_img + 1)
del l_norm, h_norm, sr_img
gc.collect()
avg_kernel = np.ones((convolve_window_size, convolve_window_size)) / (convolve_window_size ** 2)
blurred_sr_img = cv2.filter2D(log_sr_img, ddepth = -1, kernel = avg_kernel)
del log_sr_img
gc.collect()
flattened_sr_img = blurred_sr_img.flatten().reshape((-1, 1))
labels = KMeans(n_clusters = num_thresholds + 1, max_iter = 10000).fit(flattened_sr_img).labels_
flattened_sr_img = flattened_sr_img.flatten()
df = pd.DataFrame({'sample_pixels': flattened_sr_img, 'cluster': labels})
threshold_value = df.groupby(['cluster']).min().max()[0]
df['Segmented'] = np.uint8(df['sample_pixels'] >= threshold_value)
del blurred_sr_img, flattened_sr_img, labels, threshold_value
gc.collect()
shadow_mask_initial = np.array(df['Segmented']).reshape((img.shape[0], img.shape[1]))
struc_elem = disk(struc_elem_size)
shadow_mask = np.expand_dims(np.uint8(cv2.morphologyEx(shadow_mask_initial, cv2.MORPH_CLOSE, struc_elem)), axis = 0)
del df, shadow_mask_initial, struc_elem
gc.collect()
metadata['count'] = 1
with rasterio.open(shadow_mask_file, 'w', **metadata) as dst:
dst.write(shadow_mask)
return shadow_mask
def shadow_correction(image_file, shadow_mask_file, corrected_image_file, exponent = 1):
"""
This function is used to adjust brightness for shadow - covered areas in an image, as proposed in the paper
'Near Real - Time Shadow Detection and Removal in Aerial Motion Imagery Application' by Silva G.F., Carneiro G.B.,
Doth R., Amaral L.A., de Azevedo D.F.G. (2017)
Inputs:
- image_file: Path of 3 - channel (red, green, blue) image to be processed for shadow removal
- shadow_mask_file: Path of shadow mask for corresponding input image
- corrected_image_file: Path of corrected image to be saved
- exponent: Exponent to be used for the calculcation of statistics for unshaded and shaded areas
Outputs:
- corrected_img: Corrected input image
"""
with rasterio.open(image_file) as f:
metadata = f.profile
img = rescale_intensity(np.transpose(f.read(tuple(np.arange(metadata['count']) + 1)), [1, 2, 0]), out_range = 'uint8')
with rasterio.open(shadow_mask_file) as s:
shadow_mask = s.read(1)
corrected_img = np.zeros((img.shape), dtype = np.uint8)
non_shadow_mask = np.uint8(shadow_mask == 0)
for i in range(img.shape[2]):
shadow_area_mask = shadow_mask * img[:, :, i]
non_shadow_area_mask = non_shadow_mask * img[:, :, i]
shadow_stats = np.float32(np.mean(((shadow_area_mask ** exponent) / np.sum(shadow_mask))) ** (1 / exponent))
non_shadow_stats = np.float32(np.mean(((non_shadow_area_mask ** exponent) / np.sum(non_shadow_mask))) ** (1 / exponent))
mul_ratio = ((non_shadow_stats - shadow_stats) / shadow_stats) + 1
corrected_img[:, :, i] = np.uint8(non_shadow_area_mask + np.clip(shadow_area_mask * mul_ratio, 0, 255))
with rasterio.open(corrected_image_file, 'w', **metadata) as dst:
dst.write(np.transpose(corrected_img, [2, 0, 1]))
return corrected_img