BRIEF Features(二进制鲁棒独立的基本特征)
- 二进制鲁棒的独立基本特征
- 在很多情况下,在速度和识别率方面都优于其他快速描述子,如SURF和SIFT。
步骤:
- 使用高斯核平滑图像
- 转换为二进制特征向量
使用Opencv实现BRIEF
import numpyas npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("imgs/chapter9/indoor.jpg", 1); gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Initiate FAST detectorstar = cv2.xfeatures2d.StarDetector_create()kp = star.detect(gray,None) brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()keypoints, descriptors = brief.compute(gray, kp) for i in keypoints:x,y = int(i.pt[0]), int(i.pt[1])cv2.circle(img,(x,y), 5,(0, 125, 125),-1) plt.figure(figsize=(8, 8))plt.imshow(img[:,:,::-1]);plt.show()
BRISK Features(二进制鲁棒不变可扩展关键点特征)
(Binary RobustIndependent Elementary Features.)原文给的这个但BRISK全称应该是Binary Robust Invariant Scalable Keypoints
由三部分组成
- 采样模式:在描述子周围的位置采样
- 方向补偿:对关键点的方向和旋转进行某种机制的补偿。
- 抽样对:构建最终描述符时要比较哪些对。
import numpy as npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("imgs/chapter9/indoor.jpg", 1); gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) brisk = cv2.BRISK_create()keypoints, descriptors = brisk.detectAndCompute(gray, None) for i in keypoints:x,y = int(i.pt[0]), int(i.pt[1])cv2.circle(img,(x,y), 5,(0, 125, 125),-1) plt.figure(figsize=(8, 8))plt.imshow(img[:,:,::-1]);plt.show()
KAZE and Accelerated-KAZE features(KAZE是日语风的谐音)
使用OpenCV实现KAZE
import numpyas npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("imgs/chapter9/indoor.jpg", 1); gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) kaze = cv2.KAZE_create()keypoints, descriptors = kaze.detectAndCompute(gray, None) for i in keypoints:x,y = int(i.pt[0]), int(i.pt[1])cv2.circle(img,(x,y), 5,(0, 125, 125),-1) plt.figure(figsize=(8, 8))plt.imshow(img[:,:,::-1]);plt.show()
AKAZE Features
使用OpenCV实现AKAZE
import numpyas npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("imgs/chapter9/indoor.jpg", 1); gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) akaze = cv2.AKAZE_create()keypoints, descriptors = akaze.detectAndCompute(gray, None) for i in keypoints:x,y = int(i.pt[0]), int(i.pt[1])cv2.circle(img,(x,y), 5,(0, 125, 125),-1) plt.figure(figsize=(8, 8))plt.imshow(img[:,:,::-1]);plt.show()
ORB Features
面向快速鲁棒的BRIEF特征
使用OpenCV实现Orb
import numpyas npimport cv2from matplotlib import pyplot as pltimg = cv2.imread("imgs/chapter9/indoor.jpg", 1) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) orb = cv2.ORB_create(500)keypoints, descriptors = orb.detectAndCompute(gray, None) for i in keypoints:x,y = int(i.pt[0]), int(i.pt[1])cv2.circle(img,(x,y), 5,(0, 125, 125),-1) plt.figure(figsize=(8, 8))plt.imshow(img[:,:,::-1])plt.show()
特征匹配
- 用于识别多幅图像中的相似特征。
- 用于目标检测。
方法
1.暴力算法
将图像1中的每个特征与图像2中的每个特征逐个进行匹配
2.基于FLANN(快速最近邻开源库)的匹配
- 快速最近邻开源库
- 它包含一组算法,这些算法针对大型数据集中的快速最近邻搜索和高维特征进行了优化。
使用Opencv实现特征匹配
'''
Using Brute-Force matching
'''
import numpy as npimport cv2from matplotlib import pyplot as plt orb = cv2.ORB_create(500) img1 = cv2.imread("imgs/chapter9/indoor_lamp.jpg", 1);img1 = cv2.resize(img1, (256, 256));gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY); img2 = cv2.imread("imgs/chapter9/indoor.jpg", 1);img2 = cv2.resize(img2, (640, 480));gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY); # find the keypoints anddescriptors with SIFTkp1, des1 = orb.detectAndCompute(img1,None)kp2, des2 = orb.detectAndCompute(img2,None) # BFMatcher with default paramsbf = cv2.BFMatcher()matches = bf.knnMatch(des1,des2,k=2) # Apply ratio testgood = []for m,n in matches:if m.distance< 0.75*n.distance:good.append([m]) # cv.drawMatchesKnn expects list oflists as matches.img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,good,None,flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) plt.figure(figsize=(15, 15))plt.imshow(img3[:,:,::-1])plt.show()
使用Opencv实现基于Flann特征匹配
'''
Using Flann-based matching on ORB features
import numpy as np import cv2 from matplotlibimport pyplot as plt import imutils orb = cv2.ORB_create(500) img1 = cv2.imread("imgs/chapter9/indoor_lamp.jpg", 1); img1 = cv2.resize(img1, (256, 256)); gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY); img2 = cv2.imread("imgs/chapter9/indoor.jpg", 1); img2 = cv2.resize(img2, (640, 480)); gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY); # find the keypoints and descriptors with SIFT kp1, des1 = orb.detectAndCompute(img1,None) kp2, des2 = orb.detectAndCompute(img2,None) # FLANN parameters FLANN_INDEX_LSH = 6 index_params= dict(algorithm = FLANN_INDEX_LSH, table_number = 6, # 12 key_size = 12, # 20 multi_probe_level = 1) #2 search_params = dict(checks=50) # or pass empty dictionary flann = cv2.FlannBasedMatcher(index_params,search_params) matches = flann.knnMatch(des1,des2,k=2) # Need to draw only good matches, so create a mask matchesMask = [[0,0] for i in range(len(matches))] # ratio test as per Lowe's paperfor i,(m,n) in enumerate(matches): if m.distance < 0.7*n.distance: matchesMask[i]=[1,0] draw_params = dict(matchColor = (0,255,0), singlePointColor = (255,0,0), matchesMask = matchesMask, flags = cv2.DrawMatchesFlags_DEFAULT) img3 = cv2.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params) plt.figure(figsize=(15, 15)) plt.imshow(img3[:,:,::-1]) plt.show()
图像拼接
- 图像拼接或照片拼接是将多个摄影图像中重叠的视野相结合,产生一个分段的全景图或高分辨率图像的过程。
使用OpenCV来实现图像拼接
from matplotlib import pyplot as plt %matplotlib inline import cv2 import numpy as np import argparse import sys modes = (cv2.Stitcher_PANORAMA, cv2.Stitcher_SCANS)# read input imagesimgs = [cv2.imread("imgs/chapter9/left.jpeg", 1),cv2.imread("imgs/chapter9/right.jpeg", 1)]stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA) status, pano = stitcher.stitch(imgs)f = plt.figure(figsize=(15,15)) f.add_subplot(1, 2, 1).set_title('Left Image') plt.imshow(imgs[0][:,:,::-1]) f.add_subplot(1, 2, 2).set_title('Right Image') plt.imshow(imgs[1][:,:,::-1]) plt.show()
plt.figure(figsize=(15, 15))plt.imshow(pano[:,:,::-1])plt.show()