3D reconstruction 關鍵點檢測 特徵描述算法學習筆記

一. 傳統的關鍵點檢測和特徵描述算法有:

1. SIFT

import numpy as np
import cv2

imgname1 = './555.jpg'
imgname2 = './666.jpg'

sift = cv2.xfeatures2d.SIFT_create()

img1 = cv2.imread(imgname1)
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) #灰度處理圖像
kp1, des1 = sift.detectAndCompute(img1,None)   #des是描述子

img2 = cv2.imread(imgname2)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)#灰度處理圖像
kp2, des2 = sift.detectAndCompute(img2,None)  #des是描述子
# hmerge = np.hstack((gray1, gray2)) #水平拼接
# cv2.imshow("gray", hmerge) #拼接顯示爲gray
# cv2.waitKey(0)
img3 = cv2.drawKeypoints(img1,kp1,img1,color=(255,0,255)) #畫出特徵點,並顯示爲紅色圓圈
img4 = cv2.drawKeypoints(img2,kp2,img2,color=(255,0,255)) #畫出特徵點,並顯示爲紅色圓圈

cv2.imwrite('./555_sift.png', img3)
cv2.imwrite('./666_sift.png', img4)

# hmerge = np.hstack((img3, img4)) #水平拼接
# cv2.imshow("point", hmerge) #拼接顯示爲gray
# cv2.waitKey(0)
# BFMatcher解決匹配
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1,des2, k=2)

img5 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,flags=2)
cv2.imwrite('./555_666_siftmatch.png', img5)
cv2.imshow("BFmatch", img5)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. SURF

3. ORB

import cv2
import numpy as np
#from pylsd.lsd import lsd
import math

img1 = cv2.imread('./333.jpg',0)#導入灰度圖像
img2 = cv2.imread('./444.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)
# for p in kp1:
# 	x_ = p.pt[0]
# 	y_ = p.pt[1]
# 	print(x_, y_)
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,kp1,None,color = (0,255,0),flags = 0)
cv2.imwrite("333_444_orb.jpg",img3)
cv2.imshow('orbTest',img3)
cv2.waitKey(0)

4. AKAZE

二. 基於深度學習方法

1. 特徵提取

(1)Discriminative learning of deep convolutional feature point descriptors

(2)LIFT: Learned Invariant Feature Transform 2016

2. 特徵匹配

(1)MatchNet: “MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching”, CVPR 2015

paper: http://www.cs.unc.edu/~xufeng/cs/papers/cvpr15-matchnet.pdf

github:https://github.com/hanxf/matchnet

(2)UCN: Universal Correspondence Network NIPS 2016

(3)DGC-Net: “DGC-Net: Dense Geometric Correspondence Network”, CVPR 2019

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章