-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecularity.py
77 lines (60 loc) · 2.43 KB
/
specularity.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
# Author: Murat Kirtay, The BioRobotics Inst./SSSA/
# Date: 02/11/2016
# Description: Detect and remove specularity from endoscopic images
import cv2
import numpy as np
def derive_graym(impath):
''' The intensity value m is calculated as (r+g+b)/3, yet
grayscalse will do same operation!
opencv uses default formula Y = 0.299 R + 0.587 G + 0.114 B
'''
# return cv2.imread(impath, cv2.CV_LOAD_IMAGE_GRAYSCALE)
return cv2.imread(impath, cv2.IMREAD_GRAYSCALE)
def derive_m(img, rimg):
''' Derive m (intensity) based on paper formula '''
(rw, cl, ch) = img.shape
for r in range(rw):
for c in range(cl):
rimg[r,c] = int(np.sum(img[r,c])/3.0)
return rimg
def derive_saturation(img, rimg):
''' Derive staturation value for a pixel based on paper formula '''
s_img = np.array(rimg)
(r, c) = s_img.shape
for ri in range(r):
for ci in range(c):
#opencv ==> b,g,r order
s1 = img[ri,ci][0] + img[ri,ci][2]
s2 = 2 * img[ri,ci][1]
if s1 >= s2:
s_img[ri,ci] = 1.5*(img[ri,ci][2] - rimg[ri,ci])
else:
s_img[ri,ci] = 1.5*(rimg[ri,ci] - img[ri,ci][0])
return s_img
def check_pixel_specularity(mimg, simg):
''' Check whether a pixel is part of specular region or not'''
m_max = np.max(mimg) * 0.5
s_max = np.max(simg) * 0.33
(rw, cl) = simg.shape
spec_mask = np.zeros((rw,cl), dtype=np.uint8)
for r in range(rw):
for c in range(cl):
if mimg[r,c] >= m_max and simg[r,c] <= s_max:
spec_mask[r,c] = 255
return spec_mask
def enlarge_specularity(spec_mask):
''' Use sliding window technique to enlarge specularity
simply move window over the image if specular pixel detected
mark center pixel is specular
win_size = 3x3, step_size = 1
'''
win_size, step_size = (3,3), 1
enlarged_spec = np.array(spec_mask)
for r in range(0, spec_mask.shape[0], step_size):
for c in range(0, spec_mask.shape[1], step_size):
# yield the current window
win = spec_mask[r:r + win_size[1], c:c + win_size[0]]
if win.shape[0] == win_size[0] and win.shape[1] == win_size[1]:
if win[1,1] !=0:
enlarged_spec[r:r + win_size[1], c:c + win_size[0]] = 255 * np.ones((3,3), dtype=np.uint8)
return enlarged_spec