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

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.

Python+OpenCV:对极几何(Epipolar Geometry)_第1张图片

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):

Python+OpenCV:对极几何(Epipolar Geometry)_第2张图片

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.shape
    image1 = 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 images
    sift = lmc_cv.SIFT_create()
    # find the keypoints and descriptors with SIFT
    keypoints1, descriptors1 = sift.detectAndCompute(image1, None)
    keypoints2, descriptors2 = sift.detectAndCompute(image2, None)
    # FLANN parameters
    flann_index_kdtree = 1
    index_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 paper
    for 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 Matrix
    pts1 = 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 points
    pts1 = 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 image
    lines1 = 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 image
    lines2 = 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-side
    stacking_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)_第3张图片

你可能感兴趣的:(Python,OpenCV,opencv,python)