# Composing perspective transforms

I have 2 matrices, such that:

v1 = cv2.perspectiveTransform(v0, M0)
v2 = cv2.perspectiveTransform(v1, M1)


How can I get matrix M, such that:

v2 = cv2.perspectiveTransform(v0, M)


M0*M1 or M1*M0 doesn’t work.

make sure you are doing a matrix multiplication, not an element-wise multiplication.

mind the order: M_1 \cdot (M_0 \cdot v) = (M_1 \cdot M_0) \cdot v \neq (M_0 \cdot M_1) \cdot v
i.e. matrix multiplication doesn’t commute.

in Python, with numpy, you would say M = M1 @ M0. just M1 * M0 of numpy arrays only does an element-wise multiplication. the @-operator is fairly new in python, and was introduced specifically so numpy arrays could implement it and give it this purpose. it’s not to be confused with decorators, which are applied with a prefixed @.

in C++, there’s operator*() (infix multiplication) for matrix multiplication… while element-wise multiplication seems to require cv::Mat::mul()

1 Like

Thanks a lot. Just starting with Numpy.