Skip to content

Commit 66f5038

Browse files
committed
093~099 Feature descriptor matching
1 parent 3f7fc30 commit 66f5038

32 files changed

+1780
-4
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,12 @@ code_087 | [Video Analysis-Frame Difference Moving Object Analysis](python/code_
129129
code_088 | [Video Analysis-Meanshift](python/code_088) | ✏️
130130
code_089 | [Video Analysis-CamShift](python/code_089) | ✏️
131131
code_090 | [Video Analysis-Object Movement Trajectory Drawing](python/code_090) | ✔️
132-
code_090 | [Object Detection-HAAR Cascade Classification ](python/code_091) | ✔️
133-
code_090 | [Object Detection-HAAR Feature Analysis](python/code_092) | ✔️
132+
code_091 | [Object Detection-HAAR Cascade Classification ](python/code_091) | ✔️
133+
code_092 | [Object Detection-HAAR Feature Analysis](python/code_092) | ✔️
134+
code_093 | [Object Detection-LBP Feature Analysis](python/code_093/opencv_093.py) | ✔️
135+
code_094 | [ORB Feature Critical Point Detection](python/code_094) | ✏️
136+
code_095 | [ORB Feature Descriptor Matching](python/code_095) | ✔️
137+
code_096 | [Multiple Descriptor Matching Methods](python/code_096) | ✏️
138+
code_097 | [Location of Known Objects Based on Descriptor Matches](python/code_097) | ✏️
139+
code_098 | [SIFT Feature Critical Point Detection](python/code_097) | ✔️
140+
code_099 | [SIFT Feature Descriptor Matching](python/code_097) | ✔️

README_CN.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,12 @@ code_087 | [视频分析-帧差移动对象分析](python/code_087/opencv_087.py
128128
code_088 | [视频分析-均值迁移](python/code_088) | ✏️
129129
code_089 | [视频分析-连续自适应均值迁移](python/code_089) | ✏️
130130
code_090 | [视频分析-对象移动轨迹绘制](python/code_090) | ✔️
131-
code_090 | [对象检测-HAAR级联分类器](python/code_091) | ✔️
132-
code_090 | [对象检测-HAAR特征分析](python/code_092) | ✔️
131+
code_091 | [对象检测-HAAR级联分类器](python/code_091) | ✔️
132+
code_092 | [对象检测-HAAR特征分析](python/code_092) | ✔️
133+
code_093 | [对象检测-LBP特征分析](python/code_093/opencv_093.py) | ✔️
134+
code_094 | [ORB 特征关键点检测](python/code_094) | ✏️
135+
code_095 | [ORB 特征描述子匹配](python/code_095) | ✔️
136+
code_096 | [多种描述子匹配方法](python/code_096) | ✏️
137+
code_097 | [基于描述子匹配的已知对象定位](python/code_097) | ✏️
138+
code_098 | [SIFT 特征关键点检测](python/code_097) | ✔️
139+
code_099 | [SIFT 特征描述子匹配](python/code_097) | ✔️

python/code_093/faces_lbp.jpg

63.4 KB
Loading

python/code_093/lbpcascade_frontalface_improved.xml

+1,469
Large diffs are not rendered by default.

python/code_093/opencv_093.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import cv2 as cv
2+
3+
#capture = cv.VideoCapture(1)
4+
detector = cv.CascadeClassifier("lbpcascade_frontalface_improved.xml")
5+
image = cv.imread('people.jpg')
6+
7+
faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1,
8+
minSize=(30, 30), maxSize=(200, 200))
9+
for x, y, width, height in faces:
10+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
11+
12+
cv.imshow("faces", image)
13+
cv.imwrite("faces_lbp.jpg", image)
14+
15+
c = cv.waitKey(0)
16+
'''
17+
while True:
18+
ret, image = capture.read()
19+
if ret is True:
20+
cv.imshow("frame", image)
21+
faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1,
22+
minSize=(30, 30), maxSize=(120, 120))
23+
for x, y, width, height in faces:
24+
cv.rectangle(image, (x, y), (x+width, y+height), (0, 0, 255), 2, cv.LINE_8, 0)
25+
cv.imshow("faces", image)
26+
c = cv.waitKey(50)
27+
if c == 27:
28+
break
29+
else:
30+
break
31+
'''
32+
cv.destroyAllWindows()
33+

python/code_093/people.jpg

32.3 KB
Loading

python/code_094/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_094/opencv_094.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("test.jpg")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
# 创建orb检测器
8+
orb = cv.ORB_create()
9+
kps = orb.detect(src)
10+
# -1表示随机颜色
11+
result = cv.drawKeypoints(src, kps, None, -1, cv.DrawMatchesFlags_DEFAULT)
12+
cv.imshow("result", result)
13+
cv.imwrite('orb_result.jpg', result)
14+
cv.waitKey(0)
15+
cv.destroyAllWindows()

python/code_094/orb_result.jpg

136 KB
Loading

python/code_094/test.jpg

119 KB
Loading

python/code_094/test.png

811 KB
Loading

python/code_095/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_095/box.png

49.5 KB
Loading

python/code_095/box_in_scene.png

120 KB
Loading

python/code_095/opencv_095.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import cv2 as cv
2+
3+
box = cv.imread("D:/vcprojects/data/box.png",0);
4+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png",0);
5+
cv.imshow("box", box)
6+
cv.imshow("box_in_sence", box_in_sence)
7+
8+
# 创建ORB特征检测器
9+
orb = cv.ORB_create()
10+
11+
kp1, des1 = orb.detectAndCompute(box,None)
12+
kp2, des2 = orb.detectAndCompute(box_in_sence,None)
13+
14+
# 暴力匹配 汉明距离匹配特征点
15+
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
16+
matches = bf.match(des1,des2)
17+
18+
# 绘制匹配
19+
result = cv.drawMatches(box, kp1, box_in_sence, kp2, matches, None)
20+
cv.imshow("orb-match", result)
21+
cv.waitKey(0)
22+
cv.destroyAllWindows()
23+
24+

python/code_095/opencv_095_1.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Date: 2019-07-17 15:40:17
5+
6+
@author: JimmyHua
7+
"""
8+
9+
import cv2 as cv
10+
11+
box = cv.imread("D:/vcprojects/data/box.png",0);
12+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png",0);
13+
cv.imshow("box", box)
14+
cv.imshow("box_in_sence", box_in_sence)
15+
16+
# 创建ORB特征检测器
17+
orb = cv.ORB_create()
18+
19+
kp1, des1 = orb.detectAndCompute(box,None)
20+
kp2, des2 = orb.detectAndCompute(box_in_sence,None)
21+
22+
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
23+
# knn match
24+
matches = bf.knnMatch(des1, des2, k=1)
25+
26+
# 删除matches里面的空list,并且根据距离排序
27+
while [] in matches:
28+
matches.remove([])
29+
matches = sorted(matches, key = lambda x:x[0].distance)
30+
31+
# 画出距离最短的前15个点
32+
result = cv.drawMatchesKnn(box, kp1, box_in_sence, kp2, matches[0:15], None, matchColor = (0,255,0), singlePointColor = (255,0,255))
33+
cv.imshow("orb-match", result)
34+
cv.imwrite("orb-match-1.jpg", result)
35+
cv.waitKey(0)
36+
cv.destroyAllWindows()
37+
38+

python/code_095/opencv_095_2.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Date: 2019-07-17 15:27:36
5+
6+
@author: JimmyHua
7+
"""
8+
9+
import cv2 as cv
10+
11+
box = cv.imread("D:/vcprojects/data/box.png",0);
12+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png",0);
13+
cv.imshow("box", box)
14+
cv.imshow("box_in_sence", box_in_sence)
15+
16+
# 创建ORB特征检测器
17+
orb = cv.xfeatures2d.SIFT_create()
18+
19+
kp1, des1 = orb.detectAndCompute(box,None)
20+
kp2, des2 = orb.detectAndCompute(box_in_sence,None)
21+
22+
23+
index_params = dict(algorithm = 0, trees = 5)
24+
25+
search_params = dict(checks=20)
26+
27+
flann = cv.FlannBasedMatcher(index_params,search_params)
28+
29+
matches = flann.knnMatch(des1, des2, k=2)
30+
print(matches[0:5])
31+
32+
# 记录好的点
33+
goodMatches = [[0,0] for i in range(len(matches))]
34+
35+
for i,(m,n) in enumerate(matches):
36+
if m.distance < 0.7*n.distance:
37+
goodMatches[i]=[1,0]
38+
39+
draw_params = dict(matchColor = (0,255,0), singlePointColor = (255,0,0), matchesMask = goodMatches, flags = 0)
40+
41+
result = cv.drawMatchesKnn(box, kp1, box_in_sence, kp2, matches, None, **draw_params)
42+
43+
cv.imshow("orb-match", result)
44+
cv.imwrite("orb-match-2.jpg", result)
45+
cv.waitKey(0)
46+
cv.destroyAllWindows()
47+
48+

python/code_095/orb-match-1.jpg

142 KB
Loading

python/code_095/orb-match-2.jpg

174 KB
Loading

python/code_095/orb-match.jpg

154 KB
Loading

python/code_096/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_096/opencv_096.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cv2 as cv
2+
3+
box = cv.imread("D:/vcprojects/data/box.png");
4+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png");
5+
cv.imshow("box", box)
6+
cv.imshow("box_in_sence", box_in_sence)
7+
8+
# 创建ORB特征检测器
9+
orb = cv.ORB_create()
10+
kp1, des1 = orb.detectAndCompute(box,None)
11+
kp2, des2 = orb.detectAndCompute(box_in_sence,None)
12+
13+
# 暴力匹配,汉明距离匹配特征点
14+
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
15+
matches = bf.match(des1,des2)
16+
17+
# 绘制匹配,只提取距离最小的10个点
18+
matches = sorted(matches, key = lambda x:x.distance)
19+
result = cv.drawMatches(box, kp1, box_in_sence, kp2, matches[:10], None)
20+
cv.imshow("orb-match", result)
21+
cv.imwrite("orb-match.jpg", result)
22+
cv.waitKey(0)
23+
cv.destroyAllWindows()
24+
25+

python/code_096/orb-match.jpg

136 KB
Loading

python/code_097/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_097/opencv_097.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@Date: 2019-07-16 16:27:14
5+
6+
@author: JimmyHua
7+
"""
8+
9+
import cv2 as cv
10+
import numpy as np
11+
12+
box = cv.imread("D:/vcprojects/data/box.png");
13+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png");
14+
cv.imshow("box", box)
15+
cv.imshow("box_in_sence", box_in_sence)
16+
17+
# 创建ORB特征检测器
18+
orb = cv.ORB_create()
19+
kp1, des1 = orb.detectAndCompute(box,None)
20+
kp2, des2 = orb.detectAndCompute(box_in_sence,None)
21+
22+
# 暴力匹配
23+
bf = cv.BFMatcher(cv.NORM_HAMMING, crossCheck=True)
24+
matches = bf.match(des1,des2)
25+
goodMatches = []
26+
27+
# 筛选出好的描述子
28+
matches = sorted(matches, key = lambda x:x.distance)
29+
for i in range(len(matches)):
30+
if (matches[i].distance < 0.46 * matches[-1].distance):
31+
goodMatches.append(matches[i])
32+
33+
result = cv.drawMatches(box, kp1, box_in_sence, kp2, goodMatches, None)
34+
35+
obj_pts, scene_pts = [], []
36+
37+
# 单独保存 obj 和 scene 好的点位置
38+
for f in goodMatches:
39+
obj_pts.append(kp1[f.queryIdx].pt)
40+
scene_pts.append(kp2[f.trainIdx].pt)
41+
42+
#H, _= cv.findHomography(np.float32(obj_pts), np.float32(scene_pts), cv.RANSAC)
43+
44+
H, _ = cv.findHomography(np.float32(obj_pts), np.float32(scene_pts), cv.RHO)
45+
46+
h, w = box.shape[0:2]
47+
48+
pts = np.float32([[0, 0], [0, h], [w, h], [w, 0]]).reshape(-1, 1, 2)
49+
dst = cv.perspectiveTransform(pts, H).reshape(-1, 2)
50+
# 加上偏移量
51+
for i in range(4):
52+
dst[i][0] += w
53+
54+
cv.polylines(result, [np.int32(dst)], True, (0, 255, 0), 3, cv.LINE_AA)
55+
56+
cv.imshow("orb-match", result)
57+
cv.imwrite("orv-match.jpg", result)
58+
59+
cv.waitKey(0)
60+
cv.destroyAllWindows()

python/code_097/orv-match.jpg

138 KB
Loading

python/code_098/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_098/flower.jpg

155 KB
Loading

python/code_098/opencv_098.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import cv2 as cv
2+
import numpy as np
3+
4+
src = cv.imread("flower.jpg")
5+
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
6+
cv.imshow("input", src)
7+
sift = cv.xfeatures2d.SIFT_create()
8+
kps = sift.detect(src)
9+
result = cv.drawKeypoints(src, kps, None, -1, cv.DrawMatchesFlags_DEFAULT)
10+
cv.imshow("sift-detector", result)
11+
cv.waitKey(0)
12+
cv.destroyAllWindows()
13+

python/code_099/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#### More Detail, please check the blog of zhihu as below
2+
✈️ ✈️ ✈️ [OpenCV图像处理- 特征点检测、描述子匹配](https://zhuanlan.zhihu.com/p/74040063)

python/code_099/opencv_099.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cv2 as cv
2+
3+
box = cv.imread("D:/vcprojects/data/box.png");
4+
box_in_sence = cv.imread("D:/vcprojects/data/box_in_scene.png");
5+
cv.imshow("box", box)
6+
cv.imshow("box_in_sence", box_in_sence)
7+
8+
# 创建ORB特征检测器
9+
sift = cv.xfeatures2d.SIFT_create()
10+
kp1, des1 = sift.detectAndCompute(box,None)
11+
kp2, des2 = sift.detectAndCompute(box_in_sence,None)
12+
13+
# 暴力匹配
14+
bf = cv.DescriptorMatcher_create(cv.DescriptorMatcher_BRUTEFORCE)
15+
matches = bf.match(des1,des2)
16+
17+
# 绘制匹配
18+
matches = sorted(matches, key = lambda x:x.distance)
19+
result = cv.drawMatches(box, kp1, box_in_sence, kp2, matches[:15], None)
20+
cv.imshow("orb-match", result)
21+
cv.imwrite("orb-match.jpg", result)
22+
cv.waitKey(0)
23+
cv.destroyAllWindows()
24+
25+

python/code_099/orb-match.jpg

160 KB
Loading

0 commit comments

Comments
 (0)