python + opencv で画像を回転 rev2

前回の記事は以下

skattun.hatenablog.jp


前回はdst画像の背景が黒くなっていたので、alphaチャンネルを使って
背景透過にしたrev2を用意

import numpy as np
import cv2

# 画像読み込み(alphaチャンネル有り)
src_mat = cv2.imread("001.png", cv2.IMREAD_UNCHANGED)
print(src_mat.shape) # 32,32,4

# 画像サイズの取得(横, 縦)
size = tuple([src_mat.shape[1], src_mat.shape[0]])

# dst 画像用意
dst_mat = np.zeros((size[1], size[0], 4), np.uint8)

# 画像の中心位置(x, y)
center = tuple([int(size[0]/2), int(size[1]/2)])

# 回転させたい角度(正の値は反時計回り)
angle = -45.0

# 拡大比率
scale = 1.0

# 回転変換行列の算出
rotation_matrix = cv2.getRotationMatrix2D(center, angle, scale)

# アフィン変換
img_dst = cv2.warpAffine(src_mat, rotation_matrix, size, dst_mat,
                         flags=cv2.INTER_LINEAR,
                         borderMode=cv2.BORDER_TRANSPARENT)

# 表示
cv2.imwrite("dst.png", img_dst)

src
f:id:skattun:20170219183609p:plain

dst
f:id:skattun:20170219213417p:plain


背景透過のアルファチャンネル付きになった。