CV_Python特征检测: 角点、关键点检测

上一篇:Python OpenCV Image Transforms-图像变换

1. Harris角点检测 cv2.cornerHarris

# params: img, blockSize(邻域), ksize(Sobel孔径), k(公式中参数)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)

#  效果

CV_Python特征检测: 角点、关键点检测_第1张图片

2. Harris亚像素精度的角点检测 cv2.cornerSubPix

gray = np.float32(gray)
dst = cv2.cornetHarris(gray, 2, 3, 0.04)
dst = cv2.dilate(dst, None)
ret, dst = cv2.threshold(dst, 0.01*dst.max(), 255, 0)

ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)

criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITERR, 100, 0.001) # 迭代中止条件
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5,5), (-1,-1), criteria)

res = np.hstack((centroids, corners))
res = np.int0(res)
img[res[:,1], res[:,0]] = [0,0,255]
img[res[:,3], res[:,2]] = [0,255,0]

# 红点为Harris角点,绿色为精度修正后的角点

CV_Python特征检测: 角点、关键点检测_第2张图片

3. Shi-Tomasi角点检测 cv2.goodFeaturesToTrack

corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)
corners = np.int0(corners)
for i in corners:
    x,y = i.ravel()
    cv2.circle(img, (x,y), 3, 255, -1)

CV_Python特征检测: 角点、关键点检测_第3张图片

4. SIFT特征点检测 cv2.xfeatures2d.SIFT_create

# SIFT: Scale-Invariant Feature Transform,尺度不变特征转换
# 方法一
sift = cv2.xfeatures2d.SIFT_create() # 定义sift对象
kp = sift.detect(gray, None)         # 关键点集合
img = cv2.drawKeypoints(gray, kp, img) # 可视化关键点
# 可视化关键点(圆表示)
img = cv2.drawkeypoints(gray, kp, img, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)


# 方法二
sift = cv2.xfeatures2d.SIFT_create()
kp, des = sift.detectAndCompute(gray, None)

CV_Python特征检测: 角点、关键点检测_第4张图片

5. SURF 特征点检测 cv.xfeatures2d.SURF_create

# SURF: Speeded-Up Robust Features 加速鲁棒性特征
surf = cv2.xfeatures2d.SURF_create(400) # 400: Hessian阈值
kp, des = surf.detectAndCompute(img, None) # keypoints, descriptor
img2 = cv2.drawKeypoints(img, kp, None, (255,0,0), 4)

CV_Python特征检测: 角点、关键点检测_第5张图片

# U-SURF
surf.setUpright(True)
kp = surf.detect(img, None)
img2 = cv2.drawKeypoints(img, kp, None, (255,0,0), 4)

CV_Python特征检测: 角点、关键点检测_第6张图片

# 获取描述子大小
print(surf.descriptorSize())  # 64

# 更改大小
surf.setExtended(True)
kp, des = surf.dectAndCompute(img, None)
print(surf.descriptorSize()) # 64

print(des.shape)  # (47, 128)

6. FAST 角点检测 cv2.FastFeatureDetector_create

# FAST: Features from Accelerated Segment Test 加速段测试特征
fast = cv2.FastFeatureDetector_create()
kp = fast.detect(img, None)

# 极大值抑制和未抑制图例

CV_Python特征检测: 角点、关键点检测_第7张图片

7. BRIEF算法  cv2.xfeatures2d.BreifDescriptor_create

# BRIEF: Binary Robust Independent Elementary Features 二值鲁棒独立基本特征
star = cv2.xfeatures2d.StartDetector_create()   # FAST
breif = cv2.xfeatures2d.BreifDescriptor_create() # BRIEF
kp = star.detect(img, None)
kp, des = brief.compute(img, kp)

8. ORB 

# ORB: Oriented FAST and Rotated BRIEF
orb = cv2.ORB_create()
kp = orb_detect(img, None)
kp, des = orb.compute(img, kp)

CV_Python特征检测: 角点、关键点检测_第8张图片

9. 暴力特征匹配 Brute-Force Matcher

orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)

bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = sorted(matches, key=lambda x: x.distance)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], flags=2)

CV_Python特征检测: 角点、关键点检测_第9张图片

10. 与SIFT描述符和比率测试的暴力匹配

sift = cv2.SIFT()
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])
img3 = cv2.drawMatchesKnn(img1, kp1, img2, kp2, good, flags=2)

CV_Python特征检测: 角点、关键点检测_第10张图片

11. 基于FLANN的匹配子

# FLANN: Fast Library for Approximate Nearest Neighbors 近似最近邻快速库
sift = cv.SIFT()
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

FLANN_INDEX_KDTREE = 1
FLANN_INDEX_KDTREE = 1
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)   # or pass empty dictionary

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)

matchesMask = [[0,0] for i in xrange(len(matches))]
for 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 = 0)

img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)

CV_Python特征检测: 角点、关键点检测_第11张图片

你可能感兴趣的:(CV_Python特征检测: 角点、关键点检测)