明确一下基础概念:
只有物体的位置(平移变换)和朝向(旋转变换)发生改变,而形状不变,得到的变换称为刚性变换。
角点没有明确的数学定义,严格说,角点的局部领域应该具有两个不同区域不同方向的边界,可以是某些属性上强度最大或者最小的孤立点,线段的终点,或者是曲线上局部曲率最大的点
角点检测是计算机视觉系统中用来提取图像特征的一种方法,又称为特征点检测.
广泛用于运动检测,图像匹配,视频跟踪,三维建模和目标识别等
CV_WRAP KeyPoint() : pt(0,0), size(0), angle(-1), response(0), octave(0), class_id(-1) {}
现分析各项属性
pt(x,y):关键点的点坐标;
size():该关键点邻域直径大小;
angle:角度,表示关键点的方向,值为[零,三百六十),负值表示不使用。
response:响应强度,网络上有如下解释:
1)”The response, by which the strongest keypoints have been selected.”
2)”Responseis indeed an indicator of “how good” (roughly speaking, in terms of corner-ness) a point is. The higher, the better. The strongest keypoints means the ones that are the best.“
octacv:从哪一层金字塔得到的此关键点。
class_id:当要对图片进行分类时,用class_id对每个关键点进行区分,默认为-1。
sift函数
kp,des = sift.detectAndCompute(image,None)
kp指关键点 des指关键点的特征描述
解决:AttributeError: module ‘cv2.cv2’ has no attribute ‘xfeatures2d’
问题
卸载原有opencv
pip uninstall opencv-python
pip uninstall opencv-contrib-python
安装新的
pip install opencv_python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16
下载完了记得 restart kernal
import numpy as np
import cv2
def sift_kp(image):
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #转化为灰度图
sift = cv2.xfeatures2d.SIFT_create() #使用sift特征提取法
# 将图片进行SURF计算,并找出角点keypoints,keypoints是检测关键点
# descriptor是描述符,这是图像一种表示方式,可以比较两个图像的关键点描述符,可作为特征匹配的一种方法。
kp,des = sift.detectAndCompute(image,None) #
kp_image = cv2.drawKeypoints(gray_image,kp,None)
return kp_image,kp,des
def get_good_match(des1,des2):
bf = cv2.BFMatcher() #暴力匹配
# knnMatch 函数参数k是返回符合匹配的个数,暴力匹配match只返回最佳匹配结果。
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches: #再进行了一次优化,选取更符合的点
if m.distance < 0.75 * n.distance:
good.append(m)
return good
def siftImageAlignment(img1,img2):
_,kp1,des1 = sift_kp(img1) #对图一进行sift
_,kp2,des2 = sift_kp(img2) #对图二进行sift
goodMatch = get_good_match(des1,des2) #得到优秀匹配
if len(goodMatch) > 4:
# 改变数组的表现形式,不改变数据内容,数据内容是每个关键点的坐标位置
ptsA= np.float32([kp1[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
#DMatch.queryIdx表示查询图像中描述符的索引
ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
#DMatch.trainIdx表示目标图像中描述符的索引。
ransacReprojThreshold = 4
H, status =cv2.findHomography(ptsA,ptsB,cv2.RANSAC,ransacReprojThreshold); #单映性变换
#其中H为求得的单应性矩阵矩阵
#status则返回一个列表来表征匹配成功的特征点。
#ptsA,ptsB为关键点
#cv2.RANSAC, ransacReprojThreshold这两个参数与RANSAC有关
imgOut = cv2.warpPerspective(img2, H, (img1.shape[1],img1.shape[0]),flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
# book = cv2.warpPerspective(im, h, dsize)
return imgOut,H,status
img1 = cv2.imread('D:/photo_match/131.jpg')
img2 = cv2.imread('D:/photo_match/132.jpg')
while img1.shape[0] > 1000 or img1.shape[1] >1000:
img1 = cv2.resize(img1,None, fx=0.5,fy=0.5,interpolation = cv2.INTER_AREA)
while img2.shape[0] > 1000 or img2.shape[1] >1000:
img2 = cv2.resize(img2,None, fx=0.5,fy=0.5,interpolation = cv2.INTER_AREA)
result,_,_ = siftImageAlignment(img1,img2)
allImg = np.concatenate((img1,img2,result),axis=1)
cv2.namedWindow('1',cv2.WINDOW_NORMAL)
cv2.namedWindow('2',cv2.WINDOW_NORMAL)
cv2.namedWindow('Result',cv2.WINDOW_NORMAL)
cv2.imshow('1',img1)
cv2.imshow('2',img2)
cv2.imshow('Result',result)
#cv2.imshow('Result',allImg)
if cv2.waitKey(2000) & 0xff == ord('q'):
cv2.destroyAllWindows()
cv2.waitKey(1)