This question is not specific to opencv, but is largely based on mathematics and is more often considered in the field of computer graphics. What you are looking for is called Projective Transformation .
A projective transformation takes a set of coordinates and projects them onto something. In your case, you want to project a two-dimensional point in the form of a camera onto a two-dimensional point on a flat plane.
So, we want a projection transformation for 2D space. To perform a projection transformation, we need to find the projection matrix for the transformation that we want to apply. In this case, we need a matrix expressing the projective deformation of the camera relative to a flat plane.
To work with projections, you first need to transform our points into homogeneous coordinates . To do this, we simply add a new component to our vectors with a value of 1. So (x,y) becomes (x,y,1) . And we will do it with all of our five available points.
Now we will start with actual math. First, determine: the camera’s point of view and the corresponding coordinates should be camera space , the coordinates relative to the flat plane are in flat space . Let c₁ - c₄ be the angular points of the plane with respect to the chamber space as homogeneous vectors. Let p be the point we found in camera space and p' point we want to find in flat space, again as homogeneous vectors.
Mathematically, we are looking for the matrix C , which allows us to calculate p' , giving it p .
p' = C * p
Now we obviously need to find C To find the projection matrix for a two-dimensional space, we need four points (how convenient ..) I suppose that c₁ goes to (0,0) , c₂ goes to (0,1) , c₃ to (1,0) and c₄ to (1,1) . You need to solve two matrix equations using, for example, the elimination of a Gaussian line or the LR decomposition algorithm. OpenCV should contain functions to perform these tasks for you, but keep in mind the matrix setup and its effect on a useful solution.
Now back to the matrices. You need to calculate two basis change matrices, as they are called. They are used to change the coordinate system of your coordinates (exactly what we want to do). The first matrix converts our coordinates into three-dimensional basis vectors, and the second converts our two-dimensional plane into three-dimensional basis vectors.
For the coordinate, you need to calculate λ , μ and r in the following equation:
⌈ c₁.x c₂.x c₃.x ⌉ ⌈ λ ⌉ ⌈ c₄.x ⌉ c₁.y c₂.y c₃.y * μ = c₄.y ⌊ 1 1 1 ⌋ ⌊ r ⌋ ⌊ 1 ⌋
this will lead you to your first matrix, a
⌈ λ*c₁.x μ*c₂.xr*c₃.x ⌉ A = λ*c₁.y μ*c₂.yr*c₃.y ⌊ λ μ r ⌋
A now compares the points c₁ - c₄ with the base coordinates (1,0,0) , (0,1,0) , (0,0,1) and (1,1,1) . We do the same for our aircraft. First allow
⌈ 0 0 1 ⌉ ⌈ λ ⌉ ⌈ 1 ⌉ 0 1 0 * μ = 1 ⌊ 1 1 1 ⌋ ⌊ r ⌋ ⌊ 1 ⌋
and get B
⌈ 0 0 r ⌉ B = 0 μ 0 ⌊ λ μ r ⌋
A and B will now be mapped from these three-dimensional basis vectors to your respective spaces. But this is not exactly what we want. We want camera space -> basis -> flat space , so only matrix B works in the right direction. But this is easily fixed by inverting A This will give us the matrix C = B * A⁻¹ (see order B and A⁻¹ , this is not interchangeable). This leaves us with a formula for calculating p' from p .
p' = C * p p' = B * A⁻¹ * p
Read it from left to right, like: take p, convert p from camera space to base vectors and convert them to flat space.
If you remember correctly, p' still has three components, so before we can use it, we need to dehomogenize p' . It will give
x' = p'.x / p'.z y' = p'.y / p'.z
and viola, we successfully transformed the laser dot from the camera onto a flat sheet of paper. Absolutely not too complicated or so ...