1、SIFT
import cv2
import sys
import numpy as np
filename = 'img/chessboard2.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptor = sift.detectAndCompute(gray,None)
img = cv2.drawKeypoints(image=img, outImage=img, keypoints = keypoints, flags = 4, color = (0, 0, 255))
cv2.imshow('sift_keypoints', img)
cv2.waitKey(0)
2、SURF
import cv2
imgpath = 'img/chessboard2.jpg'
img = cv2.imread(imgpath)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
surf = cv2.xfeatures2d.SURF_create(5000)
keypoints, descriptor = surf.detectAndCompute(gray, None)
img = cv2.drawKeypoints(image=img, outImage=img, keypoints=keypoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,
color=(0, 0, 255))
cv2.imshow('IMAGE', img)
cv2.imwrite('SURF.png', img)
while True:
if cv2.waitKey() & 0xff == ord('q'):
break
cv2.destroyAllWindows()
3、Harris
# -*- coding: utf-8 -*-
import cv2
import numpy as np
filename = 'img/chessboard2.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# 输入图像必须是 float32 ,最后一个参数在 0.04 到 0.05 之间
dst = cv2.cornerHarris(gray,2,3,0.04)
#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
4、ORB
# coding=utf-8
import cv2
import numpy as np
img1 = cv2.imread('img/test1.png')
img2 = cv2.imread('img/test12.png')
#最大特征点数,可以设置大小,如100或5000等。
orb = cv2.ORB_create(100)
kp1, des1 = orb.detectAndCompute(img1,None)
kp2, des2 = orb.detectAndCompute(img2,None)
# 获得一个暴力匹配器的对象
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
# 利用匹配器 匹配两个描述符的相近成都
maches = bf.match(des1, des2)
# 按照相近程度 进行排序
maches = sorted(maches, key=lambda x: x.distance)
# 画出匹配项
img3 = cv2.drawMatches(img1, kp1, img2, kp2, maches[: 30], img2, flags=2)
cv2.imshow("matches", img3)
cv2.waitKey()
cv2.destroyAllWindows()
5、FAST
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('img/chessboard2.jpg',0)
# Initiate FAST object with default values
fast = cv2.FastFeatureDetector()
# find and draw the keypoints
kp = fast.detect(img,None)
img2 = cv2.drawKeypoints(img, kp, color=(255,0,0))
# Print all default params
print("Threshold: ", fast.getInt('threshold'))
print("nonmaxSuppression: ", fast.getBool('nonmaxSuppression'))
print(fast.getInt('type'))
print("Total Keypoints with nonmaxSuppression: ", len(kp))
cv2.imwrite('fast_true.png',img2)
# Disable nonmaxSuppression
fast.setBool('nonmaxSuppression',0)
kp = fast.detect(img,None)
print("Total Keypoints without nonmaxSuppression: ", len(kp))
img3 = cv2.drawKeypoints(img, kp, color=(255,0,0))
cv2.imwrite('fast_false.png',img3)
6、BRIEF描述符
不检测特征,只进行描述;对STAR检测器检测的特征描述效果最好。
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('img/chessboard2.jpg',0)
star = cv2.xfeatures2d.StarDetector_create()
# Initiate BRIEF extractor
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()
# find the keypoints with STAR
kp = star.detect(img, None)
# compute the descriptors with BRIEF
kp, des = brief.compute(img, kp)
print(brief.descriptorSize())
print(des.shape)