# Composing perspective transforms

**URL:** <https://forum.opencv.org/t/composing-perspective-transforms/5112>\
**Category:** Python\
**Tags:** calib3d, numpy, core, cg, math\
**Created:** [September 8, 2021, 10:06pm UTC](https://forum.opencv.org/t/composing-perspective-transforms/5112 "2021-09-08T22:06:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pauljurczak](https://forum.opencv.org/u/pauljurczak)\
**Post date:** [September 8, 2021, 10:06pm UTC](https://forum.opencv.org/t/composing-perspective-transforms/5112/1 "2021-09-08T22:06:24Z")

</div>

I have 2 matrices, such that:

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

```

How can I get matrix `M`, such that:

```auto
v2 = cv2.perspectiveTransform(v0, M)

```

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 8, 2021, 10:41pm UTC](https://forum.opencv.org/t/composing-perspective-transforms/5112/2 "2021-09-08T22:41:41Z")

</div>

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()`

---

<div class="post-metadata">

**Author:** ![pauljurczak](https://avatars.discourse-cdn.com/v4/letter/p/87869e/32.png) [@pauljurczak](https://forum.opencv.org/u/pauljurczak)\
**Post date:** [September 9, 2021, 12:44am UTC](https://forum.opencv.org/t/composing-perspective-transforms/5112/3 "2021-09-09T00:44:12Z")

</div>

Thanks a lot. Just starting with Numpy. 🙂
