Perspective transform given point (x,y) in quadrilateral plane to Rectangle plane's point (x', y')? - image

I am trying to transform quadrilateral to rectangular plane And need to extract coordinate of 1 specific point (in quadrilateral plane), to that in respect to rectangular plane..
I'm using EmguCV for image processing purpose in my .NET project
What I've tried is:
1) Calculate Homography matrix between quadrilateral and rectangular plane (specifying points in clockwise order from left top corner for both planes)
2) Multiply above Homography matrix by 3 x 1 matrix [x,y,1] to get final coordinates.
However, the resultant coordinate (x', y') does not seem in concordance with given point (x,y).

As Micka suggested, after having resultant matrix (3x1), all that is needed to solve this problem was this: p' = (x'/z', y'/z')
Steps as below:
Calculate Homography matrix between quadrilateral and rectangular plane
Multiply this homography mat. with candidate point [x,y,1]T and get [x',y',z']T
Now, Dehomogenize above [x',y',z']T i.e. [(x'/z'), (y'/z'), 1]T
thus, the required final coordinate of rectangular plane.

Related

Calculate transformation matrix of 3d plane given 4 corners

I have a 3d plane at the world origin that is aligned with the world X/Y plane (facing the Z axis). I then have four 3d vertex positions for a new plane transformed into some location in 3d space.
Both planes have the same winding order for all 4 vertices.
I have a guarantee that the 4 corners are planar and there is no skewing (the plane may have still been scaled individually on the x/y axes).
How can I create a 4x4 transformation matrix given the final 4 corners of this plane?
Assume that the plane looks like this:
Construct a "local basis" of the plane, with the:
X-axis parallel to AD / BC
Y-axis parallel to AB / CD
Z-axis parallel to the normal
Origin O at the center of the quad
The transformation matrix can be decomposed into 3 components:
1 – Scale
Since the original quad has dimensions of 1x1 units, the scaling factor along the X and Y local axes are simply the side lengths, i.e. the lengths of AD and AB respectively. Ignore the Z scaling factor since the quad is planar.
Therefore the scaling component is given by:
2 - Rotation
The rotational component can be directly constructed from the local basis axes X, Y, Z; each vector (normalized) is the corresponding column of the matrix.
Therefore the rotational component is given by:
3 - Translation
This is the easiest one; the translation vector is simply the absolute coordinate of the quad's center O, and is equal to the last column of the matrix.
Therefore the translational component is given by:
The final matrix can be obtained by multiplying the above in the following order:
i.e. the components are applied in the order 1 ⇨ 2 ⇨ 3.

Transforming a 3D plane onto a 2D coordinate system

Say I have a set of points from a sensor which are all within a margin of error on a 2D plane somewhere in the 3D space. How would I go on about transforming the coordinates of the points onto a 2d coordinate system, so that for example the convex hulls of the points or the distances between the points don't change?
Assuming you know the equation of the plane (otherwise you can fit it by least-square or other), construct a new coordinate frame as follows:
get the normal vector,
form the cross product with an arbitrary vector having a different direction;
form the cross product of the normal and the second vector,
normalize all three and name the new axis z, x, y.
This creates an orthonormal basis to which you will transform the points. This corresponds to a rigid transform, that preserves all distances. You can drop the z to get the orthogonal projections of the points to the plane.

How to compute 3D rotation matrix by user movement of control point

I have a projected view of a 3D scene. The 2D points are computed by multiplying the 3D points in homogenous coordinates by a view matrix (which includes a translation and rotation) and a perspective matrix. I want to allow the user to move control points which describe the three axes, and update the rotation matrix based on this.
How do I compute the new rotation matrix given a change in projected 2D coordinates, assuming rotation around the origin? Solving for the position of the end of the single axis has a large degeneracy in the set of possible, but maybe solving for rotation in the axes perpendicular to the moved axis might work.

How to approximate a 3D shape to a grid?

Given a 3D object, how do I convert it into an approximated shape in which all the sides of the object are parallel to either of the co-ordinate planes, and all the vertices have integer co-ordinates?
For example, a sphere with center at origin and a radius of 1.5 will be approximated to a cube with center at origin and side length of 2.
For another example, the line given by x = y = 0.5 will have an approximated shape as a rectangular parallelepiped with infinite length, and width and breadth as 1, and positioned such that one of its edge is along z-axis, while all the faces are along or parallel to either of x-z or y-z co-ordinate planes.
I am working with finite objects only, the above example is only meant to explain my needs.
I want an algorithm which can do this for me for any shape.
In general case you need to determine maximum and minimum shape coordinates along every axis and define minimum axis aligned integer bounding box with values rounded to larger (using Ceil) for max and rounded to smaller (using Floor) for min coordinates. For example:
XMin_Box = Floor(XMin_Shape)
XMax_Box = Ceil(XMax_Shape)
Edit:
If you need to approximate a shape with more precision, consider some kind of voxelization (3d analog of 2d rasterization)

How to transform a projected 3D rectangle into a 2D axis aligned rectangle

I have an image of a 3D rectangle (which due to the projection distortion is not a rectangle in the image). I know the all world and image coordinates of all corners of this rectangle.
What I need is to determine the world coordinate of a point in the image inside this rectangle. To do that I need to compute a transformation to unproject that rectangle to a 2D rectangle.
How can I compute that transform?
Thanks in advance
This is a special case of finding mappings between quadrilaterals that preserve straight lines. These are generally called homographic transforms. Here, one of the quads is a rectangle, so this is a popular special case. You can google these terms ("quad to quad", etc) to find explanations and code, but here are some sites for you.
Perspective Transform Estimation
a gaming forum discussion
extracting a quadrilateral image to a rectangle
Projective Warping & Mapping
ProjectiveMappings for ImageWarping by Paul Heckbert.
The math isn't particularly pleasant, but it isn't that hard either. You can also find some code from one of the above links.
If I understand you correctly, you have a 2D point in the projection of the rectangle, and you know the 3D (world) and 2D (image) coordinates of all four corners of the rectangle. The goal is to find the 3D coordinates of the unique point on the interior of the (3D, world) rectangle which projects to the given point.
(Do steps 1-3 below for both the 3D (world) coordinates, and the 2D (image) coordinates of the rectangle.)
Identify (any) one corner of the rectangle as its "origin", and call it "A", which we will treat as a vector.
Label the other vertices B, C, D, in order, so that C is diagonally opposite A.
Calculate the vectors v=AB and w=AD. These form nice local coordinates for points in the rectangle. Points in the rectangle will be of the form A+rv+sw, where r, s, are real numbers in the range [0,1]. This fact is true in world coordinates and in image coordinates. In world coordinates, v and w are orthogonal, but in image coordinates, they are not. That's ok.
Working in image coordinates, from the point (x,y) in the image of your rectangle, calculate the values of r and s. This can be done by linear algebra on the vector equations (x,y) = A+rv+sw, where only r and s are unknown. It will boil down to a 2x2 matrix equation, which you can solve generally in code using Cramer's rule. (This step will break if the determinant of the required matrix is zero. This corresponds to the case where the rectangle is seen edge-on. The solution isn't unique in that case. If that's possible, make special exception.)
Using the values of r and s from 4, compute A+rv+sw using the vectors A, v, w, for world coordinates. That's the world point on the rectangle.

Resources