###前提・実現したいこと
元の画像を回転させた画像を得たい。
得られた画像は、回転させた画像がちょうど収まるサイズで、余白は黒で埋める。
###発生している問題・エラーメッセージ
狙ったところに画像を置けない状況です。
現状、
-135度で左下
-45度で左下
0度で右ズレ
45度で右上
135度で右上
に元画像が置かれます。
###該当のソースコード
python
1import cv2 2import numpy as np 3 4def getWarpAffine(image, angle): 5 origin_height, origin_width, colors = image.shape 6 hypot = np.hypot(rows, cols) 7 rad_angle = np.deg2rad(angle) 8 rad_hypot = np.arctan(1.0 * origin_height / origin_width) 9 new_height = int(np.absolute(origin_width * np.cos(rad_angle)) + np.absolute(origin_height * np.sin(rad_angle))) 10 new_width = int(np.absolute(origin_width * np.sin(rad_angle)) + np.absolute(origin_height * np.cos(rad_angle))) 11 size = (height, width) 12 frame = np.zeros((height, width, colors), np.uint8) 13 M = cv2.getRotationMatrix2D((0, 0), angle, 1.0) 14 M[0, 2] += width * 0.5 - hypot * 0.5 * np.cos(rad_hypot + rad_angle) 15 M[1, 2] += height * 0.5 - hypot * 0.5 * np.sin(rad_hypot + rad_angle) 16 return cv2.warpAffine(image, M, size, frame, flags = cv2.INTER_LINEAR) 17 18original_image = cv2.imread('image.jpg') 19new_image = getWarpAffine(original_image, 45)
###試したこと
アフィン変換や回転行列がまだよくわかっていません。
回転の中心を元画像の中心座標にしたり、出力先画像の中心座標にしたり、平行移動させる部分でwidth, height, hypotをいろいろと置き換えてみたのですが、どうもうまくいきません。
###補足情報(言語/FW/ツール等のバージョンなど)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。