'Stereo rectification problem with OpenCV and 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()

I'm working on rectification of 5 different lenses on horizontal as well as vertical direction (i.e. I need all the common points exactly in the same position in the images from all 5 lenses). While I tested stereo recitifcation with two of my lenses which were placed much farther horizontally than vertically, cv2.stereoRectify always interprets them as though they were separated vertically. I want to be able to tell the function to interpret it horizontally. I've seen some similar questions posted here, but couldn't find helpful replies anywhere.

Edit: I've added all the distortion coefficients and calibration matrices in the code for ease of replication. These values were obtained by using a large number of calibration images and a function cv2.stereoCalibrate, but it was not possible to upload all those images and code for coefficient extraction.

output image

input image 1

input image 2

clarifying the issue



Solution 1:[1]

modify this in image_size

image_size = left.shape[::-1][1:3]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Suraj Rao