OpenCV: How to Correctly Apply Perspective Transform on Arbitrarily Shaped Objects?
I'm trying to figure out I tried several approaches but none seem to work. I'm currently working on a project where I need to apply a perspective transform on images of arbitrarily shaped objects, but I'm running into unexpected results. I want to warp the input image to get a top-down view of the object, and I've obtained the four corner points of the object in the source image. However, the output seems distorted, and the shape of the object doesn't match the intended perspective. Here's what I have tried: 1. I used the `cv2.getPerspectiveTransform()` function to compute the perspective transformation matrix. 2. After that, I applied the transformation using `cv2.warpPerspective()`. Hereβs a snippet of my code: ```python import cv2 import numpy as np # Load the image image = cv2.imread('object_image.jpg') # Define the source points (corners of the object) src_points = np.array([[100, 200], [400, 200], [50, 500], [450, 500]], dtype='float32') # Define the destination points (where I want to map them) dst_points = np.array([[0, 0], [500, 0], [0, 500], [500, 500]], dtype='float32') # Get the perspective transform matrix M = cv2.getPerspectiveTransform(src_points, dst_points) # Apply the perspective warp warped_image = cv2.warpPerspective(image, M, (500, 500)) # Display the output cv2.imshow('Warped Image', warped_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` Despite following these steps, the warped image looks stretched and misaligned. The corners don't match the intended destination points, and I get a warped appearance that doesn't accurately represent the object's shape. I checked the order of the points and ensured they are in the correct sequence (top-left, top-right, bottom-left, bottom-right). I'm using OpenCV version 4.5.3, and I've also tried adjusting the destination points, but I still need to achieve the desired perspective. Could someone guide to understand what I'm doing wrong or suggest any best practices for applying perspective transforms on non-rectangular shapes? How would you solve this? For reference, this is a production service.