[Python] How to choose dst coordinates for cv2.getPerspectiveTransform(src, dst)

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

  1. Stack

    Stack Membro Participativo

    I have been searching around for quite a while, but still could not understand how destination points are chosen for bird eye view transformation. My understanding is that cv2.getPerspectiveTransform(src, dst) calculate the homography matrix (or warp matrix) by mapping points src from one image plane to the corresponding points dst on another image plane. Taking the example of converting the front view of car lane to birds-eye view, src is generally the coordinates of 4 vertex of the region of interest, but my question how to choose dst given src? I have been thinking very hard about this in the following case, please give me any help.

    Here is a concrete example: the original code in the post:

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

    IMAGE_H = 223
    IMAGE_W = 1280

    src = np.float32([[0, IMAGE_H], [1207, IMAGE_H], [0, 0], [IMAGE_W, 0]])
    dst = np.float32([[569, IMAGE_H], [711, IMAGE_H], [0, 0], [IMAGE_W, 0]])
    M = cv2.getPerspectiveTransform(src, dst) # The transformation matrix
    Minv = cv2.getPerspectiveTransform(dst, src) # Inverse transformation

    img = cv2.imread('./test_img.jpg') # Read the test img
    img = img[450:(450+IMAGE_H), 0:IMAGE_W] # Apply np slicing for ROI crop
    warped_img = cv2.warpPerspective(img, M, (IMAGE_W, IMAGE_H)) # Image warping
    plt.imshow(cv2.cvtColor(warped_img, cv2.COLOR_BGR2RGB)) # Show results
    plt.show()


    Here is the test image: [​IMG]

    roi image:

    [​IMG]

    and output transformed bird eye view image:

    [​IMG].

    What really confuses me is how dst and src are choose in this example? dst = np.float32([[569, IMAGE_H], [711, IMAGE_H], [0, 0], [IMAGE_W, 0]]), especially 569, 711, initially I thought that was the x coordinates of ego lane markers at the top, but it was not. when I change 569, 711 to x coordinates of ego lane markers (592,692) at the top. Following is the comparsion. You may see that when I changed (569, 711) to (592,692) in dst, then parallel peroperty is getting worse. [​IMG]

    Could you please help me understand how src and dst can be properly chosen?

    Update: Here is scr and dst points visualiztion, could anyone help me understand how are points chosen? I am desperate to know. [​IMG] dst matches perfectly the output bird eye view: [​IMG]

    Continue reading...

Compartilhe esta Página