2
2
import numpy as np
3
3
4
4
'''REFER: https://hub.packtpub.com/opencv-detecting-edges-lines-shapes/'''
5
- '''Yonv1943 2018-06-30 23:00:14'''
5
+ '''Yonv1943 2018-06-30 '''
6
+ '''Yonv1943 2018-07-01 comment to test.png'''
7
+ '''Yonv1943 2018-07-01 gray in threshold, hierarchy'''
8
+ '''Yonv1943 2018-07-01 draw_approx_hull_polygon() no [for loop]'''
6
9
7
10
8
11
def draw_contours (img , cnts ): # conts = contours
@@ -13,6 +16,7 @@ def draw_contours(img, cnts): # conts = contours
13
16
14
17
def draw_min_rect_circle (img , cnts ): # conts = contours
15
18
img = np .copy (img )
19
+
16
20
for cnt in cnts :
17
21
x , y , w , h = cv2 .boundingRect (cnt )
18
22
cv2 .rectangle (img , (x , y ), (x + w , y + h ), (255 , 0 , 0 ), 2 ) # blue
@@ -30,31 +34,44 @@ def draw_min_rect_circle(img, cnts): # conts = contours
30
34
def draw_approx_hull_polygon (img , cnts ):
31
35
# img = np.copy(img)
32
36
img = np .zeros (img .shape , dtype = np .uint8 )
33
- for cnt in cnts :
34
- img = cv2 .drawContours (img , cnts , - 1 , (255 , 0 , 0 ), 2 ) # blue
35
37
36
- epsilon = 0.02 * cv2 .arcLength (cnt , True )
37
- approx = cv2 .approxPolyDP (cnt , epsilon , True )
38
- cv2 .polylines (img , [approx , ], True , (0 , 255 , 0 ), 2 ) # green
38
+ cv2 .drawContours (img , cnts , - 1 , (255 , 0 , 0 ), 2 ) # blue
39
+
40
+ epsilion = img .shape [0 ]/ 32
41
+ approxes = [cv2 .approxPolyDP (cnt , epsilion , True ) for cnt in cnts ]
42
+ cv2 .polylines (img , approxes , True , (0 , 255 , 0 ), 2 ) # green
43
+
44
+ hulls = [cv2 .convexHull (cnt ) for cnt in cnts ]
45
+ cv2 .polylines (img , hulls , True , (0 , 0 , 255 ), 2 ) # red
39
46
40
- hull = cv2 .convexHull (cnt )
41
- cv2 .polylines (img , [hull , ], True , (0 , 0 , 255 ), 2 ) # red
47
+ # for cnt in cnts:
48
+ # cv2.drawContours(img, [cnt, ], -1, (255, 0, 0), 2) # blue
49
+ #
50
+ # epsilon = 0.02 * cv2.arcLength(cnt, True)
51
+ # approx = cv2.approxPolyDP(cnt, epsilon, True)
52
+ # cv2.polylines(img, [approx, ], True, (0, 255, 0), 2) # green
53
+ #
54
+ # hull = cv2.convexHull(cnt)
55
+ # cv2.polylines(img, [hull, ], True, (0, 0, 255), 2) # red
42
56
return img
43
57
44
58
45
59
def run ():
46
- image = cv2 .imread ('test.png' )
60
+ image = cv2 .imread ('test.png' ) # a black objects on white image is better
47
61
48
- # ret, thresh = cv2.threshold(cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
62
+ # gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
63
+ # ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
49
64
thresh = cv2 .Canny (image , 128 , 256 )
50
65
51
66
thresh , contours , hierarchy = cv2 .findContours (thresh , cv2 .RETR_EXTERNAL , cv2 .CHAIN_APPROX_SIMPLE )
67
+ # print(hierarchy, ":hierarchy")
52
68
"""
53
- print("hierarchy:", hierarchy)
54
- hierarchy: [[[1, -1, -1, -1],
55
- [2, 0, -1, -1],
56
- [3, 1, -1, -1],
57
- [-1, 2, -1, -1],]]
69
+ [[[-1 -1 -1 -1]]] :hierarchy # cv2.Canny()
70
+
71
+ [[[ 1 -1 -1 -1]
72
+ [ 2 0 -1 -1]
73
+ [ 3 1 -1 -1]
74
+ [-1 2 -1 -1]]] :hierarchy # cv2.threshold()
58
75
"""
59
76
60
77
imgs = [
@@ -64,9 +81,9 @@ def run():
64
81
]
65
82
66
83
for img in imgs :
67
- cv2 .imwrite ("%s.jpg" % id (img ), img )
84
+ # cv2.imwrite("%s.jpg" % id(img), img)
68
85
cv2 .imshow ("contours" , img )
69
- cv2 .waitKey (1234 )
86
+ cv2 .waitKey (1943 )
70
87
71
88
72
89
if __name__ == '__main__' :
0 commit comments