# OpenCV-Python教程:48.核面几何

O和O'是摄像机中心，从上面可以看到右边摄像机的O'的投影可以在左边图片看到，e点。这被叫做极点。极点是通过两个摄像机中心的直线与图片平面的焦点。类似的，e'是左边摄像机的极点。在某些情况下，你没法在图像内定位极点，他们可能在图像外（这表示摄像机没法看见彼此）。

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

img1 = cv2.imread('myleft.jpg',0)  #queryimage # left image
img2 = cv2.imread('myright.jpg',0) #trainimage # right image

sift = cv2.SIFT()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
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 = []

# ratio test as per Lowe's paper
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)

# We select only inlier points

def drawlines(img1,img2,lines,pts1,pts2):
''' img1 - image on which we draw the epilines for the points in img2 lines - corresponding epilines '''
r,c = img1.shape
img1 = cv2.cvtColor(img1,cv2.COLOR_GRAY2BGR)
img2 = cv2.cvtColor(img2,cv2.COLOR_GRAY2BGR)
for r,pt1,pt2 in zip(lines,pts1,pts2):
color = tuple(np.random.randint(0,255,3).tolist())
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,1)
img1 = cv2.circle(img1,tuple(pt1),5,color,-1)
img2 = cv2.circle(img2,tuple(pt2),5,color,-1)
return img1,img2

# Find epilines corresponding to points in right image (second image) and
# drawing its lines on left image

lines1 = cv2.computeCorrespondEpilines(pts2.reshape(-1,1,2), 2,F)
lines1 = lines1.reshape(-1,3)
img5,img6 = drawlines(img1,img2,lines1,pts1,pts2)

# Find epilines corresponding to points in left image (first image) and
# drawing its lines on right image
lines2 = cv2.computeCorrespondEpilines(pts1.reshape(-1,1,2), 1,F)
lines2 = lines2.reshape(-1,3)
img3,img4 = drawlines(img2,img1,lines2,pts2,pts1)

plt.subplot(121),plt.imshow(img5)
plt.subplot(122),plt.imshow(img3)
plt.show()

### 推荐阅读更多精彩内容

• 转换 OpenCV提供了两个转换函数，cv2.warpAffine和cv2.warpPerspective,通过他...
xxxss阅读 7,096评论 1 52
• Brute-Force匹配器基础 Brute-Force匹配器很简单，它取第一个集合里一个特征的描述子并用第二个集...
xxxss阅读 24,799评论 1 55
• 基础 今天的针孔摄像头对图像做了很多扭曲，两个主要的扭曲是径向畸变和切向畸变。 由于径向畸变，直线会显示成曲线，当...
xxxss阅读 11,853评论 0 52
• 理论 傅里叶变换用来分析多种过滤器的频率特征。对于图片，2D离散傅里叶变换(DFT)用来找频率范围。一个快速算法叫...
xxxss阅读 5,289评论 0 52
• 图文/by Holly° 这一天即将十八岁的我，朋友圈选择了沉默，因为我在等，等一个不用你发圈提醒却仍然记...
Holly丫阅读 385评论 0 2