-
Notifications
You must be signed in to change notification settings - Fork 2
/
canny_watershed.py
345 lines (298 loc) · 12.8 KB
/
canny_watershed.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
import os
import random as rng
def mean_shift(inputfile, outputfile, sigma, min_edge, ratio):
rng.seed(12345)
# first, read the image
#image = cv.imread('coins.jpg')
#image = cv.imread('四破魚(藍圓鰺)2.jpg')
image = cv.imread(inputfile)
cv.imshow('Original image', image)
'''
part of mean shift
'''
# convert image from unsigned 8 bit to 32 bit float
#image_float = np.float32(image)
# define the criteria(type, max_iter, epsilon)
# cv2.TERM_CRITERIA_EPS - stop the algorithm iteration if specified accuracy, epsilon, is reached.
# cv2.TERM_CRITERIA_MAX_ITER - stop the algorithm after the specified number of iterations, max_iter.
# cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER - stop the iteration when any of the above condition is met.
# max_iter - An integer specifying maximum number of iterations.In this case it is 10
# epsilon - Required accuracy.In this case it is 1
#criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 10, 1)
#k = 50 # number of clusters
#apply K-means algorithm with random centers approach
#ret, label, centers = cv.kmeans(image_float, k, None, criteria, 50, cv.KMEANS_RANDOM_CENTERS)
# convert the image from 32 bit float to unsigned 8 bit
#center = np.uint8(centers)
# this will flatten the label
#res = center[label.flatten()]
# reshape the image
#res2 = res.reshape(image.shape)
#cv.imshow('K means', res2)
# apply meanshift algorithm on to image
meanshift = cv.pyrMeanShiftFiltering(image, sp = 8, sr = 16, maxLevel = 1, termcrit = (cv.TERM_CRITERIA_EPS+ cv.TERM_CRITERIA_MAX_ITER, 5, 1))
cv.imshow('mean shift', meanshift)
'''
part of misc
'''
# change image from BGR to grayscale
#gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
gray = cv.cvtColor(meanshift, cv.COLOR_BGR2GRAY)
# apply thresholding to convert the image to binary
ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# erode the image
foreground = cv.erode(thresh, None, iterations = 1)
# Dilate the image
backgroundTemp = cv.dilate(thresh, None, iterations = 1)
# Apply thresholding
ret, background = cv.threshold(backgroundTemp, 1, 128, 1)
# Add foreground and background
marker = cv.add(foreground, background)
'''
part of watershed
'''
# Finding the contors in the image using chain approximation
#new, contours, hierarchy = cv.findContours(canny, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
new, contours, hierarchy = cv.findContours(marker, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for watershed algorithm
#markers = np.zeros(canny.shape, dtype = np.int32)
markers = np.zeros(marker.shape, dtype = np.int32)
# Draw the foreground markers
for i in range(len(contours)):
cv.drawContours(markers, contours, i, (i + 1) , -1)
# Draw the background markers
cv.circle(markers, (5, 5), 3, (255, 255, 255), -1)
cv.imshow('markers', markers * 10000)
# Apply watershed algorithm
cv.watershed(image, markers)
'''
mark = markers.astype('uint8')
mark = cv.bitwise_not(mark)
cv.imshow('marker v2', mark)
'''
# Apply thresholding on the image to convert to binary image
m = cv.convertScaleAbs(markers)
ret, thresh = cv.threshold(m, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv.imshow('thresh', thresh)
# Invert the thresh
thresh_inv = cv.bitwise_not(thresh)
cv.imshow('thresh_inv', thresh_inv)
# Bitwise and with the image mask thresh
res = cv.bitwise_and(image, image, mask = thresh)
cv.imshow('res', res)
# Bitwise and the image with mask as threshold invert
res3 = cv.bitwise_and(image, image, mask = thresh_inv)
cv.imshow('res3', res3)
# Take the weighted average
res4 = cv.addWeighted(res, 1, res3, 1, 0)
cv.imshow('marker v2', res4)
# Generate random color
colors = []
for contour in contours:
colors.append((rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256)))
# Create the result image
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random color
for i in range(markers.shape[0]):
for j in range(markers.shape[1]):
index = markers[i,j]
if index > 0 and index <= len(contours):
dst[i,j,:] = colors[index-1]
#else:
# dst[i, j, :] = (0, 0, 0)
# Display the final result
cv.imshow('final result', dst)
'''
# converting the marker to float 32 bit
marker32 = np.int32(marker)
# Apply watershed algorithm
cv.watershed(image, marker32)
# Apply thresholding on the image to convert to binary image
m = cv.convertScaleAbs(marker32)
ret, thresh = cv.threshold(m, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv.imshow('thresh', thresh)
# Invert the thresh
thresh_inv = cv.bitwise_not(thresh)
cv.imshow('thresh_inv', thresh_inv)
# Bitwise and with the image mask thresh
res = cv.bitwise_and(image, image, mask = thresh)
cv.imshow('res', res)
# Bitwise and the image with mask as threshold invert
res3 = cv.bitwise_and(image, image, mask = thresh_inv)
cv.imshow('res3', res3)
# Take the weighted average
res4 = cv.addWeighted(res, 1, res3, 1, 0)
cv.imshow('res4', res4)
# Draw the contours on the image with green color and pixel width is 1
final = cv.drawContours(res4, contours, -1, (0, 255, 0), 1)
#for i in range(len(contours)):
# cv.drawContours(res4, contours, i, (i + 1), -1)
# Display the image
#cv.imshow("Watershed", final)
cv.imshow("Watershed", res4)
'''
'''
colors = []
for contour in contours:
colors.append((rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256)))
# Create the result image
dst = np.zeros((marker32.shape[0], marker32.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random color
for i in range(marker32.shape[0]):
for j in range(marker32.shape[1]):
index = marker32[i,j]
if index > 0 and index <= len(contours):
dst[i,j,:] = colors[index-1]
cv.imshow('final', dst)
'''
# Save the output image
#cv.imwrite(outputfile, final)
# Wait for key stroke
cv.waitKey()
def canny_watershed_distance_transform(inputfile, outputfile, sigma, min_edge, ratio):
# read image -> convert to grayscale -> apply Otus thresholding
img = cv.imread(inputfile)
'''
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
cv.imshow('otsu', thresh)
'''
'''
# noise removal (using Canny)
gaussian = cv.GaussianBlur(thresh, (5, 5), sigma, sigma)
canny = cv.Canny(gaussian, min_edge, min_edge * ratio, 3, L2gradient = True)
# sharpen the image
mask = canny != 0
imgResult = img * (mask[:,:,None].astype(img.dtype))
#sharp = np.uint8(img)
#imgResult = sharp - canny
# convert back to 8bits gray scale
#imgResult = np.clip(imgResult, 0, 255)
#imgResult = imgResult.astype('uint8')
cv.imshow('canny', canny)
cv.imshow('new sharpen image', imgResult)
'''
# Create a kernel that we will use to sharpen our image
# an approximation of second derivative, a quite strong kernel
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
# do the laplacian filtering as it is
# well, we need to convert everything in something more deeper then CV_8U
# because the kernel has some negative values,
# and we can expect in general to have a Laplacian image with negative values
# BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
# so the possible negative number will be truncated
imgLaplacian = cv.filter2D(img, cv.CV_32F, kernel)
sharp = np.float32(img)
imgResult = sharp - imgLaplacian
# convert back to 8bits gray scale
imgResult = np.clip(imgResult, 0, 255)
imgResult = imgResult.astype('uint8')
imgLaplacian = np.clip(imgLaplacian, 0, 255)
imgLaplacian = np.uint8(imgLaplacian)
#cv.imshow('Laplace Filtered Image', imgLaplacian)
cv.imshow('New Sharped Image', imgResult)
# Create binary image from source image
bw = cv.cvtColor(imgResult, cv.COLOR_BGR2GRAY)
_, bw = cv.threshold(bw, 40, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow('Binary Image', bw)
# need Otsu thresholding again?
# apply distance transform
#dist = cv.distanceTransform(canny, cv.DIST_L2, 3)
dist = cv.distanceTransform(bw, cv.DIST_L2, 3)
#print(canny.dtype)
#print(dist.dtype)
# normalize the distance image for range {0.0, 1.0}
cv.normalize(dist, dist, 0, 1.0, cv.NORM_MINMAX)
cv.imshow('distance transform image', dist)
# Threshold to obtain the peaks
# This will be the markers for the foreground objects
ret, dist = cv.threshold(dist, 0.4, 1.0, cv.THRESH_BINARY)
# Dilate a bit the dist image
kernel1 = np.ones((3,3), dtype=np.uint8)
dist = cv.dilate(dist, kernel1)
cv.imshow('Peaks', dist)
# Create the CV_8U version of the distance image
# It is needed for findContours()
dist_8u = dist.astype('uint8')
# Find total markers
ret, contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for the watershed algorithm
markers = np.zeros(dist.shape, dtype=np.int32)
# Draw the foreground markers
for i in range(len(contours)):
cv.drawContours(markers, contours, i, (i+1), -1)
# Draw the background marker
cv.circle(markers, (5,5), 3, (255,255,255), -1)
cv.imshow('Markers', markers*10000)
# Perform the watershed algorithm
cv.watershed(imgResult, markers)
#mark = np.zeros(markers.shape, dtype=np.uint8)
mark = markers.astype('uint8')
mark = cv.bitwise_not(mark)
# uncomment this if you want to see how the mark
# image looks like at that point
cv.imshow('Markers_v2', mark)
# Generate random colors
colors = []
for contour in contours:
colors.append((rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)))
# Create the result image
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random colors
for i in range(markers.shape[0]):
for j in range(markers.shape[1]):
index = markers[i,j]
if index > 0 and index <= len(contours):
dst[i,j,:] = colors[index-1]
# Visualize the final image
cv.imshow('Final Result', dst)
cv.waitKey()
def cannyWatershed(inputfile):
#img = cv.imread('8ubS9.jpg')
img = cv.imread(inputfile)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
high_thresh, thresh_img = cv.threshold(gray, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
low_thresh = high_thresh * 0.5
marker = cv.GaussianBlur(gray, (5, 5), 2)
#canny = cv.Canny(marker, 40, 100)
canny = cv.Canny(marker, low_thresh, high_thresh)
_, contours, hierarchy = cv.findContours(canny, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
marker32 = np.int32(marker)
watershed = cv.watershed(img, marker32)
m = cv.convertScaleAbs(marker32)
_, thresh = cv.threshold(m, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
thresh_inv = cv.bitwise_not(thresh)
temp = cv.bitwise_and(img, img, mask=thresh)
temp1 = cv.bitwise_and(img, img, mask=thresh_inv)
result = cv.addWeighted(temp, 1, temp1, 1, 0)
final = cv.drawContours(result, contours, -1, (0, 0, 255), 1)
mask = np.zeros(img.shape, dtype=float)
edgemap = cv.drawContours(mask, contours, -1, (255, 0, 0), 1)
return edgemap
if __name__ == "__main__":
print("Hello world")
#canny_watershed(1, 1, 1, 1)
#canny_watershed('四破魚(藍圓鰺)2.jpg', 'output.jpg', 0, 100, 3)
#mean_shift('coins.jpg', 'output.jpg', 0, 100, 3)
#canny_watershed_distance_transform('coins.jpg', 'output.jpg', 0, 100, 3)
edge_map = cannyWatershed('coins.jpg')
cv.imshow('edge map', edge_map)
cv.waitKey(0)
#canny_watershed_distance_transform('四破魚(藍圓鰺)2.jpg', 'output.jpg', 0, 100, 3)
#canny_watershed('七星鱸.JPG', 'output.jpg', 0, 100, 3)
'''
with open('file_lists.txt', 'r') as f:
for line in f:
params = []
for param in line.split():
params.append(param)
outputfile = os.path.splitext(params[0])[0] + '_' + \
params[1] + '_' + params[2] + '_' + params[3] + '.bmp'
canny_watershed(params[0], outputfile, float(params[1]), int(params[2]), int(params[3]))
#filename = os.path.splitext(params[0])[0]
#print(filename)
'''
print("end")