用OpenCV和Python解决立体校正问题

2024-04-24 19:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文


    import cv2
    import glob
    import argparse
    import math
    from numpy import genfromtxt
    import matplotlib.pyplot as plt
    import numpy as np
    import os.path
    from scipy import ndimage
    import os


    left = cv2.imread('D:/input image 1.jpg', cv2.IMREAD_UNCHANGED)
    right = cv2.imread('D:/input image 2.jpg', cv2.IMREAD_UNCHANGED)
    #left = (left/256).astype('uint8')
    #right = (right/256).astype('uint8')


    cameraMatrix1 = np.array([[1485.8503101355045, 0, 641.0072474534551], [0, 1486.8249802291273, 454.1981417235667], [0, 0, 1]])
    cameraMatrix2 = np.array([[1472.34425902698, 0, 656.7358738783742], [0, 1473.184475795988, 441.016803589085], [0, 0, 1]])
    distCoeffs1 = np.array([-0.09236217303671054, 0.15801009565677457, 0.0020679941868083445, -0.0023435708660260184, 0.04491629603683055])
    distCoeffs2 = np.array([-0.09949068652688753, 0.22953391558591676, 0.0016749995113326907, -0.0015940937703328348, -0.13603886268508916])
    rotationMatrix = np.array([[0.9999169807005986, 0.0026862926847088424, -0.012602203704541104],[-0.002633967055223802, 0.9999878496600472, 0.0041668633079119935],[0.012613243997904163, -0.004133323588458492, 0.9999119069757908]])
    transVector = np.array([29.96389633009774, 0.5883268401189343, -5.0370190999346365])
    essentialMatrix = np.array([[-0.005846632380824811, 5.0345261532342365, 0.6092635826971343], [-5.4145428656773165, 0.11031957194242471, -29.897779179091888], [-0.6672019134164675, 29.96195184048419, 0.1322696748639909]])
    fundMatrix = np.array([[4.567507458136527e-08, -3.930495370357416e-05, 0.010750771532659317], [4.227537878312907e-05, -8.607826196991683e-07, 0.3201405456504413], [-0.010999824926761303, -0.3182113833954986, 1]])


    flags = cv2.CALIB_ZERO_DISPARITY
    image_size = left.shape[::-1]

    R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, image_size, rotationMatrix, transVector, flags = flags)

    leftmapX, leftmapY = cv2.initUndistortRectifyMap(cameraMatrix1, distCoeffs1, R1, P1, image_size, cv2.CV_32FC1)
    rightmapX, rightmapY = cv2.initUndistortRectifyMap(cameraMatrix2, distCoeffs2, R2, P2, image_size, cv2.CV_32FC1)

    left_remap = cv2.remap(left, leftmapX, leftmapY, cv2.INTER_LANCZOS4)
    right_remap = cv2.remap(right, leftmapX, rightmapY, cv2.INTER_LANCZOS4)


    # For some reason, the images get rotated upside down after remapping, and I have to invert them back
    left_remap = ndimage.rotate(left_remap,180)
    right_remap = ndimage.rotate(right_remap,180)

    for line in range(0, int(right_remap.shape[0] / 20)):
        left_remap[line * 20, :] = 0
        right_remap[line * 20, :] = 0

    cv2.namedWindow('output images', cv2.WINDOW_NORMAL)
    cv2.imshow('output images', np.hstack([left_remap, right_remap]))
    cv2.waitKey(0)
    cv2.destroyAllWindows()

我正在对水平和垂直方向上的5个不同的镜头进行校正(即,我需要所有的公共点在所有5个镜头的图像中完全相同的位置)。当我用我的两个镜片测试立体背诵时,这两个镜片水平放置的比垂直放置的要远得多,cv2.StereoConvertive总是把它们当作垂直分开来解释。我希望能够告诉函数水平地解释它。我在这里看到过一些类似的问题,但在任何地方都找不到有用的答复。你知道吗

编辑:为了便于复制,我在代码中添加了所有失真系数和校准矩阵。这些值是通过使用大量的校准图像和一个函数cv2.stereoCalibrate获得的,但是不可能上传所有这些图像和用于系数提取的代码。你知道吗

output image

input image 1

input image 2

clarifying the issue


Tags: imageimportrightinputsizenparraycv2