Skip to content

getPerspectiveTransform produces wrong output on certain data #26916

@Henning742

Description

@Henning742

System Information

Python 3.10.16
Ubuntu 24.04 LTS
opencv-python 4.11.0.86

Detailed description

getPerspectiveTransform produces wrong output on certain data. In the code below, these different ways to get inv_M should all be able to correctly transfom dst_point to src_point, but the result (src_new) from inv_M1 is incorrect. This only happens when diff is 320. This issue is probably related to #11944 (in yr 2018). But this bug is still in the latest opencv-python.

Steps to reproduce

import cv2
import numpy as np

src_points = np.float32([[0, 0], [1280, 0], [0, 1024], [1280, 1024]])

diff = 320
dst_points = np.float32([[diff, 512], [1280-diff, 512], [0, 1024], [1280, 1024]])

M = cv2.getPerspectiveTransform(src_points, dst_points)
inv_M1 = cv2.getPerspectiveTransform(dst_points, src_points)
inv_M2 = np.linalg.inv(M)
inv_M3 = cv2.findHomography(dst_points, src_points)[0]

np.set_printoptions(suppress=True)

# print(inv_M1)
# print(inv_M2)
# print(inv_M3)

for inv_M in [inv_M1, inv_M2, inv_M3]:
    dst_new = np.concatenate([dst_points, np.ones((4, 1))], axis=1)
    src_new = np.matmul(inv_M, dst_new.T).T
    src_new = src_new[:, :2] / src_new[:, 2].reshape(-1, 1)

    # the output should all be the same to src_points
    print("src_new:")
    print(src_new)

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions