在opencv3中使用ORB进行特征提取实验-Python版

ORB (Oriented FAST and Rotated BRIEF)
分为两部分:

  • 特征点提取 -由FAST(Features from Accelerated Segment Test)算法发展来的
  • 特征点描述 -根据BRIEF(Binary Robust IndependentElementary Features)特征描述算法改进的
    具体不详述,数学原理对我来说需要一段时间去理解消化,网上的相关的介绍也很多。这里先从实例开始,学习怎么用这个工具。

ORB特征提取实验

基于opencv3.2.0的ORB特征提取试验

import numpy as np
import cv2
img1 = cv2.imread("data/face1.jpg",0)#导入灰度图像
img2 = cv2.imread("data/face2.jpg",0)

def drawMatches(img1, kp1, img2, kp2, matches):
    rows1 = img1.shape[0]
    cols1 = img1.shape[1]
    rows2 = img2.shape[0]
    cols2 = img2.shape[1]

    out = np.zeros((max([rows1,rows2]),cols1 + cols2, 3),dtype = 'uint8')
    #拼接图像
    out[:rows1, :cols1] = np.dstack([img1, img1,img1])
    out[:rows2, cols1:] = np.dstack([img2, img2,img2])
    
    for mat in matches:
        img1_idx = mat.queryIdx
        img2_idx = mat.trainIdx
        
        (x1,y1) = kp1[img1_idx].pt
        (x2,y2) = kp2[img2_idx].pt
        #绘制匹配点
        cv2.circle(out, (int(x1),int(y1)),4,(255,255,0),1)
        cv2.circle(out,(int(x2)+cols1,int(y2)),4,(0,255,255),1)
        
        cv2.line(out,(int(x1),int(y1)),(int(x2)+cols1,int(y2)),(255,0,0),1)
    
    return out
    
detector = cv2.ORB_create()

kp1 = detector.detect(img1,None)
kp2 = detector.detect(img2,None)
kp1,des1 = detector.compute(img1,kp1)
kp2,des2 = detector.compute(img2,kp2)

bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck = True)
matches = bf.match(des1,des2)
img3 = drawMatches(img1,kp1,img2,kp2,matches[:50])
# img3 = cv2.drawKeypoints(img1,kp,None,color = (0,255,0),flags = 0)

cv2.imwrite("orbTest.jpg",img3)
cv2.imshow('orbTest',img3)
cv2.waitKey(0)

实验结果:

《在opencv3中使用ORB进行特征提取实验-Python版》 orbResult.jpg

然后也可以对比下AKAZE,只要将改成

detector = cv2.ORB_create() 

改成

detector = cv2.AKAZE_create()

效果图:

《在opencv3中使用ORB进行特征提取实验-Python版》 orbResult_akaze.jpg

可以看出,AKAZE配对正确的数量大于ORB。
关于AKAZE和orb的比较,可以参考文章:
http://www.epacis.net/ccis2016/papers/paper_121.pdf

这里复习了一下Python特性之切片,在拼接图片时用到了:
例如:
list[1:3],表示返回从位置1开始,包括位置2,但是停止在位置3的一个序列切片,因此返回一个含有两个项目的切片。
out[:rows1, :cols1] 行:从0开始,到rows1之前;列:从0开始,到cols1之前
out[:rows2, cols1:] 行:从0开始,到rows2之前;列:从cols1开始,到最后

补充说明:
在opencv2中, orb = cv2.ORB ()是对的;
在opencv3中这样会报错,改成orb = cv2.ORB_create()就没问题了。

    原文作者:晚晴风_
    原文地址: https://www.jianshu.com/p/5083f8d75439
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞