Ask Your Question
0

OpenCV cv2 perspective transformation matrix multiplication

asked 2013-10-18 22:24:44 -0600

sjm gravatar image

updated 2013-10-19 13:59:37 -0600

Moster gravatar image

I am trying to combine a series of warpPerspective into one by combining the matrices generated by getPerspectiveTransform. If I multiply them together using cv2.multiply the resulting matrix doesn’t work. Example for just two transformations:

src = np.array([[0,0],[0,480],[640,480],[640,0]],np.float32)

dst = np.array([[-97,-718],[230,472],[421,472],[927,-717]],np.float32)

retval = cv2.getPerspectiveTransform(src, dst);

test = cv2.multiply(retval.copy(),retval.copy())

img1 = cv2.warpPerspective(img1,test,(640,480))

img2 = cv2.warpPerspective(img2,retval,(640,480))

img2 = cv2.warpPerspective(img2,retval,(640,480))

Why aren't img1 and img2 the same? How do I combine the perspective transformation matrices?

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2013-10-21 05:03:33 -0600

sjm gravatar image

You cannot multiply a 3x3 matrix. You must make the matrix 4x4:

[x,x,x,0]

[x,x,x,0]

[x,x,x,0]

[0,0,0,1]

you then multiply and then set back to a 3x3 matrix. Watch out for numpy's format for selecting the individual parts, use m.item. Don't use np.resize, it messes the matrix up.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-10-18 22:24:44 -0600

Seen: 1,140 times

Last updated: Oct 21 '13