• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

视觉SLAM学习【6】—–基于python的SIFT和ORB特征点提取及匹配的对比探索

人工智能 陈一月的编程岁月 2469次浏览 0个评论

视觉SLAM学习【6】—–基于python的SIFT和ORB特征点提取及匹配的对比探索目录

 

  • 一、python终端安装需要的图像处理库
    • 1、打开终端,安装opencv-python
    • 2、如果之前安装其他版本操作步骤
    • 3、准备图片数据
  • 二、SIFT特征提取及匹配
    • 1、导入需要的python库
    • 2、导入图片
    • 3、构建仿射图
    • 4、将img转换为RGB格式,避免色差
    • 5、SIFT特征提取及提取时间计算
    • 6、SIFT特征匹配
  • 三、ORB特征提取及匹配
    • 1、导入图片
    • 2、构建仿射图
    • 3、将img转换为RGB格式,避免色差
    • 4、ORB特征提取及提取时间计算
    • 5、ORB特征匹配
  • 四、SIFT及ORB特征提取及匹配分析
    • 1、提取时间分析
    • 2、提取特征点分析

  在进行嵌入式开发的过程中,对于图像特征点的提取及匹配一直是一个重点问题,之前我们通过C++进行对应的orb特征点的匹配提取,本次博客,林君学长将带大家了解如何通过python实现SIFT和ORB两钟方法的特征点提取,理解sift 和orb在尺度缩放、旋转、仿射图上面的特征不变性,并进行两种提取方法的速度比较,一起来看吧!

一、python终端安装需要的图像处理库

 

1、打开终端,安装opencv-python

  1)、安装opencv-python库  

<code class="prism language-bash has-numbering">pip <span class="token function">install</span> --user opencv-python<span class="token operator">==</span>3.4.2.16
</code>

 
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索 2)、安装opencv-contrib-python库  

<code class="prism language-bash has-numbering">pip <span class="token function">install</span> --user opencv-contrib-python<span class="token operator">==</span>3.4.2.16
</code>

 
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索   由于林君学长已经下载,上面出现的界面是下载好了的  

2、如果之前安装其他版本操作步骤

  由于sift的特征提取及匹配需要用的opencv-python版本需要的是低版本,而之前我们默认安装的是最新版本的opencv-python,所以我们需要卸载后重新安装,具体步骤如下所示:   1)、卸载opencv-contrib-python及opencv-python库  

<code class="prism language-bash has-numbering">pip uninstall opencv-contrib-python
pip uninstall opencv-python
</code>

  2)、重新安装opencv-contrib-python及opencv-python库  

<code class="prism language-bash has-numbering">pip <span class="token function">install</span> --user opencv-python<span class="token operator">==</span>3.4.2.16
pip <span class="token function">install</span> --user opencv-contrib-python<span class="token operator">==</span>3.4.2.16
</code>

 

3、准备图片数据

  1)、需要选择一张原图  
在这里插入图片描述   2)、对原图进行旋转  
在这里插入图片描述   3)、对原图进行缩略  
在这里插入图片描述   4)、原图变换为仿射图   对于仿射图的变换,林君学长后面会给出代码,这里给出仿射图的结果展示:  
在这里插入图片描述   以上的图片制作比较简单,可以用windows自带的画图板进行修改得到,这里就不讲解制作过程了!   在一切准备妥当之后,我们就可以开始特征点提取和匹配了哦!

二、SIFT特征提取及匹配

 

1、导入需要的python库

 

<code class="prism language-python has-numbering"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np
<span class="token keyword">import</span> cv2
<span class="token keyword">from</span> matplotlib <span class="token keyword">import</span> pyplot <span class="token keyword">as</span> plt
<span class="token keyword">import</span> time
<span class="token operator">%</span>matplotlib inline
</code>

  以上导入库的功能相对简单,林君学长这里就不给出具体介绍了  

2、导入图片

  读取原图、旋转图、缩略图  

<code class="prism language-python has-numbering">img1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/1.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取原图片</span>
img2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/2.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取旋转图片</span>
img3 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/3.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取缩放图片</span>
</code>

 

3、构建仿射图

 

<code class="prism language-python has-numbering"><span class="token comment">#构建放射图</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/1.jpg'</span><span class="token punctuation">)</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
rows<span class="token punctuation">,</span> cols<span class="token punctuation">,</span> ch <span class="token operator">=</span> img<span class="token punctuation">.</span>shape
pts1 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> rows <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
pts2 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.9</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.1</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
M <span class="token operator">=</span> cv2<span class="token punctuation">.</span>getAffineTransform<span class="token punctuation">(</span>pts1<span class="token punctuation">,</span> pts2<span class="token punctuation">)</span>
dst <span class="token operator">=</span> cv2<span class="token punctuation">.</span>warpAffine<span class="token punctuation">(</span>img<span class="token punctuation">,</span> M<span class="token punctuation">,</span> <span class="token punctuation">(</span>cols<span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code>

  放射图主要通过代码生成,以上dst就为仿射图,显示可通过传递dst参数便可以显示  

4、将img转换为RGB格式,避免色差

 

<code class="prism language-python has-numbering"><span class="token comment">#使用cv2.imread()接口读图像,读进来的是BGR格式以及[0~255]。所以要将img转换为RGB格式,不然后面显示会有色差</span>
img1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img1<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span> <span class="token comment">#灰度处理图像</span>
img2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img2<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img2<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
img3 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img3<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray3 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img3<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
img4 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>dst<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray4 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>dst<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
</code>

 

5、SIFT特征提取及提取时间计算

  1)、SIFT对四张图片的特征提取及提取时间计算  

<code class="prism language-python has-numbering"><span class="token comment">#sift 特征提取</span>
a<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
sift1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>xfeatures2d<span class="token punctuation">.</span>SIFT_create<span class="token punctuation">(</span><span class="token punctuation">)</span>
kp1<span class="token punctuation">,</span> des1 <span class="token operator">=</span> sift1<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img1<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp2<span class="token punctuation">,</span> des2 <span class="token operator">=</span> sift1<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img2<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp3<span class="token punctuation">,</span> des3 <span class="token operator">=</span> sift1<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img3<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp4<span class="token punctuation">,</span> des4 <span class="token operator">=</span> sift1<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img4<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
b<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"sift提取时间为:"</span><span class="token punctuation">,</span>b<span class="token operator">-</span>a<span class="token punctuation">)</span>
</code>

 
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索   2)、SIFT对于原图片的特征提取展示  

<code class="prism language-python has-numbering">img11 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img1<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img11<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:yuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   3)、SIFT对于旋转图片的特征提取展示  

<code class="prism language-python has-numbering">img12 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img2<span class="token punctuation">,</span>kp2<span class="token punctuation">,</span>img2<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img12<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:xuanzhuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   4)、SIFT对于缩略图的特征提取展示  

<code class="prism language-python has-numbering">img13 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img3<span class="token punctuation">,</span>kp3<span class="token punctuation">,</span>img3<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img13<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:suolue'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   5)、SIFT对于仿射图的特征提取展示  

<code class="prism language-python has-numbering">img14 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img4<span class="token punctuation">,</span>kp4<span class="token punctuation">,</span>img4<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img14<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:fangshe'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述

6、SIFT特征匹配

  1)、FLANN 参数设计  

<code class="prism language-python has-numbering"><span class="token comment"># FLANN 参数设计</span>
FLANN_INDEX_KDTREE <span class="token operator">=</span> <span class="token number">0</span>
index_params <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>algorithm <span class="token operator">=</span> FLANN_INDEX_KDTREE<span class="token punctuation">,</span> trees <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">)</span>
search_params <span class="token operator">=</span> <span class="token builtin">dict</span><span class="token punctuation">(</span>checks<span class="token operator">=</span><span class="token number">50</span><span class="token punctuation">)</span>
flann <span class="token operator">=</span> cv2<span class="token punctuation">.</span>FlannBasedMatcher<span class="token punctuation">(</span>index_params<span class="token punctuation">,</span>search_params<span class="token punctuation">)</span>
</code>

  2)、SIFT对于原图片与原图片的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与原图的匹配连线效果图展示</span>
matches <span class="token operator">=</span> flann<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span>des1<span class="token punctuation">,</span>k<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
matchesMask <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
good <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> m<span class="token punctuation">,</span>n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">if</span> m<span class="token punctuation">.</span>distance <span class="token operator"><</span> <span class="token number">0.7</span><span class="token operator">*</span>n<span class="token punctuation">.</span>distance<span class="token punctuation">:</span>
        good<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>m<span class="token punctuation">]</span><span class="token punctuation">)</span>
img41 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatchesKnn<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>good<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">,</span>flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img41<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:yuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   3)、SIFT对于原图片与旋转图片的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与旋转图的匹配连线效果图展示</span>
matches <span class="token operator">=</span> flann<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span>des2<span class="token punctuation">,</span>k<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
matchesMask <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
good <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> m<span class="token punctuation">,</span>n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">if</span> m<span class="token punctuation">.</span>distance <span class="token operator"><</span> <span class="token number">0.7</span><span class="token operator">*</span>n<span class="token punctuation">.</span>distance<span class="token punctuation">:</span>
        good<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>m<span class="token punctuation">]</span><span class="token punctuation">)</span>
img42 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatchesKnn<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img2<span class="token punctuation">,</span>kp2<span class="token punctuation">,</span>good<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">,</span>flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img42<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:xuanzhuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   4)、SIFT对于原图片与缩略图的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与缩略图的匹配连线效果图展示</span>
matches <span class="token operator">=</span> flann<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span>des3<span class="token punctuation">,</span>k<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
matchesMask <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
good <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> m<span class="token punctuation">,</span>n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">if</span> m<span class="token punctuation">.</span>distance <span class="token operator"><</span> <span class="token number">0.7</span><span class="token operator">*</span>n<span class="token punctuation">.</span>distance<span class="token punctuation">:</span>
        good<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>m<span class="token punctuation">]</span><span class="token punctuation">)</span>
img43 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatchesKnn<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img3<span class="token punctuation">,</span>kp3<span class="token punctuation">,</span>good<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">,</span>flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img43<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:suolue'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   5)、SIFT对于原图片与仿射图的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与放射图的匹配结果显示</span>
matches <span class="token operator">=</span> flann<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span>des4<span class="token punctuation">,</span>k<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
matchesMask <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>matches<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
good <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> m<span class="token punctuation">,</span>n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">if</span> m<span class="token punctuation">.</span>distance <span class="token operator"><</span> <span class="token number">0.7</span><span class="token operator">*</span>n<span class="token punctuation">.</span>distance<span class="token punctuation">:</span>
        good<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>m<span class="token punctuation">]</span><span class="token punctuation">)</span>
img44 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatchesKnn<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img4<span class="token punctuation">,</span>kp4<span class="token punctuation">,</span>good<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">,</span>flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img44<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:fangshe'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述 以上就是SIFT的体征提取及匹配啦,可以看到,SIFT的特征点提取的还是非常多的,接下来我们看那一下ORB特征点的提取及匹配并做一定的对比

三、ORB特征提取及匹配

 

1、导入图片

 

<code class="prism language-python has-numbering">img10 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/1.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取原图片</span>
img20 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/2.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取旋转图片</span>
img30 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/3.jpg'</span><span class="token punctuation">)</span><span class="token comment">#读取缩放图片</span>
</code>

 

2、构建仿射图

  通过原图片构建缩略图  

<code class="prism language-python has-numbering"><span class="token comment">#构建放射图</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">'D:/image/1.jpg'</span><span class="token punctuation">)</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
rows<span class="token punctuation">,</span> cols<span class="token punctuation">,</span> ch <span class="token operator">=</span> img<span class="token punctuation">.</span>shape
pts1 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> rows <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
pts2 <span class="token operator">=</span> np<span class="token punctuation">.</span>float32<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.9</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>cols <span class="token operator">*</span> <span class="token number">0.1</span><span class="token punctuation">,</span> rows <span class="token operator">*</span> <span class="token number">0.9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
M <span class="token operator">=</span> cv2<span class="token punctuation">.</span>getAffineTransform<span class="token punctuation">(</span>pts1<span class="token punctuation">,</span> pts2<span class="token punctuation">)</span>
dst <span class="token operator">=</span> cv2<span class="token punctuation">.</span>warpAffine<span class="token punctuation">(</span>img<span class="token punctuation">,</span> M<span class="token punctuation">,</span> <span class="token punctuation">(</span>cols<span class="token punctuation">,</span> rows<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code>

  其中dst即为我们需要的仿射图  

3、将img转换为RGB格式,避免色差

 

<code class="prism language-python has-numbering"><span class="token comment">#使用cv2.imread()接口读图像,读进来的是BGR格式以及[0~255]。所以要将img转换为RGB格式,不然后面显示会有色差</span>
img1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img10<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray1 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img10<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span> <span class="token comment">#灰度处理图像</span>
img2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img20<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img20<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
img3 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img30<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray3 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>img30<span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
img4 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>dst<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2RGB<span class="token punctuation">)</span>
gray4 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>cvtColor<span class="token punctuation">(</span>dst<span class="token punctuation">,</span>cv2<span class="token punctuation">.</span>COLOR_BGR2GRAY<span class="token punctuation">)</span>
</code>

 

4、ORB特征提取及提取时间计算

  1)、ORB特征提取及提取时间计算  

<code class="prism language-python has-numbering"><span class="token comment">#ORB 特征提取</span>
a<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
sift2 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>ORB_create<span class="token punctuation">(</span><span class="token punctuation">)</span>
kp1<span class="token punctuation">,</span> des1 <span class="token operator">=</span> sift2<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img1<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp2<span class="token punctuation">,</span> des2 <span class="token operator">=</span> sift2<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img2<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp3<span class="token punctuation">,</span> des3 <span class="token operator">=</span> sift2<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img3<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
kp4<span class="token punctuation">,</span> des4 <span class="token operator">=</span> sift2<span class="token punctuation">.</span>detectAndCompute<span class="token punctuation">(</span>img4<span class="token punctuation">,</span><span class="token boolean">None</span><span class="token punctuation">)</span>
b<span class="token operator">=</span>time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"ORB提取时间为:"</span><span class="token punctuation">,</span>b<span class="token operator">-</span>a<span class="token punctuation">)</span>
</code>

 
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索   2)、ORB对于原图片的特征提取  

<code class="prism language-python has-numbering">img11 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img1<span class="token punctuation">,</span>kp1<span class="token punctuation">,</span>img1<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img11<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:yuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   3)、ORB对于旋转图片的特征提取  

<code class="prism language-python has-numbering">img12 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img2<span class="token punctuation">,</span>kp2<span class="token punctuation">,</span>img2<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img12<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:xuanzhuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   4)、ORB对于缩略图的特征提取  

<code class="prism language-python has-numbering">img13 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img3<span class="token punctuation">,</span>kp3<span class="token punctuation">,</span>img3<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img13<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:suolue'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   5)、ORB对于仿射图的特征提取  

<code class="prism language-python has-numbering">img14 <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawKeypoints<span class="token punctuation">(</span>img4<span class="token punctuation">,</span>kp4<span class="token punctuation">,</span>img4<span class="token punctuation">,</span>color<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img14<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:fangshe'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述

5、ORB特征匹配

  1)、ORB对于原图片与原图片的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与原图的匹配连线效果图展示</span>
bf <span class="token operator">=</span> cv2<span class="token punctuation">.</span>BFMatcher<span class="token punctuation">(</span>cv2<span class="token punctuation">.</span>NORM_HAMMING<span class="token punctuation">,</span> crossCheck <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment"># orb的normType应该使用NORM_HAMMING</span>
matches <span class="token operator">=</span> bf<span class="token punctuation">.</span>match<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des1<span class="token punctuation">)</span>
matches <span class="token operator">=</span> <span class="token builtin">sorted</span><span class="token punctuation">(</span>matches<span class="token punctuation">,</span> key<span class="token operator">=</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>distance<span class="token punctuation">)</span>
knnMatches <span class="token operator">=</span> bf<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des1<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># drawMatchesKnn</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">for</span> n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">!=</span> n <span class="token operator">and</span> m<span class="token punctuation">.</span>distance <span class="token operator">>=</span> n<span class="token punctuation">.</span>distance<span class="token operator">*</span><span class="token number">0.75</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            matches<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>m<span class="token punctuation">)</span>
            <span class="token keyword">break</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatches<span class="token punctuation">(</span>img1<span class="token punctuation">,</span> kp1<span class="token punctuation">,</span> img1<span class="token punctuation">,</span> kp1<span class="token punctuation">,</span> matches<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">,</span> img1<span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:yuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   2)、ORB对于原图片与旋转图片的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与旋转图的匹配连线效果图展示</span>
bf <span class="token operator">=</span> cv2<span class="token punctuation">.</span>BFMatcher<span class="token punctuation">(</span>cv2<span class="token punctuation">.</span>NORM_HAMMING<span class="token punctuation">,</span> crossCheck <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment"># orb的normType应该使用NORM_HAMMING</span>
matches <span class="token operator">=</span> bf<span class="token punctuation">.</span>match<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des2<span class="token punctuation">)</span>
matches <span class="token operator">=</span> <span class="token builtin">sorted</span><span class="token punctuation">(</span>matches<span class="token punctuation">,</span> key<span class="token operator">=</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>distance<span class="token punctuation">)</span>
knnMatches <span class="token operator">=</span> bf<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des2<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># drawMatchesKnn</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">for</span> n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">!=</span> n <span class="token operator">and</span> m<span class="token punctuation">.</span>distance <span class="token operator">>=</span> n<span class="token punctuation">.</span>distance<span class="token operator">*</span><span class="token number">0.75</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            matches<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>m<span class="token punctuation">)</span>
            <span class="token keyword">break</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatches<span class="token punctuation">(</span>img1<span class="token punctuation">,</span> kp1<span class="token punctuation">,</span> img2<span class="token punctuation">,</span> kp2<span class="token punctuation">,</span> matches<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">,</span> img2<span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:xuanzhuan'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   3)、ORB对于原图片与缩略图的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与缩略图的匹配连线效果图展示</span>
bf <span class="token operator">=</span> cv2<span class="token punctuation">.</span>BFMatcher<span class="token punctuation">(</span>cv2<span class="token punctuation">.</span>NORM_HAMMING<span class="token punctuation">,</span> crossCheck <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment"># orb的normType应该使用NORM_HAMMING</span>
matches <span class="token operator">=</span> bf<span class="token punctuation">.</span>match<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des3<span class="token punctuation">)</span>
matches <span class="token operator">=</span> <span class="token builtin">sorted</span><span class="token punctuation">(</span>matches<span class="token punctuation">,</span> key<span class="token operator">=</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>distance<span class="token punctuation">)</span>
knnMatches <span class="token operator">=</span> bf<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des3<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># drawMatchesKnn</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">for</span> n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">!=</span> n <span class="token operator">and</span> m<span class="token punctuation">.</span>distance <span class="token operator">>=</span> n<span class="token punctuation">.</span>distance<span class="token operator">*</span><span class="token number">0.75</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            matches<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>m<span class="token punctuation">)</span>
            <span class="token keyword">break</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatches<span class="token punctuation">(</span>img1<span class="token punctuation">,</span> kp1<span class="token punctuation">,</span> img3<span class="token punctuation">,</span> kp3<span class="token punctuation">,</span> matches<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">,</span> img3<span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:suolue'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述   4)、ORB对于原图片与射图的特征匹配  

<code class="prism language-python has-numbering"><span class="token comment">#原图与仿射图的匹配连线效果图展示</span>
bf <span class="token operator">=</span> cv2<span class="token punctuation">.</span>BFMatcher<span class="token punctuation">(</span>cv2<span class="token punctuation">.</span>NORM_HAMMING<span class="token punctuation">,</span> crossCheck <span class="token operator">=</span> <span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token comment"># orb的normType应该使用NORM_HAMMING</span>
matches <span class="token operator">=</span> bf<span class="token punctuation">.</span>match<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des4<span class="token punctuation">)</span>
matches <span class="token operator">=</span> <span class="token builtin">sorted</span><span class="token punctuation">(</span>matches<span class="token punctuation">,</span> key<span class="token operator">=</span><span class="token keyword">lambda</span> x<span class="token punctuation">:</span> x<span class="token punctuation">.</span>distance<span class="token punctuation">)</span>
knnMatches <span class="token operator">=</span> bf<span class="token punctuation">.</span>knnMatch<span class="token punctuation">(</span>des1<span class="token punctuation">,</span> des4<span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment"># drawMatchesKnn</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
    <span class="token keyword">for</span> n <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>m <span class="token operator">!=</span> n <span class="token operator">and</span> m<span class="token punctuation">.</span>distance <span class="token operator">>=</span> n<span class="token punctuation">.</span>distance<span class="token operator">*</span><span class="token number">0.75</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            matches<span class="token punctuation">.</span>remove<span class="token punctuation">(</span>m<span class="token punctuation">)</span>
            <span class="token keyword">break</span>
img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>drawMatches<span class="token punctuation">(</span>img1<span class="token punctuation">,</span> kp1<span class="token punctuation">,</span> img4<span class="token punctuation">,</span> kp4<span class="token punctuation">,</span> matches<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token number">50</span><span class="token punctuation">]</span><span class="token punctuation">,</span> img4<span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>figure<span class="token punctuation">(</span>num<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">,</span>figsize<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span><span class="token number">16</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span>img<span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'Left:yuan---Right:fangshe'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>axis<span class="token punctuation">(</span><span class="token string">'off'</span><span class="token punctuation">)</span>
plt<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code>

 
在这里插入图片描述

四、SIFT及ORB特征提取及匹配分析

 

1、提取时间分析

  两种方法的提取时间如下所示:   1)、SIFT提取时间
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索   2)、ORB提取时间
视觉SLAM学习【6】-----基于python的SIFT和ORB特征点提取及匹配的对比探索   从提取时间上来看ORB的提取时间很短,大约是SIFT提取方法的十分之一,这也是ORB特征提取方法的优越性,同时,ORB在尺度缩放、旋转、仿射上的特征点不变,具有不变性

2、提取特征点分析

  1)、SIFT提取点提取  
在这里插入图片描述   2)、ORB提取点提取  
在这里插入图片描述   从特征点提取特征来看,SIFT提取的特征点非常多,趋近于实物,准确性非常高,这也是SIFT提取方法的优越性,同时,SIFT在尺度缩放、旋转、仿射上的特征点不变,具有不变性!   以上就是本次博客的全部内容啦,希望小伙伴们对本次博客的阅读可以帮助大家理解python中,如何进行sfit及orb特征点提取及匹配,了解在尺度缩放、旋转、仿射上的特征点不变原则!   遇到问题的小伙伴记得评论区留言,林君学长看到会为大家解答的,这个学长不太冷! 陈一月的又一天编程岁月^ _ ^


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明视觉SLAM学习【6】—–基于python的SIFT和ORB特征点提取及匹配的对比探索
喜欢 (0)

您必须 登录 才能发表评论!

加载中……