import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread('1.jpg',0)
# 初始化ORB检测器
orb = cv.ORB_create()
# FLANN 参数设计
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv.FlannBasedMatcher(index_params,search_params)
SIFT特征
sift = cv2.xfeatures2d.SIFT_create()
首先对图片进行灰度处理
img1 = cv.imread('1.jpg')
#使用cv2.imread()接口读图像,读进来的是BGR格式以及[0~255]。所以要将img转换为RGB格式,不然后面显示会有色差
img1 = cv.cvtColor(img1,cv.COLOR_BGR2RGB)
gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = orb.detectAndCompute(img1,None)#des是描述符
print (des1.shape) #描述符数组维度
print(len(kp1)) #关键点个数
img2 = cv.imread('2.jpg')
#使用cv2.imread()接口读图像,读进来的是BGR格式以及[0~255]。所以要将img转换为RGB格式,不然后面显示会有色差
img2 = cv.cvtColor(img2,cv.COLOR_BGR2RGB)
gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
kp2, des2 = orb.detectAndCompute(img2,None)
print (des2.shape) #描述符数组维度
print(len(kp2)) #关键点个数
可以显示程序运行时间
import time
start = time.time()
画出两张图的特征点,并水平拼接显示
img3 = cv.drawKeypoints(img1,kp1,img1,color=(0,255,0))
img4 = cv.drawKeypoints(img2,kp2,img2,color=(0,255,0))
hmerge = np.hstack((img3, img4)) #水平拼接
plt.figure(num=1,figsize=(16,16))
plt.imshow(hmerge)
plt.title('keypoint')
plt.axis('off')
plt.show()
kp1, des1 = sift.detectAndCompute(img1,None)#des是描述符
kp2, des2 = sift.detectAndCompute(img2,None)
des1=np.float32(des1)
des2=np.float32(des2)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append([m])
img5 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
plt.figure(num=1,figsize=(16,16))
plt.imshow(img5)
plt.title('ORB_rotation')
plt.axis('off')
plt.show()
#打印出程序运行的时间
end = time.time()
print (end-start)
import time
start = time.time()
读取图片并对图片进行灰度处理。
img1 = cv.imread("1.jpg")
img2 = cv.imread("3.jpg")
img1 = cv.cvtColor(img1,cv.COLOR_BGR2RGB)
img2 = cv.cvtColor(img2,cv.COLOR_BGR2RGB)
gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
kp1, des1 = orb.detectAndCompute(img1,None)
print (des1.shape) #描述符数组维度
print(len(kp1)) #关键点个数
gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
kp2, des2 = orb.detectAndCompute(img2,None)
print (des1.shape) #描述符数组维度
print(len(kp2)) #关键点个数
显示特征点
img3 = cv.drawKeypoints(img1,kp1,img1,color=(0,255,0))
img4 = cv.drawKeypoints(img2,kp2,img2,color=(0,255,0))
plt.figure(num=1,figsize=(12,12))
plt.imshow(img3)
plt.title('keypoint_img3')
plt.axis('off')
plt.show()
plt.figure(num=1,figsize=(12,12))
plt.imshow(img4)
plt.title('keypoint_img4')
plt.axis('off')
plt.show()
kp1, des1 = sift.detectAndCompute(img1,None)#des是描述符
kp2, des2 = sift.detectAndCompute(img2,None)
des1=np.float32(des1)
des2=np.float32(des2)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append([m])
img5 = cv.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=2)
plt.figure(num=1,figsize=(16,16))
plt.imshow(img5)
plt.title('ORB_size')
plt.axis('off')
plt.show()
#打印程序运行时间
end = time.time()
print (end-start)
import time
start = time.time()
改变图片亮度并与原图片水平拼接显示
img1 = cv.imread("1.jpg")
img1 = cv.cvtColor(img1,cv.COLOR_BGR2RGB)
res = np.uint8(np.clip((1.5 * img1 + 10), 0, 255))
print(img1.shape)
print(res.shape)
tmp = np.hstack((img1, res)) # 两张图片横向合并(便于对比显示)
plt.figure(num=1,figsize=(16,16))
plt.imshow(tmp)
plt.title('original')
plt.axis('off')
plt.show()
gray1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = orb.detectAndCompute(img1,None)#des是描述符
print (des1.shape) #描述符数组维度
print(len(kp1)) #关键点个数
gray2 = cv.cvtColor(res, cv.COLOR_BGR2GRAY) #灰度处理图像
kp2, des2 = orb.detectAndCompute(res,None)#des是描述符
print (des2.shape) #描述符数组维度
print(len(kp2)) #关键点个数
img3 = cv.drawKeypoints(img1,kp1,img1,color=(0,255,0))
img4 = cv.drawKeypoints(res,kp2,res,color=(0,255,0))
hmerge = np.hstack((img3, img4)) #水平拼接
plt.figure(num=1,figsize=(16,16))
plt.imshow(hmerge)
plt.title('keypoint')
plt.axis('off')
plt.show()
kp1, des1 = sift.detectAndCompute(img1,None)#des是描述符
kp2, des2 = sift.detectAndCompute(img2,None)
des1=np.float32(des1)
des2=np.float32(des2)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append([m])
img5 = cv.drawMatchesKnn(img1,kp1,res,kp2,good,None,flags=2)
plt.figure(num=1,figsize=(16,16))
plt.imshow(img5)
plt.title('ORB_brightness')
plt.axis('off')
plt.show()
#打印程序运行时间
end = time.time()
print (end-start)
import time
start = time.time()
读取图片并对图片进行处理
img = cv.imread('1.jpg')
img = cv.cvtColor(img,cv.COLOR_BGR2RGB)
rows, cols, ch = img.shape
pts1 = np.float32([[0, 0], [cols - 1, 0], [0, rows - 1]])
pts2 = np.float32([[cols * 0.2, rows * 0.1], [cols * 0.9, rows * 0.2], [cols * 0.1, rows * 0.9]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
plt.figure(num=1,figsize=(16,16))
plt.imshow(dst)
plt.title('affine_img')
plt.axis('off')
plt.show()
对原图和处理过后的图片进行灰度处理
gray1 = cv.cvtColor(img, cv.COLOR_BGR2GRAY) #灰度处理图像
kp1, des1 = orb.detectAndCompute(img,None)#des是描述符
print (des1.shape) #描述符数组维度
print(len(kp1)) #关键点个数
gray2 = cv.cvtColor(dst, cv.COLOR_BGR2GRAY) #灰度处理图像
kp2, des2 = orb.detectAndCompute(dst,None)#des是描述符
print (des2.shape) #描述符数组维度
print(len(kp2)) #关键点个数
显示两张图片的特征点
img3 = cv.drawKeypoints(img,kp1,img,color=(0,255,0))
img4 = cv.drawKeypoints(dst,kp2,dst,color=(0,255,0))
hmerge = np.hstack((img3, img4)) #水平拼接
plt.figure(num=1,figsize=(16,16))
plt.imshow(hmerge)
plt.title('keypoint')
plt.axis('off')
plt.show()
kp1, des1 = sift.detectAndCompute(img1,None)#des是描述符
kp2, des2 = sift.detectAndCompute(img2,None)
des1=np.float32(des1)
des2=np.float32(des2)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append([m])
img5 = cv.drawMatchesKnn(img,kp1,dst,kp2,good,None,flags=2)
plt.figure(num=1,figsize=(16,16))
plt.imshow(img5)
plt.title('ORB_affine')
plt.axis('off')
plt.show()
#显示程序运行时间
end = time.time()
print (end-start)
首先要在 anaconda prompt界面使用命令下载opencv-contrib-python
pip install opencv-contrib-python==3.4.2.16 --user
然后再打开jupyter,代码和ORB的代码很多都是相同的,我们需要改的地方就是
将ORB的
orb = cv.ORB_create()
改为
sift = cv2.xfeatures2d.SIFT_create()
并且,后面的程序中使用orb.detectAndComputer()函数这些的,全都将orb改为sift
这样也可以得到关于SIFT特征点的这几个特性的验证,ORB和SIFT就可以进行比较。
参考:https://blog.csdn.net/zdx1996/article/details/86583676.
https://stackoverflow.com/questions/37039224/attributeerror-module-object-has-no-attribute-xfeatures2d-python-opencv-2.
如有错误请指正!