Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCv warpPerspective meaning of elements of homography

I have a question regarding the meaning of the elements from an projective transformation matrix e.g. in an homography used by OpenCv warpPerspective.

I know the basic of an affin transformation, but here I'm more interested in the projective transformation, meaning in the below shown matrix the elements A31 and A32:

A11 A12 A13
A21 A22 A23
A31 A32  1

I played around with the values a bit which means having a fixed numbers for all other element. Meaning:

 1   0   0 
 0   1   0
A31 A32  1

to have just the projective elements.

But what exactly causing the elements A31 and A32 ? Like A13 and A23 are responsible for the horizontal and vertical translation.

Is there an simple explanation for this two elements? Like having a positive value means ...., having a negativ value meaning ... . S.th. like that.

Hope anyone can help me.

like image 802
flor1an Avatar asked Dec 13 '22 21:12

flor1an


2 Answers

Newton's descriptions are correct, but it might be helpful to actually see the transformations to understand what's going on, and how they might work together with other values in the transformation matrix to make a bit more sense. I'll give some python/OpenCV examples with animations to show what these values do.

import numpy as np
import cv2
img = cv2.imread('img1.png')
h, w = img.shape[:2]

# initializations
max_m20 = 2e-3
nsteps = 50
M = np.eye(3)

So here I'm setting the transformation matrix to be the identity (no transformation). We want to see the effect of changing the element at (2, 0) in the transformation matrix M, so we'll animate by looping through nsteps linearly spaced between 0 to max_m20.

for m20 in np.linspace(0, max_m20, nsteps):
    M[2, 0] = m20
    warped = cv2.warpPerspective(img, M, (w, h))
    cv2.imshow('warped', warped)
    k = cv2.waitKey(1)
    if k == ord('q') & 0xFF:
        break

I applied this on an image taken from Oxford's Visual Geometry Group.

m20

So indeed, we can see that this is similar to either rotating your camera around a point that is aligned with the left edge of the image, or rotating the image itself around an axis. However, it is a little different than that. Note that the top edge stays along the top the whole time, which is a little strange. Instead of we rotate around an axis like above, we would imagine that the top edge would start to come down on the right edge too. Like this:

m20m10

Well, if you're thinking about transformations, one easy way to get this transformation is to take the transformation above, and add some skew distortion so that the right top side is being pushed down as that bottom right corner is being pushed up. And that's actually exactly how this view was created:

M = np.eye(3)
max_m20 = 2e-3
max_m10 = 0.6
for m20, m10 in zip(np.linspace(0, max_m20, nsteps), np.linspace(0, max_m10, nsteps)):
    M[2, 0] = m20
    M[1, 0] = m10
    warped = cv2.warpPerspective(img, M, (w, h))
    cv2.imshow('warped', warped)
    k = cv2.waitKey(1)
    if k == ord('q') & 0xFF:
        break

So the right way to think about the perspective in these matrices is, IMO, with the skew entries and the last row together. Those are the two places in the homography matrix where angles actually get modified*; otherwise, it's just rotation, scaling, and translation---all of which are angle preserving.

*Note: Actually, angles can be changed in one more way that I didn't mention. Affine transformations allow for non-uniform scaling, which means you can stretch a shape in width and not in height or vice-versa, which would also change the angles. Imagine if you had a triangle and stretched it only in width; the angles would change. So it turns out that non-uniform scaling (i.e. when the first and middle element of the transformation matrix are different values) can also modify angles in addition to the perspective change and shearing distortions.

Note that in these examples, the same applies to the second entry in the last row with the other skew location; the only difference is it happens at the top instead of the left side. Negative values in both cases is akin to rotating the plane along that axis towards, instead of farther away from, the camera.

like image 102
alkasm Avatar answered Dec 16 '22 11:12

alkasm


The 3x1 ,3x2 elements of homography matrix change the plane of the image. Thats the difference between Affine and Homography matrices. For instance consider this- The A31 changes the plane of your image along the left edge. Its like sticking your image to a stick like a flag and rotating. The positive is clock wise and the negative is reverse. The other element does the same from the top edge. But together, they set a plane for your image. That's the simplest way i could put it.

like image 23
I.Newton Avatar answered Dec 16 '22 10:12

I.Newton