how do I determine the locations of the points after perspective transform, in the new image plane?

2024/10/1 5:37:33

I'm using OpenCV+Python+Numpy and I have three points in the image, I know the exact locations of those points.

(P1, P2);N1

I am going to transform the image to another view, (for example I am transforming the perspective view to side view). If I do so I will not have the exact location of those three points in the image plane. I should write the code in a way that I can get new coordinates of those points.

   pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]]) pts2=np.float32([[700,732],[869,754],[906,916],[712,906]])matrix=cv2.getPerspectiveTransform(pts1,pts2)result=cv2.warpPerspective(Image1,matrix,(1920,1080))cv2.imshow('Image',Image1) cv2.imshow('Tran',result)

My question is: How can I determine the new locations of those 3 points?

Answer

Easy, you can look in the documentation how warpPerspective works. To transform the location of a point you can use the following transformation:

enter image description here

Where [x, y] is the original point, and M is your perspective matrix

Implementing this in python you can use the following code:

p = (50,100) # your original point
px = (matrix[0][0]*p[0] + matrix[0][1]*p[1] + matrix[0][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
py = (matrix[1][0]*p[0] + matrix[1][1]*p[1] + matrix[1][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
p_after = (int(px), int(py)) # after transformation

You can see the result in a code below. The red dot is your original point. The second figure shows where it went after the perspective transform. The blue circle is the point you calculated in formula above.

blue

You can have a look in my Jupyter Notebook here or here.

The code:

import numpy as np
import cv2
import matplotlib.pyplot as plt# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread('sample.png')
pts1=np.float32([[867,652],[1020,580],[1206,666],[1057,757]]) 
pts2=np.float32([[700,732],[869,754],[906,916],[712,906]])
matrix=cv2.getPerspectiveTransform(pts1,pts2)# Draw the point
p = (50,100)
cv2.circle(image,p, 20, (255,0,0), -1)# Put in perspective
result=cv2.warpPerspective(image,matrix,(1500,800))# Show images
plt.imshow(image)
plt.title('Original')
plt.show()plt.imshow(result)
plt.title('Distorced')
plt.show()# Here you can transform your point
p = (50,100)
px = (matrix[0][0]*p[0] + matrix[0][1]*p[1] + matrix[0][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
py = (matrix[1][0]*p[0] + matrix[1][1]*p[1] + matrix[1][2]) / ((matrix[2][0]*p[0] + matrix[2][1]*p[1] + matrix[2][2]))
p_after = (int(px), int(py))# Draw the new point
cv2.circle(result,p_after, 20, (0,0,255), 12)# Show the result
plt.imshow(result)
plt.title('Predicted position of your point in blue')
plt.show()
https://en.xdnf.cn/q/70994.html

Related Q&A

How to do a simple Gaussian mixture sampling and PDF plotting with NumPy/SciPy?

I add three normal distributions to obtain a new distribution as shown below, how can I do sampling according to this distribution in python?import matplotlib.pyplot as plt import scipy.stats as ss im…

Python dict.get() or None scenario [duplicate]

This question already has answers here:Truth value of a string in python(4 answers)Closed 7 years ago.I am attempting to access a dictionarys values based on a list of keys I have. If the key is not pr…

p-values from ridge regression in python

Im using ridge regression (ridgeCV). And Ive imported it from: from sklearn.linear_model import LinearRegression, RidgeCV, LarsCV, Ridge, Lasso, LassoCVHow do I extract the p-values? I checked but rid…

AutoTokenizer.from_pretrained fails to load locally saved pretrained tokenizer (PyTorch)

I am new to PyTorch and recently, I have been trying to work with Transformers. I am using pretrained tokenizers provided by HuggingFace.I am successful in downloading and running them. But if I try to…

How to scroll down in an instagram pop-up frame with Selenium

I have a python script using selenium to go to a given Instagram profile and iterate over the users followers. On the instagram website when one clicks to see the list of followers, a pop-up opens with…

Get starred messages from GMail using IMAP4 and python

I found many dummy info about working with IMAP, but I didnt understand how to use it for my purposes. I found how I can get ALL messages from mailbox and ALL SEEN messages, but how should I work with …

python and php bcrypt

I was using Laravel to register the users. It uses bcrypt like so:$2y$10$kb9T4WXdz5aKLSZX1OkpMOx.3ogUn9QX8GRZ93rd99i7VLKmeoXXXI am currently making another script that will authenticate users from anot…

Python socket library thinks socket is open when its not

Im working with a bit of Python that looks like this:HOST = 127.0.0.1 PORT = 43434 single = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:single.bind((HOST, PORT)) except socket.error as e:# Pr…

object of type _csv.reader has no len(), csv data not recognized

The following is a self-contained example. Change the "folder_name" to run it. This answers : reader type = _csv.reader list(reader) = [] _csv.reader has no len()I have tried many things but …

Lookup country for GPS coordinates without Internet access

I need to find out in what country given GPS coordinates are, on a device that has no Internet access (e.g. this, but without the easy on-line solution). Having no experience with GIS, I guess Id need …