1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Image Rectification - OpenCV - Python

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to rectify pairs of images from the RGB-D Dataset 7-Scenes . Since the dataset provides ground truth pose data, I am not trying to extract matching points and calculate F. Instead I am calculating the relative translation and rotation using https://math.stackexchange.com/a/709658 which seems to be correct, and treat the two frames as a calibrated scene.

    However, the rectification results are garbage. I tried playing with several alpha values (-1, 0, 1, and many values in [0,1] range) but nothing gives good results.

    The relative R and t seem fine (near frames have small rotations and translations).

    For the camera matrix I am using values given in the dataset (Principle point (320,240), Focal length (585,585))

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

    frame1 = 100
    frame2 = 160

    img_1_path = './chess/seq-01/frame-000{}.color.png'.format(frame1)
    img_2_path = './chess/seq-01/frame-000{}.color.png'.format(frame2)

    pose_1_path = './chess/seq-01/frame-000{}.pose.txt'.format(frame1)
    pose_2_path = './chess/seq-01/frame-000{}.pose.txt'.format(frame2)

    img_1 = cv2.imread(img_1_path)
    img_2 = cv2.imread(img_2_path)

    pose1 = np.loadtxt(pose_1_path)
    pose2 = np.loadtxt(pose_2_path)

    R1 = pose1[0:3, 0:3]
    t1 = pose1[0:3, 3]

    R2 = pose2[0:3, 0:3]
    t2 = pose2[0:3, 3]

    # https://math.stackexchange.com/questions/709622/relative-camera-matrix-pose-from-global-camera-matrixes
    R = np.matmul(np.linalg.inv(R2), R1)
    T = np.matmul(np.linalg.inv(R2), (t1 - t2))


    # https://www.microsoft.com/en-us/research/project/rgb-d-dataset-7-scenes/
    px = 320.0
    py = 240.0
    fx = 585.0
    fy = 585.0

    cameraMatrix1 = np.array(
    [
    [fx, 0, px],
    [0, fy, py],
    [0, 0, 1.0]
    ]
    )

    cameraMatrix2 = cameraMatrix1

    distCoeff = np.zeros(4)

    R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(
    cameraMatrix1=cameraMatrix1,
    distCoeffs1=distCoeff,
    cameraMatrix2=cameraMatrix2,
    distCoeffs2=distCoeff,
    imageSize=(640, 480),
    R=R,
    T=T,
    flags=cv2.CALIB_ZERO_DISPARITY,
    alpha=1)

    map1x, map1y = cv2.initUndistortRectifyMap(
    cameraMatrix=cameraMatrix1,
    distCoeffs=distCoeff,
    R=R1,
    newCameraMatrix=P1,
    size=(640, 480),
    m1type=cv2.CV_32FC1)

    map2x, map2y = cv2.initUndistortRectifyMap(
    cameraMatrix=cameraMatrix2,
    distCoeffs=distCoeff,
    R=R2,
    newCameraMatrix=P2,
    size=(640, 480),
    m1type=cv2.CV_32FC1)


    img1_rect = cv2.remap(img_1, map1x, map1y, cv2.INTER_LINEAR)
    img2_rect = cv2.remap(img_2, map2x, map2y, cv2.INTER_LINEAR)

    fig, ax = plt.subplots(nrows=2, ncols=2)

    plt.subplot(2, 2, 1)
    plt.imshow(img_1)

    plt.subplot(2, 2, 2)
    plt.imshow(img_2)

    plt.subplot(2, 2, 3)
    plt.imshow(img1_rect)

    plt.subplot(2, 2, 4)
    plt.imshow(img2_rect)

    plt.show(block=False)
    plt.pause(10)
    plt.close()


    [​IMG] Any idea what am I missing? Should I not trust the ground truth pose data or is it a flaw in my pipeline?

    Thanks.

    Continue reading...

Compartilhe esta Página