LoginSignup
9
9

More than 3 years have passed since last update.

複数の画像をnumpy配列に読み込む

Posted at

CNNなどで画像を処理する際、事前に画像データを読み込んでおく必要があります。
このとき、以下のような形式でデータを読み込む方法です。

(n_samples,height,width,channels)

コード

gistをここに置いています。

import os
import numpy as np
import glob
import cv2

image_dir = './images/'
search_pattern = '*.png'
datas = []
for image_path in glob.glob(os.path.join(image_dir,search_pattern)):
    # (height,width,channels)
    data = cv2.imread(image_path)
    # (1,height,width,channels)
    data_expanded = np.expand_dims(data,axis=0)
    datas.append(data_expanded)
# (n_samples,height,width,channels)
image_datas = np.concatenate(datas,axis=0)

(幅,高さ,チャンネル数)=(50,100,3)の画像を10枚読み込んだ場合以下のようになります。
- data.shape(100,50,3)
- data_expanded.shape:(1,100,50,3)
- image_datas.shape:(10,100,50,3)

いざというときにいつもどうやるか忘れているのでメモとして残しておきます。

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