Skip to content

Commit 787785f

Browse files
committed
update for zhihu TUTR_edge_detection
1 parent 72db03c commit 787785f

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

Demo/TUTR_edge_detection.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
import numpy as np
33

44
'''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]'''
69

710

811
def draw_contours(img, cnts): # conts = contours
@@ -13,6 +16,7 @@ def draw_contours(img, cnts): # conts = contours
1316

1417
def draw_min_rect_circle(img, cnts): # conts = contours
1518
img = np.copy(img)
19+
1620
for cnt in cnts:
1721
x, y, w, h = cv2.boundingRect(cnt)
1822
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
3034
def draw_approx_hull_polygon(img, cnts):
3135
# img = np.copy(img)
3236
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
3537

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
3946

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
4256
return img
4357

4458

4559
def run():
46-
image = cv2.imread('test.png')
60+
image = cv2.imread('test.png') # a black objects on white image is better
4761

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)
4964
thresh = cv2.Canny(image, 128, 256)
5065

5166
thresh, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
67+
# print(hierarchy, ":hierarchy")
5268
"""
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()
5875
"""
5976

6077
imgs = [
@@ -64,9 +81,9 @@ def run():
6481
]
6582

6683
for img in imgs:
67-
cv2.imwrite("%s.jpg" % id(img), img)
84+
# cv2.imwrite("%s.jpg" % id(img), img)
6885
cv2.imshow("contours", img)
69-
cv2.waitKey(1234)
86+
cv2.waitKey(1943)
7087

7188

7289
if __name__ == '__main__':

0 commit comments

Comments
 (0)