Ask Your Question
0

How to get the scale factor of getPerspectiveTransform in opencv?

asked 2015-12-20 02:21:22 -0600

mrmuto21245 gravatar image

updated 2015-12-23 06:10:50 -0600

I have image A and i want to get the bird-eye's view of image A. So I used getPerspectiveTransform method to get the transform matrix. The output result is 3x3 matrix. See my code. In my case i want to know the scale factor of the 3x3 matrix. I have looked the opencv document, but i cannot find detail of the transform matrix and i don't know how to get the scale. Also i have read some paper, the paper said we can get scaling, shearing and ratotion from a11, a12, a21, a22. See the pic. So how can i get the scale factor.Can you give me some advice? Thank you!

Points[0] = Point2f(..., ...);
Points[1] = Point2f(..., ...);
Points[2] = Point2f(..., ...);
Points[3] = Point2f(..., ...);

dst[0] = Point2f(..., ...);
dst[1] = Point2f(..., ...);
dst[2] = Point2f(..., ...);
dst[3] = Point2f(..., ...);
Mat trans = getPerspectiveTransform(gpsPoints, dst);//I want to know the scale of trans
warpPerspective(A, B, trans, img.size());

image description

image description

I use getPerspectiveTransform(trapezoids1, rectangle),from left to right. Here are two sample,trapezoids1 to rectangle and trapezoids2 to rectangle.The rectangle is the bird-eye's view. You know we can change the rectangle length but keep height:width(2:3). The result also is bird-eye's view.My problem is the small warp into 10 times large rectange, it should resize image to high resolutions and may lose pixels.So i think we should resize the rectangle first, then warp the trapezoids to the small size rectangle. To resize the rectangle, i need to get the scale first.This is my thought.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-12-20 23:02:43 -0600

Tetragramm gravatar image

As a simpler form, the Affine Transform is a special case of the Perspective Transform, with the bottom row as [0,0,1].

If you look here you can see how it decomposes into scale, rotation, and translation. The analogy of scale isn't quite perfect with a Perspective Transform, since different parts of the image have different scale factors, but as an approximation, just pretend it's an affine matrix and decompose it. This will fail if there is a large non-planar component.

Or you could go the stupid simple route and take the distance between each of the four points, and each of the others, calculate the 6 scale factors, and average them. If you're trying to find the size of image you need to warp into, taking the four corners of the original image, putting all four through the perspective transform, and finding the bounding box will do it.

It rather depends on what you want to do with it.

edit flag offensive delete link more

Comments

Thank you for your comment.In my case, i want to get the bird-eye's view of image when the target in different size. For example,the target is trapezium and i need to convert it to rectangle.So i set four points of rectangle and these points position has been fixed. But i think when the camera height changed,the trapezium size in the image will be changed. As a result, different size trapezium will convert into a same size rectangle. The details of rectangle will have little different,right? So, i think can i
change them into same scale. But i don't konw how to set into same scale. And you said"different parts of the image have different scale factors". I have wondered is my idea right or not. Can you give me some advice?Detaisl i have update in my question

mrmuto21245 gravatar imagemrmuto21245 ( 2015-12-21 22:20:47 -0600 )edit

Ah, I think I see what you mean. A different problem.

What you want to do, is create a Mat of the size you want, I'll assume it's 400x300 pixels, just because it's easier to explain with numbers. So you already know the corners of the area in the original image, that is, the corners of the trapezoid. You want to find the transform to turn that into a rectangle of size 400x300. So use the function getPerspectiveTransform to find the transformation between the corners of the trapezoid and the corners of the rectangle image (in this case (0,0) (400,0) (0,300) (400,300)).

Now you have all of the information needed to warp the image. The output matrix encodes all of the rotation, translation, scale and warping that needs to be done. Just feed it into the warpPerspective function.

Tetragramm gravatar imageTetragramm ( 2015-12-22 15:02:17 -0600 )edit

To be clear, this will always warp whatever corners you pick for the trapezoid into the same size matrix. If you order the points wrong it will mirror or flip the image. If you want to change the size of the rectangle you're warping into, you change the corners, and of course, the size of the Mat.

Looking at your bit of code, I notice you call it gpspoints. I should point out for completeness, that these inputs should be the locations of the corners of the trapezoid in image (X,Y) pixels, not lat-lon.

If you still need to extract numeric values for scale factor, in this problem, you can get a good number in the Y direction, it's the height of the rectangle, divided by the (top of the trapezoid minus the bottom). The X scale varies with the vertical location, so you'll need to average

Tetragramm gravatar imageTetragramm ( 2015-12-22 15:12:16 -0600 )edit

Thank you for your comment.But i think that the result from height of the rectangle, divided by the (top of the trapezoid minus the bottom) is not very accruate. Is my idea not good? Using the matrix got from getPerspectiveTransform method and decompose it into scale. Then using the scale to change the Mat size.I have updated my question to show more details.

mrmuto21245 gravatar imagemrmuto21245 ( 2015-12-23 06:17:33 -0600 )edit

Well, the problem is you don't have just one scale factor. Looking at your examples, the top one has a vertical scale of 0.5, and a horizontal scale that ranges from 2 to 0.8, between the top and bottom of the trapezoid.

I know this without looking at the matrix, because I know that the top line of the trapezoid is equal to the top line of the rectangle, and the same for all the other lines.

So, as a suggestion, take the length of the bottom side of the trapezoid, and make that the width of your rectangle. Then take twice the height, and make that the height of the rectangle.

Alternatively, if you know the size of the rectangle the trapezoid makes on the ground, you can make your rectangle in that aspect ratio, with the width equal to the width of the bottom of the trapezoid.

Tetragramm gravatar imageTetragramm ( 2015-12-23 08:39:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-20 02:21:22 -0600

Seen: 4,622 times

Last updated: Dec 23 '15