from pylab import *
import cv2from backend._gs_ import stereo_cameradef compute_epipole(F):""" 从基础矩阵 F 中计算右极点(可以使用 F.T 获得左极点)"""# 返回 F 的零空间(Fx=0)U,S,V = np.linalg.svd(F)e = V[-1]return e/e[2]def plot_epipolar_line(im, F, x, epipole=None, show_epipole=True):""" 在图像中,绘制外极点和外极线 F×x=0。F 是基础矩阵,x 是另一幅图像中的点 """m, n = im.shape[:2]line = np.dot(F, x)# 外极线参数和值t = np.linspace(0, n, 100)lt = np.array([(line[2] + line[0] * tt) / (-line[1]) for tt in t])# 仅仅处理位于图像内部的点和线ndx = (lt >= 0) & (lt < m)plot(t[ndx], lt[ndx], linewidth=2)if show_epipole:if epipole is None:epipole = compute_epipole(F)plot(epipole[0] / epipole[2], epipole[1] / epipole[2], 'r*')F = stereo_camera.F
im1 = cv2.imread(r'D:\mydocs\ftp\stereo_test\stereo_cali\small_test1\75_1.png')
im2 = cv2.imread(r'D:\mydocs\ftp\stereo_test\stereo_cali\small_test1\89_1.png')
x2 = np.array([ [1422, 1490, 1486], [1082,1183,1074], [1,1,1]])
# 计算极点
e = compute_epipole(F)# 绘制图像
figure()subplot(121)
imshow(im1)
# 分别绘制每个点,这样会绘制出和线同样的颜色
for i in range(3):plot(x2[0, i], x2[1, i], 'o')
title(u'outer polar')
axis('off')subplot(122)
imshow(im2)
# 分别绘制每条线,这样会绘制出很漂亮的颜色
for i in range(3):plot_epipolar_line(im2, F, x2[:, i], e, False)
title(u'outer polar')
axis('off')show()