@@ -57,10 +57,34 @@ def canny_watershed(inputfile, outputfile, sigma, min_edge, ratio):
57
57
58
58
# Apply watershed algorithm
59
59
cv .watershed (image , markers )
60
+
60
61
62
+ '''
61
63
mark = markers.astype('uint8')
62
64
mark = cv.bitwise_not(mark)
63
65
cv.imshow('marker v2', mark)
66
+ '''
67
+
68
+ # Apply thresholding on the image to convert to binary image
69
+ m = cv .convertScaleAbs (markers )
70
+ ret , thresh = cv .threshold (m , 0 , 255 , cv .THRESH_BINARY + cv .THRESH_OTSU )
71
+ cv .imshow ('thresh' , thresh )
72
+
73
+ # Invert the thresh
74
+ thresh_inv = cv .bitwise_not (thresh )
75
+ cv .imshow ('thresh_inv' , thresh_inv )
76
+
77
+ # Bitwise and with the image mask thresh
78
+ res = cv .bitwise_and (image , image , mask = thresh )
79
+ cv .imshow ('res' , res )
80
+
81
+ # Bitwise and the image with mask as threshold invert
82
+ res3 = cv .bitwise_and (image , image , mask = thresh_inv )
83
+ cv .imshow ('res3' , res3 )
84
+ # Take the weighted average
85
+ res4 = cv .addWeighted (res , 1 , res3 , 1 , 0 )
86
+ cv .imshow ('marker v2' , res4 )
87
+
64
88
65
89
# Generate random color
66
90
colors = []
@@ -90,14 +114,19 @@ def canny_watershed(inputfile, outputfile, sigma, min_edge, ratio):
90
114
# Apply thresholding on the image to convert to binary image
91
115
m = cv.convertScaleAbs(marker32)
92
116
ret, thresh = cv.threshold(m, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
117
+ cv.imshow('thresh', thresh)
93
118
# Invert the thresh
94
119
thresh_inv = cv.bitwise_not(thresh)
120
+ cv.imshow('thresh_inv', thresh_inv)
95
121
# Bitwise and with the image mask thresh
96
122
res = cv.bitwise_and(image, image, mask = thresh)
123
+ cv.imshow('res', res)
97
124
# Bitwise and the image with mask as threshold invert
98
125
res3 = cv.bitwise_and(image, image, mask = thresh_inv)
126
+ cv.imshow('res3', res3)
99
127
# Take the weighted average
100
128
res4 = cv.addWeighted(res, 1, res3, 1, 0)
129
+ cv.imshow('res4', res4)
101
130
# Draw the contours on the image with green color and pixel width is 1
102
131
final = cv.drawContours(res4, contours, -1, (0, 255, 0), 1)
103
132
@@ -114,6 +143,8 @@ def canny_watershed(inputfile, outputfile, sigma, min_edge, ratio):
114
143
print ("Hello world" )
115
144
#canny_watershed(1, 1, 1, 1)
116
145
#canny_watershed('四破魚(藍圓鰺)2.jpg', 0, 100, 3)
146
+ #canny_watershed('8ubS9.jpg', 'output.jpg', 0, 100, 3)
147
+
117
148
with open ('file_lists.txt' , 'r' ) as f :
118
149
for line in f :
119
150
params = []
@@ -124,4 +155,5 @@ def canny_watershed(inputfile, outputfile, sigma, min_edge, ratio):
124
155
canny_watershed (params [0 ], outputfile , float (params [1 ]), int (params [2 ]), int (params [3 ]))
125
156
#filename = os.path.splitext(params[0])[0]
126
157
#print(filename)
158
+
127
159
print ("end" )
0 commit comments