Skip to content

Commit 3ef918b

Browse files
committed
polygon
TUTO_edge_detection.py
1 parent 1414c04 commit 3ef918b

File tree

6 files changed

+27
-29
lines changed

6 files changed

+27
-29
lines changed

Demo/DEMO_edge_detection.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55

66
"""
77
2018-06-03 Yonv1943
8-
2018-06-08 stable compare the real time frames, and draw the contours of the moving objects
8+
2018-06-08 stable, compare the real time frames, and draw the contours of the moving objects
99
2018-07-02 setattr(), if is_opened
10-
2018-10-19 auto_canny
11-
12-
13-
def auto_canny(image, sigma=0.33):
14-
# compute the median of the single channel pixel intensities
15-
v = np.median(image)
16-
17-
lower = int(max(0, (1.0 - sigma) * v))
18-
upper = int(min(255, (1.0 + sigma) * v))
19-
edged = cv2.Canny(image, lower, upper)
20-
return edged
21-
22-
10+
2018-11-24 polygon
11+
"""
12+
13+
2314
class DrawROI(object): # draw Region of Interest
2415
def __init__(self, img):
2516
self.img = img

Demo/TUTO_edge_detection.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import cv2
22
import numpy as np
33

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+
"""
912

1013

1114
def draw_contours(img, cnts): # conts = contours
@@ -37,9 +40,14 @@ def draw_approx_hull_polygon(img, cnts):
3740

3841
cv2.drawContours(img, cnts, -1, (255, 0, 0), 2) # blue
3942

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
4351

4452
hulls = [cv2.convexHull(cnt) for cnt in cnts]
4553
cv2.polylines(img, hulls, True, (0, 0, 255), 2) # red
@@ -57,7 +65,7 @@ def draw_approx_hull_polygon(img, cnts):
5765

5866

5967
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
6169

6270
# gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
6371
# ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
@@ -89,4 +97,3 @@ def run():
8997
if __name__ == '__main__':
9098
run()
9199
pass
92-
Binary file not shown.

Demo/test_edge_detection.jpg

13.1 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
git clone https://github.com/Yonv1943/Python
55

6-
git config --global user.email "*********@qq.com"
6+
git config --global user.email "178320049@qq.com"
77

88
git config --global user.name "Yonv"
99

main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from test import main
1+
# from test import main
2+
# main()
3+
from Demo.TUTO_edge_detection import run
24

3-
if __name__ == '__main__':
4-
main()
5-
pass
5+
run()

0 commit comments

Comments
 (0)