SIFT简介
成像匹配的核心问题是将同一目标在不同时间、不同分辨率、不同光照、不同位姿情况下所成的像相对应。传统的匹配算法往往是直接提取角点或边缘,对环境的适应能力较差,急需提出一种鲁棒性强、能够适应不同光照、不同位姿等情况下能够有效识别目标的方法。1999年British Columbia大学大卫.劳伊( David G.Lowe)教授总结了现有的基于不变量技术的特征检测方法,并正式提出了一种基于尺度空间的、对图像缩放、旋转甚至仿射变换保持不变性的图像局部特征描述算子-SIFT(尺度不变特征变换),这种算法在2004年被加以完善。
一、算法思想
将一幅图像映射(变换)为一个局部特征向量集;特征向量具有平移、缩放、旋转不变性,同时对光照变化、仿射及投影变换也有一定不变性。
算法实现步骤简述:
SIFT算法的实质可以归为在不同尺度空间上查找特征点(关键点)的问题。
SIFT算法实现物体识别主要有三大工序:
1、提取关键点;
2、对关键点附加详细的信息(局部特征)也就是所谓的描述器;
3、通过两方特征点(附带上特征向量的关键点)的两两比较找出相互匹配的若干对特征点,也就建立了景物间的对应关系。
二、SIFT算法实现步骤
1.构建尺度空间
2.关键点检测
3.消除错配点
4.关键点描述
5.关键点匹配
三、SIFT算法简单实现代码
旋转缩放图像
def rotsImg(image, angle, scale):
"""旋转缩放图像"""
height = image.shape[0]
width = image.shape[1]
matRot = cv2.getRotationMatrix2D((height*0.5, width*0.5), angle, scale)
# 中心,角度,缩放系数
I_rot = cv2.warpAffine(image, matRot, (width, height))
return I_rot
实现简单的SIFT检测
def SIFT_CV(image):
"""实现简单的SIFT检测"""
sift = cv2.xfeatures2d.SIFT_create()
imageRot = rotsImg(image, -45, 0.8) #旋转缩放
imageGray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
imageRotGray = cv2.cvtColor(imageRot, cv2.COLOR_RGB2GRAY)
kp1, des1 = sift.detectAndCompute(imageGray, None)
kp2, des2 = sift.detectAndCompute(imageRotGray, None)
imageSIFT = cv2.drawKeypoints(image,kp1,image,color=(255,0,255)) #画出特征点,并显示为红色圆圈
imageRotSIFT = cv2.drawKeypoints(imageRot,kp2,imageRot,color=(255,0,255)) #画出特征点,并显示为红色圆圈
hmerge = np.hstack((imageSIFT, imageRotSIFT))#水平拼接
return hmerge, des1, des2, kp1, kp2, imageRot
使用KNN算法进行匹配,如果dis为false,直接匹配,默认为True
def BFmatch(image, des1, des2, kp1, kp2, imageRot, dis=True):
"""使用KNN算法进行匹配,如果dis为false,直接匹配,默认为True"""
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
if(dis == True):
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
imgRes = cv2.drawMatchesKnn(image, kp1, imageRot, kp2, good, None,flags=2)
else:
imgRes = cv2.drawMatchesKnn(image, kp1, imageRot, kp2, matches, None,flags=2)
return imgRes
if __name__ == '__main__':
image = cv2.imread("./image/test.jpg")
imageRe = cv2.resize(image, (300,400))
imageSift, des1, des2, kp1, kp2, imageRot = SIFT_CV(imageRe)
imageRes = BFmatch(imageRe, des1, des2, kp1, kp2, imageRot)
cv2.imshow("imageSift", imageRes)
cv2.waitKey(20000)
完整代码
import cv2
import numpy as np
import matplotlib.pyplot as plt
def rotsImg(image, angle, scale):
"""旋转缩放图像"""
height = image.shape[0]
width = image.shape[1]
matRot = cv2.getRotationMatrix2D((height*0.5, width*0.5), angle, scale)
# 中心,角度,缩放系数
I_rot = cv2.warpAffine(image, matRot, (width, height))
return I_rot
def SIFT_CV(image):
"""实现简单的SIFT检测"""
sift = cv2.xfeatures2d.SIFT_create()
imageRot = rotsImg(image, -45, 0.8) #旋转缩放
imageGray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
imageRotGray = cv2.cvtColor(imageRot, cv2.COLOR_RGB2GRAY)
kp1, des1 = sift.detectAndCompute(imageGray, None)
kp2, des2 = sift.detectAndCompute(imageRotGray, None)
imageSIFT = cv2.drawKeypoints(image,kp1,image,color=(255,0,255)) #画出特征点,并显示为红色圆圈
imageRotSIFT = cv2.drawKeypoints(imageRot,kp2,imageRot,color=(255,0,255)) #画出特征点,并显示为红色圆圈
hmerge = np.hstack((imageSIFT, imageRotSIFT))#水平拼接
return hmerge, des1, des2, kp1, kp2, imageRot
def BFmatch(image, des1, des2, kp1, kp2, imageRot, dis=True):
"""使用KNN算法进行匹配,如果dis为false,直接匹配,默认为True"""
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
if(dis == True):
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append([m])
imgRes = cv2.drawMatchesKnn(image, kp1, imageRot, kp2, good, None,flags=2)
else:
imgRes = cv2.drawMatchesKnn(image, kp1, imageRot, kp2, matches, None,flags=2)
return imgRes
if __name__ == '__main__':
image = cv2.imread("./image/test.jpg")
imageRe = cv2.resize(image, (300,400))
imageSift, des1, des2, kp1, kp2, imageRot = SIFT_CV(imageRe)
imageRes = BFmatch(imageRe, des1, des2, kp1, kp2, imageRot)
cv2.imshow("imageSift", imageRes)
cv2.waitKey(20000)