Python OpenCV3でせん断(平行四辺形を作るような変換)
Python OpenCV3で画像のアフィン変換(回転とか移動とか) の続き。
というかまとめてもよかったかも。
前提
 
元画像
アフィン変換の時と同じ、この画像(slack_botter.png)を使う。
以前の例だとsource/imageディレクトリ配下に設置する。
せん断変換
回転、移動の時のアフィン変換の行列を以下のように変えるとせん断の変換ができる。
[python]
rad = 回転する角度(ラジアン)
matrix = [
[1, np.tan(rad), x軸方向に移動する距離],
[0, 1, y軸方向に移動する距離]
]
affine_matrix = np.float32(matrix)
[/python]
アフィン変換で移動、回転する
実際にアフィン変換してみる。
sourceディレクトリにshearing.pyを作成する。
[python title="shearing.pyの内容"]
# -*- coding: utf-8 -*-
import cv2
import numpy as np
if __name__ == '__main__’:
# 画像読み込み
img_src = cv2.imread("./image/slack_botter.png", 1)
# 画像サイズの取得(横, 縦)
size = tuple(np.array([img_src.shape[1], img_src.shape[0]]))
# 回転させたい角度
rad = np.pi / 4
# x軸方向に平行移動させたい距離
move_x = 0
# y軸方向に平行移動させたい距離
move_y = 0
matrix = [
[1, np.tan(rad), move_x],
[0, 1, move_y]
]
affine_matrix = np.float32(matrix)
img_afn = cv2.warpAffine(img_src, affine_matrix, size, flags=cv2.INTER_LINEAR)
# 表示
cv2.imshow("Show AFFINE Image", img_afn)
cv2.waitKey(0)
cv2.destroyAllWindows()
[/python]
実行する。
[shell]
(opencv_python)$ python shearing.py
[/shell]
せん断できた。
ディスカッション
コメント一覧
まだ、コメントがありません