-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
53 lines (46 loc) · 1.37 KB
/
test.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 cv2
from skimage import io, color
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
filename1='test.png'
"""
rgb = Image.open(filename1)
rgb = np.asarray(rgb)
plt.figure()
plt.subplot(1,2,1)
plt.title('Test Image')
plt.imshow(rgb)"""
img=cv2.imread('test1.png')
rgb=cv2. cvtColor(img, cv2. COLOR_BGR2RGB)
image_B = np.copy(rgb[:, :, 0])
image_G = np.copy(rgb[:, :, 1])
image_R = np.copy(rgb[:, :, 2])
s=np.shape(rgb)
plt.subplot(1,2,1)
plt.title('Test Image')
plt.imshow(rgb)
#Converting RGB to LAB color space
lab = color.rgb2lab(rgb)
image_b = np.copy(lab[:, :, 0])
image_a = np.copy(lab[:, :, 1])
image_l = np.copy(lab[:, :, 2])
lm=np.mean(lab[:,:,0], axis=(0, 1))
am=np.mean(lab[:,:,1], axis=(0, 1))
bm=np.mean(lab[:,:,2], axis=(0, 1))
#Creating empty mask for masking shadow
mas = np.empty([rgb.shape[0], rgb.shape[1]], dtype = bool)
lb=lab[:,:,0]+lab[:,:,2]
#Hand crafted thresholds: Dataset specific
if (am+bm)<=15:
mas[(image_l <=(lm-(np.std(image_l))/15))] = False
else:
mas[(image_l+image_b)<=50] = False
B_masked = np.ma.masked_array(image_b, mask = mas)
G_masked = np.ma.masked_array(image_G, mask = mas)
R_masked = np.ma.masked_array(image_R, mask = mas)
mam = np.dstack([rgb, (~mas).astype(np.uint8)*255])
plt.subplot(1,2,2)
plt.imshow(mam)
plt.title('Shadow detected Image')
plt.show()