Skip to content

Commit a821831

Browse files
committed
null
1 parent 8200123 commit a821831

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

Extra-02-High-Quality-Save-and-Matplotlib/cv2_using_matplotlib_show_image.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import cv2
2+
# from PIL import Image
23
import matplotlib.image as pli
34
import matplotlib.pyplot as plt
45

6+
57
# 1.显示灰度图
68
img = cv2.imread('lena.jpg', 0)
79
plt.imshow(img, cmap='gray')

Extra-10-Contours-Hierarchy/cv2_exercise1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
99

1010
# 2.使用cv2.RETR_CCOMP寻找轮廓
11-
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, 2)
11+
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, 2)
1212

1313
# 3.找到内层轮廓并填充
1414
# hierarchy的形状为(1,6,4),使用np.squeeze压缩一维数据,变成(6,4)

Extra-10-Contours-Hierarchy/cv2_understand_hierarchy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
88

99
# 2.寻找轮廓 cv2.RETR_TREE
10-
image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, 2)
10+
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, 2)
1111

1212
# 3.绘制轮廓
1313
print(len(contours), hierarchy, sep='\n') # 8条
@@ -20,7 +20,7 @@
2020

2121
# 不同轮廓寻找方式的不同
2222
# cv2.RETR_LIST
23-
_, _, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, 2)
23+
_, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, 2)
2424
print(hierarchy)
2525
# 结果应该如下:
2626
# [[[1 - 1 - 1 - 1]
@@ -34,7 +34,7 @@
3434

3535

3636
# cv2.RETR_EXTERNAL
37-
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 2)
37+
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, 2)
3838
print(len(contours), hierarchy, sep='\n')
3939
# 结果应该如下:
4040
# 3
@@ -44,7 +44,7 @@
4444

4545

4646
# cv2.RETR_CCOMP
47-
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, 2)
47+
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, 2)
4848
print(hierarchy)
4949
# 结果应该如下:
5050
# [[[1 - 1 - 1 - 1]

Extra-11-Convex-Hull/cv2_convex_hull.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
# 1.先找到轮廓
77
img = cv2.imread('unregular.jpg', 0)
88
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
9-
image, contours, hierarchy = cv2.findContours(thresh, 3, 2)
9+
contours, hierarchy = cv2.findContours(thresh, 3, 2)
1010
cnt = contours[0]
1111

1212
# 2.进行多边形逼近,得到多边形的角点
1313
approx = cv2.approxPolyDP(cnt, 3, True)
1414

1515
# 3.画出多边形
16-
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
16+
image = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
1717
cv2.polylines(image, [approx], True, (0, 255, 0), 2)
1818
print(len(approx)) # 角点的个数
1919
cv2.imshow('approxPloyDP', image)
@@ -24,14 +24,14 @@
2424
# 1.先找到轮廓
2525
img = cv2.imread('convex.jpg', 0)
2626
_, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
27-
image, contours, hierarchy = cv2.findContours(thresh, 3, 2)
27+
contours, hierarchy = cv2.findContours(thresh, 3, 2)
2828
cnt = contours[0]
2929

3030
# 2.寻找凸包,得到凸包的角点
3131
hull = cv2.convexHull(cnt)
3232

3333
# 3.绘制凸包
34-
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
34+
image = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
3535
cv2.polylines(image, [hull], True, (0, 255, 0), 2)
3636
cv2.imshow('convex hull', image)
3737
cv2.waitKey(0)

0 commit comments

Comments
 (0)