不用显示特征点,只需要显示对应的特征线段
时间: 2024-02-23 18:01:48 浏览: 25
下面是一个只显示对应的特征线段而不显示特征点的示例代码:
```python
import cv2
# 读取图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 初始化 SIFT 特征检测器
sift = cv2.xfeatures2d.SIFT_create()
# 检测特征点和特征描述符
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)
# 初始化暴力匹配器
bf = cv2.BFMatcher()
# 匹配特征点
matches = bf.match(des1, des2)
# 根据距离排序
matches = sorted(matches, key=lambda x: x.distance)
# 取前 10 个匹配的特征点
matches = matches[:10]
# 提取匹配的特征点的坐标
pts1 = []
pts2 = []
for match in matches:
pts1.append(kp1[match.queryIdx].pt)
pts2.append(kp2[match.trainIdx].pt)
# 计算基础矩阵
F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_RANSAC)
# 提取内点的特征点坐标
pts1 = [pts1[i] for i in range(len(pts1)) if mask[i] == 1]
pts2 = [pts2[i] for i in range(len(pts2)) if mask[i] == 1]
# 绘制对应的特征线段
lines = cv2.computeCorrespondEpilines(pts2, 2, F)
lines = lines.reshape(-1, 3)
img3 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img4 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
for line, pt2 in zip(lines, pts2):
x0, y0 = map(int, [0, -line[2]/line[1]])
x1, y1 = map(int, [img3.shape[1], -(line[2]+line[0]*img3.shape[1])/line[1]])
img3 = cv2.line(img3, (x0, y0), (x1, y1), (0, 255, 0), 1)
img3 = cv2.circle(img3, tuple(map(int, pt2)), 5, (0, 0, 255), -1)
img4 = cv2.circle(img4, tuple(map(int, pt2)), 5, (0, 0, 255), -1)
# 显示图像
cv2.imshow('Epipolar Lines', cv2.hconcat([img3, img4]))
cv2.waitKey(0)
cv2.destroyAllWindows()
```
上述代码中,首先使用 `cv2.imread()` 函数读取两张图像,然后使用 `cv2.xfeatures2d.SIFT_create()` 函数初始化 SIFT 特征检测器,然后调用 `detectAndCompute()` 函数检测特征点和特征描述符。
接着,使用 `cv2.BFMatcher()` 函数初始化暴力匹配器,然后调用 `match()` 函数匹配特征点。匹配后,根据距离排序,并取前 10 个匹配的特征点。
然后,计算基础矩阵,并提取内点的特征点坐标。接着,使用 `cv2.computeCorrespondEpilines()` 函数计算对应的特征线段,并在图像上绘制线段和特征点。
最后,使用 `cv2.imshow()` 函数显示图像。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)