Skip to content

Commit f87335e

Browse files
SailByCodeSailByCode
authored andcommitted
update ch22
1 parent f76b0eb commit f87335e

File tree

8 files changed

+37
-18
lines changed

8 files changed

+37
-18
lines changed

ch21-轮廓Contours/21.4.3-形状匹配.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,31 @@
1414
import cv2
1515
import numpy as np
1616

17-
img1 = cv2.imread('star.jpg', 0)
18-
img2 = cv2.imread('star2.jpg', 0)
19-
20-
ret, thresh = cv2.threshold(img1, 127, 255, 0)
21-
ret, thresh2 = cv2.threshold(img2, 127, 255, 0)
22-
23-
image,contours, hierarchy = cv2.findContours(thresh, 2, 1)
24-
cnt1 = contours[0]
25-
image,contours, hierarchy = cv2.findContours(thresh2, 2, 1)
26-
cnt2 = contours[0]
27-
28-
ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0)
17+
img_a = cv2.imread('star_a.jpg', 0)
18+
img_b = cv2.imread('star_b.jpg', 0)
19+
img_c = cv2.imread('star_c.jpg', 0)
20+
img = cv2.imread('star.jpg', 0)
21+
22+
23+
ret, thresh = cv2.threshold(img, 127, 255, 0)
24+
ret, thresh_a = cv2.threshold(img_a, 127, 255, 0)
25+
ret, thresh_b = cv2.threshold(img_b, 127, 255, 0)
26+
ret, thresh_c = cv2.threshold(img_c, 127, 255, 0)
27+
28+
contours, hierarchy = cv2.findContours(thresh_a, 2, 1)
29+
cnt_a = contours[0]
30+
contours, hierarchy = cv2.findContours(thresh_b, 2, 1)
31+
cnt_b = contours[0]
32+
contours, hierarchy = cv2.findContours(thresh_c, 2, 1)
33+
cnt_c = contours[0]
34+
35+
# ret = cv2.matchShapes(cnt1, cnt2, 1, 0.0)
36+
# print(ret)
37+
ret = cv2.matchShapes(cnt_a, cnt_b, 1, 0.0)
38+
print(ret)
39+
ret = cv2.matchShapes(cnt_a, cnt_c, 1, 0.0)
40+
print(ret)
41+
ret = cv2.matchShapes(cnt_b, cnt_c, 1, 0.0)
2942
print(ret)
3043

3144
#Hu 矩是归一化中心矩的线性组合

ch21-轮廓Contours/minAreaRect-旋转矩形.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
img = cv2.imread('../data/lightning.png')
1515
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
1616

17-
image, contours, hierarchy = cv2.findContours(imgray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
17+
contours, hierarchy = cv2.findContours(imgray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
1818
contours = [cnt for cnt in contours if cv2.contourArea(cnt) > 100]
1919
print('len(contours)', len(contours))
2020
cnt = contours[0]

ch21-轮廓Contours/star_a.jpg

6.51 KB
Loading

ch21-轮廓Contours/star_b.jpg

9.08 KB
Loading

ch21-轮廓Contours/star_c.jpg

2.34 KB
Loading

ch21-轮廓Contours/凸包-凸性检测-边界矩形-最小外接圆-拟合.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212

1313
img=cv2.imread('../data/lightning.png',0)
1414

15-
image, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
15+
contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
1616

1717
cnt=contours[0]
1818
'''
19-
函数 cv2.convexHull() 可以用来检测一个曲线是否具有凸性缺 并能纠 正缺 。一般来 凸性曲线总是凸出来的 至少是平的。如果有地方凹 去 了就 叫做凸性缺
19+
函数 cv2.convexHull() 可以用来检测一个曲线是否具有凸性缺 并能纠 正缺 。一般来 凸性曲线总是凸出来的 至少是平的。如果有地方凹 去 了就 叫做凸性缺
2020
例如下图中的手。红色曲线显示了手的凸包 凸性缺 双箭头标出来了。
2121
'''
2222
# convexHull(points, hull=None, clockwise=None, returnPoints=None)
23-
hull = cv2.convexHull(points, hull, clockwise, returnPoints)
23+
# hull = cv2.convexHull(points, hull, clockwise, returnPoints)
2424

2525
'''
2626
points 我们 传入的 廓
27-
• hull 输出 通常不需要
27+
• hull 输出 通常不需要
2828
• clockwise 方向标志。如果 置为 True 出的凸包是顺时针 方向的。 否则为逆时针 方向。
2929
• returnPoints 值为 True。它会 回凸包上点的坐标。如果 置 为 False 就会 回与凸包点对应的 廓上的点。
3030
'''

ch22-直方图/22.1.2-绘制直方图-matplotlib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
from matplotlib import pyplot as plt
1010

1111
img = cv2.imread('../data/home.jpg', 0)
12+
13+
plt.subplots(1, 2)
14+
plt.subplot(121)
15+
plt.imshow(img)
16+
17+
plt.subplot(122)
1218
# img.ravel() 将图像转成一维数组 没有中括号
1319
plt.hist(img.ravel(), 256, [0, 256])
1420
plt.show()
21+

ch22-直方图/22.3-2D直方图.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@
3737

3838
hist, xbins, ybins = np.histogram2d(h.ravel(), s.ravel(), [180, 256], [[0, 180], [0, 256]])
3939

40-
pass

0 commit comments

Comments
 (0)