-
Notifications
You must be signed in to change notification settings - Fork 87
/
Edge_detection.py
112 lines (58 loc) · 2.07 KB
/
Edge_detection.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
import cv2
import numpy as np
def draw_contours(img, cnts):
img = np.copy(img)
img = cv2.drawContours(img, cnts, -1, (0, 255, 0), 2)
return img
def draw_min_rect_circle(img, cnts):
img = np.copy(img)
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
min_rect = cv2.minAreaRect(cnt)
min_rect = np.int0(cv2.boxPoints(min_rect))
cv2.drawContours(img, [min_rect], 0, (0, 255, 0), 2)
(x, y), radius = cv2.minEnclosingCircle(cnt)
center, radius = (int(x), int(y)), int(radius)
img = cv2.circle(img, center, radius, (0, 0, 255), 2)
return img
def draw_approx_hull_polygon(img, cnts):
img = np.zeros(img.shape, dtype=np.uint8)
cv2.drawContours(img, cnts, -1, (255, 0, 0), 2)
min_side_len = img.shape[0] / 32
min_poly_len = img.shape[0] / 16
min_side_num = 3
approxs = [cv2.approxPolyDP(cnt, min_side_len, True) for cnt in cnts]
approxs = [
approx for approx in approxs
if cv2.arcLength(approx, True) > min_poly_len
]
approxs = [approx for approx in approxs if len(approx) > min_side_num]
cv2.polylines(img, approxs, True, (0, 255, 0), 2)
hulls = [cv2.convexHull(cnt) for cnt in cnts]
cv2.polylines(img, hulls, True, (0, 0, 255), 2)
return img
def run():
image = cv2.imread('Demo/test_edge_detection.jpg')
thresh = cv2.Canny(image, 128, 256)
thresh, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
"""
[[[-1 -1 -1 -1]]] :hierarchy # cv2.Canny()
[[[ 1 -1 -1 -1]
[ 2 0 -1 -1]
[ 3 1 -1 -1]
[-1 2 -1 -1]]] :hierarchy # cv2.threshold()
"""
imgs = [
image,
thresh,
draw_min_rect_circle(image, contours),
draw_approx_hull_polygon(image, contours),
]
for img in imgs:
cv2.imshow("contours", img)
cv2.waitKey(1943)
if __name__ == '__main__':
run()
pass