LoginSignup
2
1

More than 3 years have passed since last update.

【Python】複数の画像の代表値をとる【Numpy】

Posted at

目的

複数の画像の各ピクセルの代表値を使って画像を生成する。
例えば10枚の画像の平均画像を作りたい。

準備

ライブラリのImport

import numpy as np
from scipy import stats

画像

10枚のRGB画像。
10枚の平均画像、中央値画像、最頻値画像をそれぞれ生成する。

imgs.shape    # (10, 128, 128, 3)

平均

x.shapeでaxisに設定したい軸を確認できる。

img_mean = np.mean(imgs, axis=0)
img_mean.shape    # (128, 128, 3)

中央値

img_median = np.median(imgs, axis=0)
img_median.shape    # (128, 128, 3)

最頻値

numpyに最頻値を求めるライブラリがないのでscipy。
他にいい方法あるかも。

# 返値がmodeとcountの2つあるのでmodeだけ拾う
img_mode = stats.mode(imgs, axis=0)[0]

# 形を他の代表値画像と合わせる
img_mode.shape    # (1, 128, 128, 3)
img_mode = img_mode.reshape(128, 128, 3)

img_mode.shape    # (128, 128, 3)
2
1
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
2
1