harris特征点匹配python代码
时间: 2023-10-12 22:21:47 浏览: 124
以下是harris特征点匹配的Python代码示例:
```python
import cv2
import numpy as np
# 读取图片
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# 转换为灰度图
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Harris角点检测
dst1 = cv2.cornerHarris(gray1,2,3,0.04)
dst2 = cv2.cornerHarris(gray2,2,3,0.04)
# 归一化
cv2.normalize(dst1, dst1, 0, 255, cv2.NORM_MINMAX)
cv2.normalize(dst2, dst2, 0, 255, cv2.NORM_MINMAX)
# 绘制角点
img1[dst1>0.01*dst1.max()]=[0,0,255]
img2[dst2>0.01*dst2.max()]=[0,0,255]
# 特征点描述
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, 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, None, flags=2)
# 显示图片
cv2.imshow('img1', img1)
cv2.imshow('img2', img2)
cv2.imshow('matches', img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
请注意,这只是harris特征点匹配的示例代码,实际情况下可能需要根据具体需求进行优化和修改。
阅读全文