-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCV_8_Valley_emphasis.py
More file actions
212 lines (169 loc) · 7.05 KB
/
CV_8_Valley_emphasis.py
File metadata and controls
212 lines (169 loc) · 7.05 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import cv2
import numpy as np
import os
import tools
def otsu(input_image):
"""
follow the formula from https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
"""
blur = cv2.GaussianBlur(input_image,(5,5),0)
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([blur],[0],None,[256],[0,256])
hist_norm = hist.ravel()/hist.sum()
# cumulative distribution function (CDF)
Q = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
thresh = -1
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
q1, q2 = Q[i],Q[255]-Q[i] # cum sum of classes
# make sure it would not divide by zero
if q1 < 1.e-6 or q2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(p1*i1)/q1, np.sum(p2*i2)/q2
var1, var2 = np.sum(((i1-mu1)**2)*p1)/q1, np.sum(((i2-mu2)**2)*p2)/q2
# calculates the minimization function
fn_tmp = var1*q1 + var2*q2
if fn_tmp < fn_min:
fn_min = fn_tmp
thresh = i
# find otsu's threshold value with Opencv2 function
ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# print( "manual otsu: {}, opencv otsu: {}".format(thresh,ret) )
return thresh
def otsu_revised(input_image):
"""
follow the formula from https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
"""
blur = cv2.GaussianBlur(input_image,(5,5),0)
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([blur],[0],None,[256],[0,256])
hist_norm = hist.ravel()/hist.sum()
# Omega_total cumulative distribution function (CDF)
Q = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
thresh = -1
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
# cum sum of classes
q1, q2 = Q[i],Q[255]-Q[i]
# make sure it would not divide by zero
if q1 < 1.e-6 or q2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(p1*i1)/q1, np.sum(p2*i2)/q2
var1, var2 = np.sum(((i1-mu1)**2)*p1)/q1, np.sum(((i2-mu2)**2)*p2)/q2
# calculates the minimization function
fn_tmp = var1*q1 + var2*q2
if fn_tmp < fn_min:
fn_min = fn_tmp
thresh = i
# find otsu's threshold value with Opencv2 function
ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print( "manual otsu: {}, opencv otsu: {}".format(thresh,ret) )
return thresh
def v_otsu(input_image):
"""
follow the formula from https://docs.opencv.org/3.4/d7/d4d/tutorial_py_thresholding.html
"""
blur = cv2.GaussianBlur(input_image,(5,5),0)
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([blur],[0],None,[256],[0,256])
hist_norm = hist.ravel()/hist.sum()
# cumulative distribution function (CDF)
omega_total = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
thresh = -1
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
omega1, omega2 = omega_total[i], omega_total[255]- omega_total[i] # cum sum of classes
# make sure it would not divide by zero
if omega1 < 1.e-6 or omega2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(i1*p1)/omega1, np.sum(i2*p2)/omega2
var1, var2 = np.sum(((i1-mu1)**2)*p1)/omega1, np.sum(((i2-mu2)**2)*p2)/omega2
# calculates the minimization function
fn_tmp = var1*omega1 + var2*omega2
# fn_tmp = omega1 * var1^2 + omega2*var2^2
if fn_tmp < fn_min:
fn_min = fn_tmp
thresh = i
# find otsu's threshold value with Opencv2 function
ret, otsu = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# print( "manual otsu: {}, opencv otsu: {}".format(thresh,ret) )
return thresh
def valley_emphasis(input_image):
"""
follow the formula from the thsis:
Automatic thersholding for defect detection - Hui-Fuang Ng 2006
"""
blur = cv2.GaussianBlur(input_image,(5,5),0)
# find normalized_histogram, and its cumulative distribution function
hist = cv2.calcHist([blur],[0],None,[256],[0,256])
hist_norm = hist.ravel()/hist.sum()
# cumulative distribution function (CDF) for probabilities
omega_total = hist_norm.cumsum()
bins = np.arange(256)
fn_min = np.inf
valley = -1
for i in range(256):
# probabilities
p1, p2 = np.hsplit(hist_norm,[i])
omega1, omega2 = omega_total[i], omega_total[255]-omega_total[i] # cum sum of classes
# make sure it would not divide by zero
if omega1 < 1.e-6 or omega2 < 1.e-6:
continue
# weights
i1, i2 = np.hsplit(bins,[i])
# finding means and variances
mu1, mu2 = np.sum(i1*p1)/omega1, np.sum(i2*p2)/omega2
var1, var2 = np.sum(((i1-mu1)**2)*p1)/omega1, np.sum(((i2-mu2)**2)*p2)/omega2
# calculates the minimization function
# fn_tmp = (1-hist_norm[i])*(omega1*(mu1**2) + omega2*(mu2**2))
fn_tmp = (1-hist_norm[i])*(omega1*var1 + omega2*var2)
if fn_tmp < fn_min:
fn_min = fn_tmp
valley = i
# otsu
otsu_value = otsu(input_image)
# print("otsu: {}, valley: {}".format(otsu_value, valley))
return valley
for file_name in os.listdir("./Image/"):
name = "./Image/" + file_name
image = cv2.imread(name,0)
thresh_valley = valley_emphasis(image)
thresh_otsu = v_otsu(image)
print("\n================ Final Threshold value ================")
print("\nOtus: {}, valley: {}".format(thresh_otsu, thresh_valley))
otsu_bin, otsu_colour = tools.threshold(image,0,thresh_otsu)
valley_bin, valley_colour = tools.threshold(image,0,thresh_valley)
cv2.imshow("otsu_bin", otsu_bin)
cv2.imshow("valley_bin", valley_bin)
cv2.waitKey()
cv2.destroyAllWindows()
# for idx in range(18):
# name = "./Image/original_sample_" + str(idx) + ".jpg"
# image = cv2.imread(name,0)
# thresh_valley = valley_emphasis(image)
# thresh_otsu = v_otsu(image)
# print("\n================ Final Threshold value ================")
# print("\nOtus: {}, valley: {}".format(thresh_otsu, thresh_valley))
# otsu_bin, otsu_colour = tools.threshold(image,0,thresh_otsu)
# valley_bin, valley_colour = tools.threshold(image,0,thresh_valley)
# cv2.imshow("otsu_bin", otsu_bin)
# cv2.imshow("valley_bin", valley_bin)
# cv2.waitKey()