-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathshadow_removal.py
executable file
·82 lines (69 loc) · 2.39 KB
/
shadow_removal.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
import cv2
import numpy as np
def hsvPassShadowRemoval(src, shadowThreshold):
blurLevel = 3
height, width = src.shape[:2]
imgHSV = cv2.cvtColor(src, cv2.COLOR_RGB2HSV)
gaussianBlur = cv2.GaussianBlur(imgHSV, (blurLevel, blurLevel), 0)
hueImg, satImg, valImg = cv2.split(gaussianBlur)
NSVDI = np.zeros((height, width, 1), np.uint8)
count = height * width
with np.errstate(divide='ignore'):
# for i in range(0, height):
# for j in range(0, width):
# sat = int(satImg[i, j])
# val = int(valImg[i, j])
# NSVDI[i, j] = (satImg[i, j] - valImg[i, j]) / ((satImg[i, j] + valImg[i, j]) * 1.0)
NSVDI = (satImg + valImg) / ((satImg - valImg) * 1)
thresh = np.sum(NSVDI)
avg = thresh / (count * 1.0)
# for i in range(0, height):
# for j in range(0, width):
# if NSVDI[i, j] >= 0.25:
# hueImg[i, j] = 255
# satImg[i, j] = 255
# valImg[i, j] = 255
# else:
# hueImg[i, j] = 0
# satImg[i, j] = 0
# valImg[i, j] = 0
if shadowThreshold is None:
avg = avg
else:
avg = shadowThreshold
np.where(NSVDI > avg, 255, 0)
_, threshold = cv2.threshold(NSVDI, avg, 255, cv2.THRESH_BINARY_INV)
output = threshold
return output
def yuvPassShadowRemoval(src, shadowThreshold):
height, width = src.shape[:2]
imgYUV = cv2.cvtColor(src, cv2.COLOR_RGB2YUV)
yImg, uImg, vImg = cv2.split(imgYUV)
# for i in range(0, height):
# for j in range(0, width):
# yImg[i, j] = 0
yImg = np.zeros((height, width, 1), np.uint8)
imgYUV = cv2.merge([yImg, uImg, vImg])
rgbImg = cv2.cvtColor(imgYUV, cv2.COLOR_YUV2RGB)
rImg, gImg, bImg = cv2.split(rgbImg)
count = width * height
avg = np.sum(bImg)
avg /= count * 1.0
# for i in range(0, height):
# for j in range(0, width):
# if bImg[i, j] > ave:
# rImg[i, j] = 255
# gImg[i, j] = 255
# bImg[i, j] = 255
# else:
# rImg[i, j] = 0
# gImg[i, j] = 0
# bImg[i, j] = 0
if shadowThreshold is None:
avg = avg
else:
avg = shadowThreshold
np.where(bImg > avg, 255, 0)
_, threshold = cv2.threshold(bImg, avg, 255, cv2.THRESH_BINARY)
output = threshold
return output