LoginSignup
0
0

More than 1 year has passed since last update.

OpenCVで画像を読み込んだときのsizeとshapeの意味

Last updated at Posted at 2021-12-05

気になったこと

OpenCVで読み込んだ画像から、サイズや幅を出力するときにsizeやshapeを使うと思いますが、実際の画像ファイルとの関係が気になったので確認してみました。

確認した内容

適当な画像ファイル「a.jpg」のファイルをimreadで読み込んで、いろいろ表示させてみました。

import cv2

path = "C:\\XXXXXXXXXXXXXXX\\a.jpg"
img = cv2.imread(path)

print(type(img))
print(img.size)
print(img.shape)
print("高さ " + str(img.shape[0]))
print("幅   " + str(img.shape[1]))
print("色   " + str(img.shape[2]))

実行結果

<class 'numpy.ndarray'>
15116544
(1944, 2592, 3)
高さ 1944
幅   2592
色   3

image.png

確認に使用した「a.jpg」のプロパティはこのようになってました。
つまり、cv2で読み込むとndarray型になって、shape[0]で高さ、shape[1]で幅、shape[2]はビットの深さ(24ビット→3バイト)。
shape[0] × shape[1] × shape[2]の結果がsizeで出力される結果ということみたいです。

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