Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cb86df8

Browse files
committed
update ch37 data
1 parent 7057efc commit cb86df8

File tree

129 files changed

+26308
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+26308
-15
lines changed

‎ch32-介绍SIFT/sift.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午2:23
3+
# @Author : play4fun
4+
# @File : sift.py
5+
# @Software: PyCharm
6+
7+
"""
8+
sift.py:尺度不变特征变换
9+
10+
关键点 极值点 定位
11+
12+
"""
13+
14+
import cv2
15+
import numpy as np
16+
17+
img = cv2.imread('../data/home.jpg')
18+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19+
20+
sift = cv2.xfeatures2d.SIFT_create()
21+
kp = sift.detect(gray, None)
22+
img = cv2.drawKeypoints(gray, kp, img)
23+
24+
# 计算关键点描述符
25+
# 使用函数 sift.compute() 来 计算 些关键点的描述符。例如
26+
# kp, des = sift.compute(gray, kp)
27+
kp, des = sift.detectAndCompute(gray,None)
28+
29+
cv2.imwrite('sift_keypoints.jpg', img)
30+
cv2.imshow('sift_keypoints.jpg', img)
31+
cv2.waitKey(0)

‎ch33-介绍SURF/surf.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午3:13
3+
# @Author : play4fun
4+
# @File : surf.py
5+
# @Software: PyCharm
6+
7+
"""
8+
surf.py:SURF加速稳健特征,加速版的SIFT
9+
10+
积分图像的一大特点是 计算图像中某个窗 口内所有像素和时,计算量的大小与窗口大小无关
11+
12+
"""
13+
import cv2
14+
import matplotlib.pyplot as plt
15+
16+
img = cv2.imread('fly.png', 0)
17+
# Create SURF object. You can specify params here or later. # Here I set Hessian Threshold to 400
18+
# surf = cv2.SURF(400)
19+
surf = cv2.xfeatures2d.SURF_create(400)
20+
21+
# Find keypoints and descriptors directly
22+
kp, des = surf.detectAndCompute(img, None)
23+
len(kp) # 699
24+
25+
# 提高Hessian 的阈值
26+
# Check present Hessian threshold
27+
print(surf.getHessianThreshold())
28+
# 400.0
29+
# We set it to some 50000. Remember, it is just for representing in picture.
30+
# In actual cases, it is better to have a value 300-500
31+
surf.setHessianThreshold(50000)
32+
# Again compute keypoints and check its number.
33+
kp, des = surf.detectAndCompute(img, None)
34+
print(len(kp))
35+
# 47
36+
img2 = cv2.drawKeypoints(img, kp, None, (255, 0, 0), 4)
37+
38+
plt.imshow(img2)
39+
plt.show()
40+
41+
# 最后我们再看看关键点描述符的大小 如果是 64 维的就改成 128 维。
42+
43+
# Find size of descriptor
44+
print(surf.descriptorSize())
45+
# 64
46+
# That means flag, "extended" is False.
47+
print(surf.getExtended())
48+
# False
49+
# So we make it to True to get 128-dim descriptors.
50+
surf.extended = True
51+
kp, des = surf.detectAndCompute(img, None)
52+
print(surf.descriptorSize())
53+
# 128
54+
print(des.shape)
55+
# (47, 128)
56+
57+
# 接下来要做的就是匹配了 我们会在后讨论。
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午3:31
3+
# @Author : play4fun
4+
# @File : fast.py
5+
# @Software: PyCharm
6+
7+
"""
8+
fast.py:FAST 特征检测器
9+
10+
效果很好。但是从实时处理的角度来看
11+
这些算法 不够快。
12+
一个最好例子就是 SLAM 同步定位与地图构建
13+
移动机器人 它们的计算资源非常有限。
14+
15+
"""
16+
17+
import numpy as np
18+
import cv2
19+
from matplotlib import pyplot as plt
20+
21+
img = cv2.imread('simple.jpg', 0)
22+
23+
# Initiate FAST object with default values
24+
fast = cv2.FastFeatureDetector_create()
25+
# find and draw the keypoints
26+
kp = fast.detect(img, None)
27+
img2 = cv2.drawKeypoints(img, kp, None, color=(255, 0, 0))
28+
29+
# Print all default params
30+
print("Threshold: ", fast.getThreshold())
31+
print("nonmaxSuppression: ", fast.getNonmaxSuppression())
32+
print("neighborhood: ", fast.getType())
33+
print("Total Keypoints with nonmaxSuppression: ", len(kp))
34+
cv2.imwrite('fast_true.png', img2)
35+
36+
# Disable nonmaxSuppression
37+
fast.setNonmaxSuppression(0)
38+
kp = fast.detect(img, None)
39+
print("Total Keypoints without nonmaxSuppression: ", len(kp))
40+
41+
img3 = cv2.drawKeypoints(img, kp, None, color=(255, 0, 0))
42+
cv2.imwrite('fast_false.png', img3)
43+
44+
#结果如下。第一幅图是使用了 非最大值抑制的结果
45+
# 第二幅没有使用非最大值抑制。

‎ch35-BRIEF/brief.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午3:38
3+
# @Author : play4fun
4+
# @File : brief.py
5+
# @Software: PyCharm
6+
7+
"""
8+
brief.py:
9+
算法使用的是已经平滑后的图像
10+
11+
非常重要的一点是 BRIEF 是一种特征描述符 它不提供查找特征的方法。 所以我们不得不使用其他特征检测器 比如 SIFT 和 SURF 等。
12+
原始文献推荐 使用 CenSurE 特征检测器 种算法很快。而且 BRIEF 算法对 CenSurE 关键点的描述效果 比 SURF 关键点的描述更好。
13+
14+
简单来 BRIEF 是一种对特征点描述符运算和匹 的快速方法。 这种算法可以实现很高的识别率,除非出现平面内的大旋 。
15+
16+
"""
17+
18+
import numpy as np
19+
import cv2
20+
from matplotlib import pyplot as plt
21+
22+
img = cv2.imread('simple.jpg', 0)
23+
24+
# Initiate FAST detector
25+
star = cv2.xfeatures2d.StarDetector_create()
26+
# Initiate BRIEF extractor
27+
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
28+
29+
# find the keypoints with STAR
30+
kp = star.detect(img, None)
31+
# compute the descriptors with BRIEF
32+
kp, des = brief.compute(img, kp)
33+
34+
print(brief.descriptorSize())
35+
print(des.shape)

‎ch36-ORB/orb.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午3:45
3+
# @Author : play4fun
4+
# @File : orb.py
5+
# @Software: PyCharm
6+
7+
"""
8+
orb.py:
9+
SIFT 和 SURF 算法是有专利保护的 如果你 使用它们 就可能要花钱 。但是 ORB 不需要
10+
11+
ORB 基本是 FAST 关 点检测和 BRIEF 关 点描 器的结合体 并
12+
很多修改增强了性能。 先它使用 FAST 找到关 点 然后再使用 Harris 点检测对 些关 点 排序找到其中的前 N 个点。它也使用 字塔从而产 生尺度不变性特征。
13+
14+
数据的方差大的一个好处是 使得特征更容易分辨。
15+
16+
对于描述符,ORB使用BRIEF描述符。但我们已经看到,这个BRIEF的表现在旋转方面表现不佳。因此,ORB所做的是根据关键点的方向来"引导"。
17+
对于在位置(xi,yi)的n个二进制测试的任何特性集,定义一个包含这些像素坐标的2 n矩阵。然后利用补丁的方向,找到旋转矩阵并旋转S,以得到引导(旋转)版本s。
18+
19+
ORB将角度进行离散化,以增加2/30(12度),并构造一个预先计算过的简短模式的查找表。只要键点的方向是一致的,就会使用正确的点集来计算它的描述符。
20+
21+
BRIEF有一个重要的属性,即每个比特的特性都有很大的方差,而平均值接近0.5。但是一旦它沿着键点方向移动,它就会失去这个属性并变得更加分散。高方差使特征更有区别,因为它对输入的响应不同。另一个可取的特性是让测试不相关,因为每个测试都将对结果有所贡献。为了解决所有这些问题,ORB在所有可能的二进制测试中运行一个贪婪的搜索,以找到那些既有高方差又接近0.5的,同时又不相关的。结果被称为rBRIEF。
22+
23+
对于描述符匹配,在传统的LSH上改进的多探测LSH是被使用的。这篇文章说,ORB比冲浪快得多,而且比冲浪还好。对于全景拼接的低功率设备,ORB是一个不错的选择。
24+
25+
"""
26+
27+
import numpy as np
28+
import cv2
29+
from matplotlib import pyplot as plt
30+
31+
img = cv2.imread('simple.jpg', 0)
32+
33+
# Initiate ORB detector
34+
orb = cv2.ORB_create()
35+
# find the keypoints with ORB
36+
kp = orb.detect(img, None)
37+
# compute the descriptors with ORB
38+
kp, des = orb.compute(img, kp)
39+
40+
# draw only keypoints location,not size and orientation
41+
img2 = cv2.drawKeypoints(img, kp, None, color=(0, 255, 0), flags=0)
42+
43+
plt.imshow(img2), plt.show()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午4:00
3+
# @Author : play4fun
4+
# @File : 37.2-对ORB描述符进行蛮力匹配.py
5+
# @Software: PyCharm
6+
7+
"""
8+
37.2-对ORB描述符进行蛮力匹配.py:
9+
匹配器对象是什么
10+
matches = bf.match(des1, des2) 返回值是一个 DMatch对象列表
11+
DMatch 对 具有下列属性
12+
• DMatch.distance - 描 符之 的 离。 小 好。
13+
• DMatch.trainIdx - 目标图像中描 符的索引。
14+
• DMatch.queryIdx - 查 图像中描 符的索引。
15+
• DMatch.imgIdx - 目标图像的索引。
16+
"""
17+
18+
import numpy as np
19+
import cv2
20+
import matplotlib.pyplot as plt
21+
22+
img1 = cv2.imread('box.png', 0) # queryImage
23+
img2 = cv2.imread('box_in_scene.png', 0) # trainImage
24+
25+
# Initiate ORB detector
26+
orb = cv2.ORB_create()
27+
# find the keypoints and descriptors with ORB
28+
kp1, des1 = orb.detectAndCompute(img1, None)
29+
kp2, des2 = orb.detectAndCompute(img2, None)
30+
31+
# create BFMatcher object
32+
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
33+
# Match descriptors.
34+
matches = bf.match(des1, des2)
35+
36+
# Sort them in the order of their distance.
37+
matches = sorted(matches, key=lambda x: x.distance)
38+
# Draw first 10 matches.
39+
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], flags=2) # 前10个匹配
40+
41+
plt.imshow(img3), plt.show()

‎ch37-特征匹配/37.4-SIFT_match.py‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
#-*-coding:utf8-*-#
1+
#-*-coding:utf8-*-#
22
__author__ = 'play4fun'
33
"""
44
create time:15-11-9 下午1:28
5+
对 SIFT 描述符进行蛮力匹配和比值测试
56
"""
67

7-
8-
98
import numpy as np
109
import cv2
1110
from matplotlib import pyplot as plt
12-
img1 = cv2.imread('../data/box.png',0)
11+
12+
img1 = cv2.imread('../data/box.png', 0)
1313
# queryImage
14-
img2 = cv2.imread('../data/box_in_scene.png',0) # trainImage
14+
img2 = cv2.imread('../data/box_in_scene.png',0) # trainImage
1515
# Initiate SIFT detector
1616
# sift = cv2.SIFT()
1717
sift = cv2.xfeatures2d.SIFT_create()
18-
#TODO
18+
1919

2020
# find the keypoints and descriptors with SIFT
21-
kp1, des1 = sift.detectAndCompute(img1,None)
22-
kp2, des2 = sift.detectAndCompute(img2,None)
21+
kp1, des1 = sift.detectAndCompute(img1, None)
22+
kp2, des2 = sift.detectAndCompute(img2, None)
23+
2324
# BFMatcher with default params
2425
bf = cv2.BFMatcher()
25-
matches = bf.knnMatch(des1,des2, k=2)
26+
matches = bf.knnMatch(des1, des2, k=2)
27+
2628
# Apply ratio test
2729
# 比值测试,首先获取与 A距离最近的点 B (最近)和 C (次近),
2830
# 只有当 B/C 小于阀值时(0.75)才被认为是匹配,
29-
#因为假设匹配是一一对应的,真正的匹配的理想距离为0
31+
#因为假设匹配是一一对应的,真正的匹配的理想距离为0
3032
good = []
31-
for m,n in matches:
32-
if m.distance < 0.75*n.distance:
33+
for m,n in matches:
34+
if m.distance < 0.75*n.distance:
3335
good.append([m])
3436

3537
# cv2.drawMatchesKnn expects list of lists as matches.
36-
img3=np.ndarray([2,2])
37-
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good[:10],img3,flags=2)
38-
plt.imshow(img3),plt.show()
38+
# img3 = np.ndarray([2, 2])
39+
# img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good[:10], img3, flags=2)
40+
41+
# cv2.drawMatchesKnn expects list of lists as matches.
42+
img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,flags=2)
43+
44+
plt.imshow(img3), plt.show()
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017年7月13日 下午4:22
3+
# @Author : play4fun
4+
# @File : 37.5-FLANN匹配器.py
5+
# @Software: PyCharm
6+
7+
"""
8+
37.5-FLANN匹配器.py:
9+
FLANN 是快速最近邻搜索包 Fast_Library_for_Approximate_Nearest_Neighbors 的简称。
10+
它是一个对大数据集和高维特征进行最近邻搜索的算法的集合
11+
而且这些算法 已经被优化 了。
12+
在面对大数据集时它的效果 好于 BFMatcher
13+
14+
"""
15+
16+
17+
18+
import numpy as np
19+
import cv2
20+
from matplotlib import pyplot as plt
21+
22+
img1 = cv2.imread('box.png', 0) # queryImage
23+
img2 = cv2.imread('box_in_scene.png', 0) # trainImage
24+
25+
# Initiate SIFT detector
26+
# sift = cv2.SIFT()
27+
sift = cv2.xfeatures2d.SIFT_create()
28+
29+
# find the keypoints and descriptors with SIFT
30+
kp1, des1 = sift.detectAndCompute(img1, None)
31+
kp2, des2 = sift.detectAndCompute(img2, None)
32+
33+
# FLANN parameters
34+
FLANN_INDEX_KDTREE = 0
35+
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
36+
search_params = dict(checks=50) # or pass empty dictionary
37+
38+
flann = cv2.FlannBasedMatcher(index_params, search_params)
39+
matches = flann.knnMatch(des1, des2, k=2)
40+
# Need to draw only good matches, so create a mask
41+
matchesMask = [[0, 0] for i in range(len(matches))]
42+
43+
# ratio test as per Lowe's paper
44+
for i, (m, n) in enumerate(matches):
45+
if m.distance < 0.7 * n.distance:
46+
matchesMask[i] = [1, 0]
47+
48+
draw_params = dict(matchColor=(0, 255, 0),
49+
singlePointColor=(255, 0, 0),
50+
matchesMask=matchesMask,
51+
flags=0)
52+
53+
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, matches, None, **draw_params)
54+
55+
plt.imshow(img3, ), plt.show()

‎data/._messi5.jpg‎

-4 KB
Binary file not shown.

‎data/H1to3p.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<opencv_storage>
3+
<H13 type_id="opencv-matrix">
4+
<rows>3</rows>
5+
<cols>3</cols>
6+
<dt>d</dt>
7+
<data>
8+
7.6285898e-01 -2.9922929e-01 2.2567123e+02
9+
3.3443473e-01 1.0143901e+00 -7.6999973e+01
10+
3.4663091e-04 -1.4364524e-05 1.0000000e+00 </data></H13>
11+
</opencv_storage>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /