0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OpenCVを学びたい(Step4:画像のサイズを変更する)

0
Posted at

はじめに

前回の記事では、画像の一部分を切り抜く方法を試しました。

前回の記事はこちらです。

今回はその続きとして、画像のサイズを変更してみます。

画像サイズを変更する

今回は以下の画像を使います。

sample.jpg

OpenCVでは、cv2.resize を使って画像サイズを変更できます。

import cv2

image = cv2.imread("sample.jpg")

print("元画像:", image.shape)

resized = cv2.resize(image, (600, 250))

print("変更後:", resized.shape)

cv2.imwrite("resized.jpg", resized)

実行します。

python main.py

cv2.resize では、以下のようにサイズを指定します。

cv2.resize(image, (, 高さ))

今回は、

cv2.resize(image, (600, 250))

としているため、

  • 幅: 600px
  • 高さ: 250px

に変更しています。

サイズ変更後の画像

cv2.imwrite で、サイズ変更後の画像を保存します。

cv2.imwrite("resized.jpg", resized)

変更後の画像はこちらです。

resized.jpg

注意点

image.shapecv2.resize では、幅と高さの順番が異なります。

image.shape
→ (高さ, 幅, チャンネル数)

cv2.resize
→ (幅, 高さ)

ここは少し混乱しやすいポイントです。

まとめ

今回は、cv2.resize を使って画像サイズを変更してみました。

画像を小さくして処理量を減らしたい場合などに利用できます。

次は、画像をグレースケールに変換してみます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?