【对极几何】Opencv在原图中画极线

Opencv在原图中画极线

根据双目标定后,已知基础矩阵,在原图中画待匹配点的极线
参考: opencv的极线几何

import cv2
import numpy as np
from matplotlib import pyplot as plt
import os

img1 = cv2.imread('shuangmu_biaoding/11_3/11_3_left_img/23.JPG')  # queryimage # 左侧图片
img2 = cv2.imread('shuangmu_biaoding/11_3/11_3_right_img/23.JPG')  # trainimage # 右侧图片
line_save = './shuangmu_biaoding/11_3/line_save/'
if not os.path.exists(line_save):
    os.makedirs(line_save)

# img1_gray = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
# img2_gray = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)

# sift = cv2.xfeatures2d.SIFT_create()
#
# # 用SIFT查找关键点和描述子
# kp1, des1 = sift.detectAndCompute(img1, None)
# kp2, des2 = sift.detectAndCompute(img2, None)
#
# # FLANN参数
# FLANN_INDEX_KDTREE = 1
# index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
# search_params = dict(checks=50)
#
# flann = cv2.FlannBasedMatcher(index_params, search_params)
# matches = flann.knnMatch(des1, des2, k=2)
#
# good = []
# pts1 = []
# pts2 = []
#
# # 按照Lowe的论文进行比率测试
# for i, (m, n) in enumerate(matches):
#     if m.distance < 0.8 * n.distance:
#         good.append(m)
#         pts2.append(kp2[m.trainIdx].pt)
#         pts1.append(kp1[m.queryIdx].pt)
#
# pts1 = np.int32(pts1)
# pts2 = np.int32(pts2)
# F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_LMEDS)
#
# # 我们只使用inlier点
# pts1 = pts1[mask.ravel() == 1]
# pts2 = pts2[mask.ravel() == 1]
color_list = [(37, 70, 241), (228, 57, 46), (12, 255, 146)]

def drawlines(img1, img2, lines, pts1, pts2):
    ''' img1 - 我们要绘制到的图像
        lines - 相应的极线 '''
    r, c, n = img1.shape
    # img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR)
    # img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2BGR)
    i = 0
    for r, pt1, pt2 in zip(lines, pts1, pts2):
        # color = tuple(np.random.randint(0, 255, 3).tolist())
        color = color_list[i]
        x0, y0 = map(int, [0, -r[2]/r[1]])
        x1, y1 = map(int, [c, -(r[2]+r[0]*c)/r[1]])
        img1 = cv2.line(img1, (x0, y0), (x1, y1), color, 10)
        img1 = cv2.circle(img1,tuple(pt1), 20, color, -1)
        img2 = cv2.circle(img2,tuple(pt2), 20, color, -1)
        i+=1
    return img1, img2

pts2 = np.array([[1562,1029], [1573, 842], [1615, 1181] ])	# 给出右视图中要匹配的点,用于在左视图中画对应的极线
pts1 = np.array([[2861, 1081], [2867, 895], [2912, 1234] ])	# 给出左视图中要匹配的点,用于在右视图中画对应的极线
F = np.load('./shuangmu_biaoding/11_3/files_save/F.npy')	# 基础矩阵F在双目标定过程中得到
E = np.load('./shuangmu_biaoding/11_3/files_save/E.npy')	# 本质矩阵E在双目标定过程中得到
print('基础矩阵F:'+ '\n', F)
print('本征矩阵:'+ '\n', E)



# 找到右边图像(第二张图像)中的点对应的极线,在左边图像上画出来
lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 2, F)
lines1 = lines1.reshape(-1, 3)
img5, img6 = drawlines(img1, img2, lines1, pts1, pts2)

# 找到左边图像(第一张图像)中的点对应的极线
# 在右边图像上画出来
lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 1, F)
lines2 = lines2.reshape(-1, 3)
img3, img4 = drawlines(img2, img1, lines2, pts2, pts1)

cv2.imwrite(line_save+ 'result_left.png', img5)	
cv2.imwrite(line_save+ 'result_right.png', img3)
plt.subplot(121), plt.imshow(img5)
plt.subplot(122), plt.imshow(img3)
plt.show()
plt.savefig('sift_left_right.png')
# plt.show()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XTX_AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值