1
1
import cv2
2
2
import numpy as np
3
3
4
- '''REFER: https://hub.packtpub.com/opencv-detecting-edges-lines-shapes/'''
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]'''
4
+ """
5
+ REFER: https://hub.packtpub.com/opencv-detecting-edges-lines-shapes/
6
+ 2018-06-30 Yonv1943
7
+ 2018-07-01 comment to test.png
8
+ 2018-07-01 gray in threshold, hierarchy
9
+ 2018-07-01 draw_approx_hull_polygon() no [for loop]
10
+ 2018-11-24
11
+ """
9
12
10
13
11
14
def draw_contours (img , cnts ): # conts = contours
@@ -37,9 +40,14 @@ def draw_approx_hull_polygon(img, cnts):
37
40
38
41
cv2 .drawContours (img , cnts , - 1 , (255 , 0 , 0 ), 2 ) # blue
39
42
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
+ min_side_len = img .shape [0 ] / 32 # 多边形边长的最小值 the minimum side length of polygon
44
+ min_poly_len = img .shape [0 ] / 16 # 多边形周长的最小值 the minimum round length of polygon
45
+ min_side_num = 3 # 多边形边数的最小值
46
+ approxs = [cv2 .approxPolyDP (cnt , min_side_len , True ) for cnt in cnts ] # 以最小边长为限制画出多边形
47
+ approxs = [approx for approx in approxs if cv2 .arcLength (approx , True ) > min_poly_len ] # 筛选出周长大于 min_poly_len 的多边形
48
+ approxs = [approx for approx in approxs if len (approx ) > min_side_num ] # 筛选出边长数大于 min_side_num 的多边形
49
+ # Above codes are written separately for the convenience of presentation.
50
+ cv2 .polylines (img , approxs , True , (0 , 255 , 0 ), 2 ) # green
43
51
44
52
hulls = [cv2 .convexHull (cnt ) for cnt in cnts ]
45
53
cv2 .polylines (img , hulls , True , (0 , 0 , 255 ), 2 ) # red
@@ -57,7 +65,7 @@ def draw_approx_hull_polygon(img, cnts):
57
65
58
66
59
67
def run ():
60
- image = cv2 .imread ('test.png ' ) # a black objects on white image is better
68
+ image = cv2 .imread ('Demo/test_edge_detection.jpg ' ) # a black objects on white image is better
61
69
62
70
# gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
63
71
# ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
@@ -89,4 +97,3 @@ def run():
89
97
if __name__ == '__main__' :
90
98
run ()
91
99
pass
92
-
0 commit comments