LoginSignup
26
26

More than 5 years have passed since last update.

OpenCVで画像を回転させてみた

Posted at

なにをしたか?

→OpenCVで画像を回転

実装手順

  1. ライブラリのインポート
  2. 画像の読み込み
  3. 回転の中心を指定
  4. 回転処理
  5. 画像の保存

※筆者はJupyterNotebookを使用しています

実際にやってみた

1. ライブラリのインポート

import cv2

OpenCVライブラリをインポート
※OpenCVはインストール済みの状態とします

2. 画像の読み込み

img = cv2.imread('./data/pict/image1.jpg')

imread関数で画像の読み込みを行います

3. 画像の中心を指定

#高さを定義
height = img.shape[0]                         
#幅を定義
width = img.shape[1]  
#回転の中心を指定                          
center = (int(width/2), int(height/2))

高さ、幅を定義して、回転の中心を指定

4. 回転処理を実行

#回転角を指定
angle = 45.0
#スケールを指定
scale = 1.0
#getRotationMatrix2D関数を使用
trans = cv2.getRotationMatrix2D(center, angle , scale)
#アフィン変換
image2 = cv2.warpAffine(img, trans, (width,height))

getRotationMatrix2D関数、warpAffine関数を使って画像を回転

5. 画像の保存

cv2.imwrite('./data/pict/image2.jpg',image2)

imread関数で画像を保存

以上の結果から

image1(変換前)

image1.jpg

image2(変換後)

image8.jpg
となり、反時計周りに45度回転できていることが確認できました。

26
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
26