Help us understand the problem. What is going on with this article?

# Opencv PerspeciveTransformの罠

More than 1 year has passed since last update.

Python2系やOpencv2系は記法が違ったりするので注意。

Pythonを使って透視変換をする時，画像の変換には次の関数を使う。

```dst= cv2.warpPerspective(src, Matrix, (rows,cols), flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, fillval=(0, 0, 0, 0))
```

## 点群の座標変換

```cv2.perspectiveTransform(points,perspective)
```

のように記述する。点群の方が先にくるなんてなんか違和感がある。

さらに違和感があるのは記述法だ。次の例を見て欲しい。

```import cv2
import numpy as np

a = np.array([[1, 2], [4, 5], [7, 8]], dtype='float32')
h = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32')

pointsOut = cv2.perspectiveTransform(a, h)
```

```cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/matmul.cpp:1916:
error: (-215) scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)
in function perspectiveTransform
```

ということで正解はこちら

```import cv2
import numpy as np

a = np.array([[1, 2], [4, 5], [7, 8]], dtype='float32')
h = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='float32')
a2 = np.array([a])

pointsOut = cv2.perspectiveTransform(a2, h)
```

```a=
array([[ 1.,  2.],
[ 4.,  5.],
[ 7.,  8.]], dtype=float32)

a2=
array([[[ 1.,  2.],
[ 4.,  5.],
[ 7.,  8.]]], dtype=float32)
```

なんとなくで公式documentを読んでいてもダメなのだ。

Why do not you register as a user and use Qiita more conveniently?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away