Python+OpenCV:对极几何(Epipolar Geometry)

理论

When we take an image using pin-hole camera, we loose an important information, ie depth of the image.

Or how far is each point in the image from the camera because it is a 3D-to-2D conversion.

So it is an important question whether we can find the depth information using these cameras.

And the answer is to use more than one camera. Our eyes works in similar way where we use two cameras (two eyes) which is called stereo vision.

So let's see what OpenCV provides in this field.

(Learning OpenCV by Gary Bradsky has a lot of information in this field.)

Before going to depth images, let's first understand some basic concepts in multiview geometry.

In this section we will deal with epipolar geometry. See the image below which shows a basic setup with two cameras taking the image of same scene.

If we are using only the left camera, we can't find the 3D point corresponding to the point x in image because every point on the line OX projects to the same point on the image plane.

But consider the right image also. Now different points on the line OX projects to different points ( x′) in right plane.

So with these two images, we can triangulate the correct 3D point. This is the whole idea.

The projection of the different points on OX form a line on right plane (line l′). We call it epiline corresponding to the point x.

It means, to find the point x on the right image, search along this epiline.

It should be somewhere on this line (Think of it this way, to find the matching point in other image, you need not search the whole image, just search along the epiline.

So it provides better performance and accuracy). This is called Epipolar Constraint.

Similarly all points will have its corresponding epilines in the other image. The plane XOO′ is called Epipolar Plane.

O and O′ are the camera centers.

From the setup given above, you can see that projection of right camera O′ is seen on the left image at the point, e. It is called the epipole.

Epipole is the point of intersection of line through camera centers and the image planes.

Similarly e′ is the epipole of the left camera.

In some cases, you won't be able to locate the epipole in the image, they may be outside the image (which means, one camera doesn't see the other).

All the epilines pass through its epipole. So to find the location of epipole, we can find many epilines and find their intersection point.

So in this session, we focus on finding epipolar lines and epipoles.

But to find them, we need two more ingredients, Fundamental Matrix (F) and Essential Matrix (E).

Essential Matrix contains the information about translation and rotation, which describe the location of the second camera relative to the first in global coordinates.

See the image below (Image courtesy: Learning OpenCV by Gary Bradsky):

But we prefer measurements to be done in pixel coordinates, right?

Fundamental Matrix contains the same information as Essential Matrix in addition to the information about the intrinsics of both cameras so that we can relate the two cameras in pixel coordinates.

(If we are using rectified images and normalize the point by dividing by the focal lengths, F=E).

In simple words, Fundamental Matrix F, maps a point in one image to a line (epiline) in the other image.

This is calculated from matching points from both the images. A minimum of 8 such points are required to find the fundamental matrix (while using 8-point algorithm).

More points are preferred and use RANSAC to get a more robust result.

Epipolar Geometry in OpenCV

####################################################################################################
# draw Epilines
def drawlines(image1, image2, lines, pts1, pts2):"""函数功能: img1: image on which we draw the epilines for the points in img2.lines: corresponding epilines."""r, c = image1.shapeimage1 = lmc_cv.cvtColor(image1, lmc_cv.COLOR_GRAY2BGR)image2 = lmc_cv.cvtColor(image2, lmc_cv.COLOR_GRAY2BGR)for r, pt1, pt2 in zip(lines, pts1, pts2):color = (np.random.randint(0, 255), np.random.randint(0, 255), np.random.randint(0, 255))x0, y0 = map(int, [0, -r[2] / r[1]])x1, y1 = map(int, [c, -(r[2] + r[0] * c) / r[1]])image1 = lmc_cv.line(image1, (x0, y0), (x1, y1), color, 1)image1 = lmc_cv.circle(image1, tuple(pt1), 5, color, -1)image2 = lmc_cv.circle(image2, tuple(pt2), 5, color, -1)return image1, image2
####################################################################################################
# 对极几何(Epipolar Geometry)
def lmc_cv_epipolar_geometry():"""函数功能: 对极几何(Epipolar Geometry)。"""image1 = lmc_cv.imread('D:/99-Research/TestData/cv/stereo/targets/targets_l.png', lmc_cv.IMREAD_GRAYSCALE)image2 = lmc_cv.imread('D:/99-Research/TestData/cv/stereo/targets/targets_r.png', lmc_cv.IMREAD_GRAYSCALE)# find the list of best matches from both the imagessift = lmc_cv.SIFT_create()# find the keypoints and descriptors with SIFTkeypoints1, descriptors1 = sift.detectAndCompute(image1, None)keypoints2, descriptors2 = sift.detectAndCompute(image2, None)# FLANN parametersflann_index_kdtree = 1index_params = dict(algorithm=flann_index_kdtree, trees=5)search_params = dict(checks=50)flann = lmc_cv.FlannBasedMatcher(index_params, search_params)matches = flann.knnMatch(descriptors1, descriptors2, k=2)pts1 = []pts2 = []# ratio test as per Lowe's paperfor i, (m, n) in enumerate(matches):if m.distance < 0.8 * n.distance:pts2.append(keypoints2[m.trainIdx].pt)pts1.append(keypoints1[m.queryIdx].pt)# find the Fundamental Matrixpts1 = np.int32(pts1)pts2 = np.int32(pts2)ret, mask = lmc_cv.findFundamentalMat(pts1, pts2, method=lmc_cv.FM_LMEDS, ransacReprojThreshold=3, confidence=0.99,maxIters=10)# We select only inlier pointspts1 = pts1[mask.ravel() == 1]pts2 = pts2[mask.ravel() == 1]# find the epilines in both the images and draw them# Find epilines corresponding to points in right image (second image) and drawing its lines on left imagelines1 = lmc_cv.computeCorrespondEpilines(pts2.reshape(-1, 1, 2), 2, ret)lines1 = lines1.reshape(-1, 3)image5, image6 = drawlines(image1, image2, lines1, pts1, pts2)# Find epilines corresponding to points in left image (first image) and drawing its lines on right imagelines2 = lmc_cv.computeCorrespondEpilines(pts1.reshape(-1, 1, 2), 1, ret)lines2 = lines2.reshape(-1, 3)image3, image4 = drawlines(image2, image1, lines2, pts2, pts1)stacking_images = []# stacking images side-by-sidestacking_image = np.hstack((image5, image3))stacking_images.append(stacking_image)# 显示图像for i in range(len(stacking_images)):pyplot.figure('Epipolar Geometry %d' % (i + 1), figsize=(16, 9))# pyplot.subplot(1, 1, 1)pyplot.imshow(stacking_images[i], 'gray')pyplot.title('Epipolar Geometry')pyplot.xticks([])pyplot.yticks([])pyplot.savefig('%02d.png' % (i + 1))pyplot.show()

Python+OpenCV:对极几何(Epipolar Geometry)相关推荐

  1. 视觉SLAM14讲笔记04:ch7对极几何(epipolar geometry)

    2D-2D:对极几何(epipolar geometry) 对极约束 现在,假设我们从两张图像中,得到了一对配对好的特征点,像图7-7里显示的那样.如果我们有若干对这样的匹配点,就可以通过这些二维图像 ...

  2. 计算机视觉基础——对极几何(Epipolar Geometry)

    先思考一个问题:用两个相机在不同的位置拍摄同一物体,如果两张照片中的景物有重叠的部分,我们有理由相信,这两张照片之间存在一定的对应关系,本节的任务就是如何描述它们之间的对应关系,描述工具是对极几何 , ...

  3. 极线几何[Epipolar Geometry]

    前段时间看过一篇文章Image-Based Visual Hulls[Matusik,siggraph2000],其中关于Epipolar Geometry(可以翻译为极线几何,也有人叫对极几何)的部 ...

  4. [OpenCV实战]49 对极几何与立体视觉初探

    本文主要介绍对极几何(Epipolar Geometry)与立体视觉(Stereo Vision)的相关知识.对极几何简单点来说,其目的就是描述是两幅视图之间的内部对应关系,用来对立体视觉进行建模,实 ...

  5. Planar Homography (共面点成像) Epipolar Geometry(对极几何)

    转载:http://blog.csdn.NET/yvonnezju/article/details/40982192 这一篇,要搞清楚两个概念,Planar Homography (共面点成像)&am ...

  6. 【图像处理】——Python+opencv实现提取图像的几何特征(面积、周长、细长度、区间占空比、重心、不变矩等)

    转载请注明详细地址 本文简单介绍了图像常见几何特征的概念以及求解方法 本文介绍了Python和opencv求解几何特征的常用方法 目录 其他形状外接轮廓的方法可以参考:<OpenCV-Pytho ...

  7. python中geometry用法_pythongdal教程之:几何形状geometry与投影projection

    建立空的geometry对象:ogr.Geometry 定义各种不同的geometry使用的方法是不一样的(point, line, polygon, etc) 新建点point,使用方法AddPoi ...

  8. OpenCV系列之对极几何 | 五十一

    目标 在本节中 我们将学习多视图几何的基础知识 我们将了解什么是极点,极线,极线约束等 基础概念 当我们使用针孔相机拍摄图像时,我们失去了重要信息,即图像深度. 或者图像中的每个点距相机多远,因为它是 ...

  9. python 几何教学_python gdal教程之:几何形状geometry与投影projection

    建立空的geometry对象:ogr.Geometry 定义各种不同的geometry使用的方法是不一样的(point, line, polygon, etc) 新建点point,使用方法AddPoi ...

最新文章

  1. 招不招新人?IT经理很纠结.
  2. 前端模版引擎选择指南
  3. OCFS2+ASM 的RAC安装文档
  4. 项目经理怎么运用思维导图
  5. 数组去重(JavaScript)先从网上整理一波,待验证
  6. c语言结构体位定义,C语言结构体位域
  7. latex的基本使用
  8. 实验7.1 对Point类重载“++”(自增)、“–”(自减)运算符
  9. 轻量级小型网站导航条
  10. Session超时设置
  11. Windows系统和Mac OS系统的免费FTP客户端有哪些?
  12. PPP和PPPOE详解
  13. java简易日历表_java简易日历代码
  14. 域名过期后能否抢注过期高外链域名?
  15. 信贷业务全流程22个环节
  16. 安装与使用 supervisor(可管理Tomcat进程)
  17. ST-LINK固件升级
  18. 哥尼斯堡的“七桥问题” (25分)
  19. java仿qq思路_java仿QQ聊天软件OIM艰辛之路(开源项目)
  20. c语言内存和文件处理有关知识

热门文章

  1. 在写文档时如何在正文中对参考文献的引用
  2. 第七十节,css选择器
  3. 2.抽象工厂(Abstract Factory)
  4. 【MVC5】对MySql数据库使用EntityFramework
  5. CSS创建三角形(小三角)的几种方法
  6. centos5.5 下面 lnmp环境遇到的小问题
  7. android之阴影效果
  8. linux之vi,vim命令
  9. oracle 查询clob
  10. UWA TIPS:让你的项目更懂你!