# scale matrix and rectangle

This topic is 664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello.
I have some parallelograms like from image.

i must find a method for calculate a scale matrix that convert a fixed rectangle (that is always the same) in to these parallelogram is possible?
thanks.

##### Share on other sites

The one on the right you might have some luck with using a shear matrix, the top left one I don't think you will be able to do and the bottom one is difficult to make out what's going on.

There was a similar post here recently which might help (I couldn't find it but I will have another look) though I recall the answer being no.

##### Share on other sites

Yes, you need a perspective transformation matrix or simply a transformation matrix, not a scale matrix. I want to guess you want to transform first quad to second quad.

(P1)      (P2)

+--------+                                                   (P1')       (P2')

|           |                                                    +--------+

|           |                   =>                            /             \

|           |                                                  /               \

+--------+                                                +--------------+

(P3)     (P4)                                          (P3')           (P4')

P1 (x1, y1)          ==>   P1'        (x1', y1')

P2 (x2, y2)          ==>   P2'        (x2', y2')

P3 (x3, y3)          ==>   P3'        (x3', y3')

P4 (x4, y4)          ==>   P4'        (x4', y4')

Transformation T matrix that converts Pn to Pn' (Pn and Pn' written as a column vector with homogeneous coordinate) is:

Pn' = T * Pn

+-                -+    +-          -+    +-       -+

|    Xn' * Wn'  |    |   a  b  c |     |    Xn   |

|    Yn' * Wn'  | = |   d  e  f  | *  |    Yn   |

|    Wn'          |    |   g  h  1 |     |     1    |

+-                -+   +-           -+   +-        -+

You must have 4 mapping points known in order to create a linear equation system with 12 variables. Each Pn' has a homogenous cordinate too.

X1' * W1'  = a * X1 + b * Y1 + c

Y1' * W1'  = d * X1 + e * Y1 + f

W1'          = g * X1 + h * Y1 + 1

X2' * W2'  = a * X2 + b * Y2 + c

Y2' * W2'  = d * X2 + e * Y2 + f

W2'          = g * X2 + h * Y2 + 1

X3' * W3'  = a * X3 + b * Y3 + c

Y3' * W3'  = d * X3 + e * Y3 + f

W3'          = g * X3 + h * Y3 + 1

X4' * W4'  = a * X4 + b * Y4 + c

Y4' * W4'  = d * X4 + e * Y4 + f

W4'          = g * X4 + h * Y4 + 1

Reordering, constants on the left, variables on the right:

0  = a * X1 + b * Y1 + c - X1' * W1'

0  = d * X1 + e * Y1 + f -  Y1' * W1'

-1 = g * X1 + h * Y1 -               W1'

0  = a * X2 + b * Y2 + c - X2' * W2'

0  = d * X2 + e * Y2 + f -  Y2' * W2'

-1 = g * X2 + h * Y2 -               W2'

0  = a * X3 + b * Y3 + c - X3' * W3'

0  = d * X3 + e * Y3 + f -  Y3' * W3'

-1 = g * X3 + h * Y3 -               W3'

0  = a * X4 + b * Y4 + c - X4' * W4'

0  = d * X4 + e * Y4 + f -  Y4' * W4'

-1 = g * X4 + h * Y4 -               W4'

Constants: X1, X1', X2, X2',X3, X3',X4, X4'

Variables: a, b, c, d, e, f, g, h, W1', W2', W3', W4' (12 variables)

Number of equations: 12

Solve the system with a matricial method, for instance Cramer, and you will get the transformation matrix you are looking for. Remember, after applying transformation matrix to a point you will have to divide point coordinates by its homogeneous coordinate.

There are several libraries/frameworks that can compute perspective transformation matrix directly. One of them is Qt Framework (quite known in c++ comunity). It's easy and quick.

The code would be the following:

// Remember x1, y1, ..., x4, y4 and x1', y1', ..., x4',y4' are known values

quad_source << QPointF(x1, y1) << QPointF(x2, y2) << QPointF(x3, y3) << QPointF(x4, y4);

quad_target  << QPointF(x1', y1') << QPointF(x2', y2') << QPointF(x3', y3') << QPointF(x4', y4');

// Declaring transform matrix variable

QTransform t;

assert(ok);

// Here t contains a 3x3 transformation matrix

//ok could result false for instance if the origin quad is a convex quad and target quad is not convex. Example:

//

//                                                                           (1)

//             (1)      (2)                                                +

//              +------+                                                 /   \

//              |         |              =>                              /     \

//              +------+                                              +--+\  \

//             (3)      (4)                                        (3)   (4)\ \

//                                                                                \ \

//                                                                                  + (2)

//

// (4) is convex on first polygon and concave on second polygon. There isn't solution for this kind of situations.

If you work on Computer Vision, OpenCV has a similar method called cv::getPerspectiveTransform:

http://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#Mat%20getPerspectiveTransform%28InputArray%20src,%20InputArray%20dst%29

I'm sure there must be more APIs that can compute matrix directly without solving any linear equation system.

Regards.

Edited by jlluengo

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×