1
- import numpy as np
2
- import cv2
3
- from matplotlib import pyplot as plt
4
-
5
- MIN_MATCH_COUNT = 10
6
- img1 = cv2 .imread ('assets/butterfly.png' , 0 ) # 原图像
7
- img2 = cv2 .imread ('assets/scan1.jpg' , 0 ) # 待搜索图像,下面称目标图像
8
- # 启动SIFT检测器
9
- sift = cv2 .SIFT_create ()
10
- # 使用SIFT查找关键点和描述符
11
- kp1 , des1 = sift .detectAndCompute (img1 , None )
12
- kp2 , des2 = sift .detectAndCompute (img2 , None )
13
-
14
- # 使用FLANN匹配器进行匹配
15
- FLANN_INDEX_KDTREE = 1
16
- index_params = dict (algorithm = FLANN_INDEX_KDTREE , trees = 5 )
17
- search_params = dict (checks = 50 )
18
- flann = cv2 .FlannBasedMatcher (index_params , search_params )
19
- matches = flann .knnMatch (des1 , des2 , k = 2 ) # 获得匹配结果
20
-
21
- # 按照Lowe的比率存储所有好的匹配。
22
- good = []
23
- for m , n in matches :
24
- if m .distance < 0.7 * n .distance :
25
- good .append (m )
26
-
27
- # 只有好的匹配点多于10个才查找目标,否则显示匹配不足
28
- if len (good ) > MIN_MATCH_COUNT :
29
- # 获取匹配点在原图像和目标图像中的的位置
30
- # kp1:原图像的特征点
31
- # m.queryIdx:匹配点在原图像特征点中的索引
32
- # .pt:特征点的坐标
33
- src_pts = np .float32 ([kp1 [m .queryIdx ].pt for m in good ]).reshape (- 1 , 1 , 2 )
34
- dst_pts = np .float32 ([kp2 [m .trainIdx ].pt for m in good ]).reshape (- 1 , 1 , 2 )
35
- # 获取变换矩阵,采用RANSAC算法
36
- M , mask = cv2 .findHomography (src_pts , dst_pts , cv2 .RANSAC , 5.0 )
37
- matchesMask = mask .ravel ().tolist ()
38
- # 图像变换,将原图像变换为检测图像中匹配到的形状
39
- # 获得原图像尺寸
40
- h , w = img1 .shape
41
- # 使用得到的变换矩阵对原图像的四个角进行变换,获得在目标图像上对应的坐标。
42
- pts = np .float32 ([[0 , 0 ], [0 , h - 1 ], [w - 1 , h - 1 ], [w - 1 , 0 ]]
43
- ).reshape (- 1 , 1 , 2 )
44
- # 对角点进行变换
45
- dst = cv2 .perspectiveTransform (pts , M )
46
- # 画出边框
47
- img2 = cv2 .polylines (img2 , [np .int32 (dst )], True , 255 , 5 , cv2 .LINE_AA )
48
- else :
49
- print ("Not enough matches are found - {}/{}" .format (len (good ), MIN_MATCH_COUNT ))
50
- matchesMask = None
51
-
52
- # 画出匹配点
53
- draw_params = dict (matchColor = (0 , 255 , 0 ), # draw matches in green color
54
- singlePointColor = None ,
55
- matchesMask = matchesMask , # draw only inliers
56
- flags = 2 )
57
- img3 = cv2 .drawMatches (img1 , kp1 , img2 , kp2 , good , None , ** draw_params )
58
- plt .imshow (img3 ), plt .title ('Result' ),
59
- plt .axis ('off' )
1
+ import numpy as np
2
+ import cv2
3
+ from matplotlib import pyplot as plt
4
+
5
+ MIN_MATCH_COUNT = 10
6
+ img1 = cv2 .imread ('assets/butterfly.png' , 0 ) # 原图像
7
+ img2 = cv2 .imread ('assets/scan1.jpg' , 0 ) # 待搜索图像,下面称目标图像
8
+ # 启动SIFT检测器
9
+ sift = cv2 .SIFT_create ()
10
+ # 使用SIFT查找关键点和描述符
11
+ kp1 , des1 = sift .detectAndCompute (img1 , None )
12
+ kp2 , des2 = sift .detectAndCompute (img2 , None )
13
+
14
+ # 使用FLANN匹配器进行匹配
15
+ FLANN_INDEX_KDTREE = 1
16
+ index_params = dict (algorithm = FLANN_INDEX_KDTREE , trees = 5 )
17
+ search_params = dict (checks = 50 )
18
+ flann = cv2 .FlannBasedMatcher (index_params , search_params )
19
+ matches = flann .knnMatch (des1 , des2 , k = 2 ) # 获得匹配结果
20
+
21
+ # 按照Lowe的比率存储所有好的匹配。
22
+ good = []
23
+ for m , n in matches :
24
+ if m .distance < 0.7 * n .distance :
25
+ good .append (m )
26
+
27
+ # 只有好的匹配点多于10个才查找目标,否则显示匹配不足
28
+ if len (good ) > MIN_MATCH_COUNT :
29
+ # 获取匹配点在原图像和目标图像中的的位置
30
+ # kp1:原图像的特征点
31
+ # m.queryIdx:匹配点在原图像特征点中的索引
32
+ # .pt:特征点的坐标
33
+ src_pts = np .float32 ([kp1 [m .queryIdx ].pt for m in good ]).reshape (- 1 , 1 , 2 )
34
+ dst_pts = np .float32 ([kp2 [m .trainIdx ].pt for m in good ]).reshape (- 1 , 1 , 2 )
35
+ # 获取变换矩阵,采用RANSAC算法
36
+ M , mask = cv2 .findHomography (src_pts , dst_pts , cv2 .RANSAC , 5.0 )
37
+ matchesMask = mask .ravel ().tolist ()
38
+ # 图像变换,将原图像变换为检测图像中匹配到的形状
39
+ # 获得原图像尺寸
40
+ h , w = img1 .shape
41
+ # 使用得到的变换矩阵对原图像的四个角进行变换,获得在目标图像上对应的坐标。
42
+ pts = np .float32 ([[0 , 0 ], [0 , h - 1 ], [w - 1 , h - 1 ], [w - 1 , 0 ]]
43
+ ).reshape (- 1 , 1 , 2 )
44
+ # 对角点进行变换
45
+ dst = cv2 .perspectiveTransform (pts , M )
46
+ # 画出边框
47
+ img2 = cv2 .polylines (img2 , [np .int32 (dst )], True , 255 , 5 , cv2 .LINE_AA )
48
+ else :
49
+ print ("Not enough matches are found - {}/{}" .format (len (good ), MIN_MATCH_COUNT ))
50
+ matchesMask = None
51
+
52
+ # 画出匹配点
53
+ draw_params = dict (matchColor = (0 , 255 , 0 ), # draw matches in green color
54
+ singlePointColor = None ,
55
+ matchesMask = matchesMask , # draw only inliers
56
+ flags = 2 )
57
+ img3 = cv2 .drawMatches (img1 , kp1 , img2 , kp2 , good , None , ** draw_params )
58
+ plt .imshow (img3 ), plt .title ('Result' ),
59
+ plt .axis ('off' )
60
60
plt .show ()
0 commit comments