forked from GoAMATEUR/EE356-Project1-Threshold-selection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaryGradTres.py
29 lines (20 loc) · 1.16 KB
/
varyGradTres.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
from util import *
if __name__ == '__main__':
datapath = 'data'
savepath = 'gradT_out'
histpath = 'hist'
boundarypath = 'gradT_boundary'
filename = "1_gray.bmp"
imagepath = os.path.join(datapath, filename)
image = cv2.imread(imagepath, 0)
for t in range(10, 200, 10):
thresholds, threshold, boundary = getThreshold(image, t)
print('threshold for {}: {}, t: {}'.format(filename, threshold, t))
cv2.imwrite(os.path.join(boundarypath, filename.split('.')[0] + '_{}'.format(t) + '.bmp'), boundary)
# plot the histogram
# n, bins, patches = plt.hist(thresholds, bins=256, facecolor='blue', alpha=0.75)
# plt.savefig(os.path.join(histpath, filename.split('.')[0] + '_{}_{}'.format(t, int(threshold)) + '.png'))
# plt.cla()
# segment the image with the threshold, which is the mean of all the gray value as threshold.
_, img_treated = cv2.threshold(image, int(threshold), 255, cv2.THRESH_BINARY)
cv2.imwrite(os.path.join(savepath, filename.split('.')[0] + '_{}_{}'.format(t, int(threshold)) +'.bmp'), img_treated)