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を学びたい(Step3:画像の一部分を切り抜く)

0
Posted at

はじめに

前回の記事では、画像の中から1ピクセルを取り出して、BGRの値を確認しました。
前回の記事はこちらです。

今回はその続きとして、画像の一部分を切り抜いてみます。

画像の一部分を切り抜く

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

sample.jpg

OpenCVでは、画像の一部分だけを取り出すことができます。

import cv2

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

crop = image[100:300, 200:600]

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

image[100:300, 200:600] は、

image[y1:y2, x1:x2]

という意味です。

今回は、

  • 上から100px〜300px
  • 左から200px〜600px

の範囲を切り抜いています。

切り抜いた画像

cv2.imwrite を使うと、切り抜いた画像を保存できます。

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

実行すると、以下のような画像が作成されます。

crop.jpg

ROI

このように、画像の中から処理したい範囲だけを取り出すことを、ROI(Region of Interest)と呼びます。

必要な範囲だけを対象に画像処理したい場合に使えます。

まとめ

今回は、画像の一部分を切り抜いて保存してみました。

image[y1:y2, x1:x2]

という形で、必要な範囲だけを取り出せます。

次は画像のサイズを変更してみます。

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?