LoginSignup
1
1

More than 1 year has passed since last update.

自作画像セグメンテーションデータセットの統計について

Last updated at Posted at 2022-10-18

こちらにアノテーションツールLabelmeを使ってデータセットを作る方法を記載しました。作ったデータセットの属性を了解することは、データ処理やモデル構築に有益です。この記事では、自作画像セグメンテーションデータセットの画像ごとにあるRGB値とそれぞれが占めるピクセル値を統計する方法と全データセットにあるRGB値を入手する方法を記載します。

1.画像ごとにあるRGB値とそれぞれが占めるピクセル数

使う画像はPascal VOC2012データセットのgt画像です。
nisgel.png

from PIL import Image
import numpy as np

img = Image.open('pascal/nisgel.png').convert('RGB')
arr = np.array(img)
colours, counts = np.unique(arr.reshape(-1,3), axis=0, return_counts=1)
print(colours,counts)

このスクリプトで画像中にあるRGB値とそれぞれが占めるピクセル数を算出できます。
結果:

[[  0   0   0]
 [128   0   0]
 [224 224 192]] [223955  26602  12612]

2.全データセットのRGB値の統計

使う画像は以下の二つのPascal VOC2012データセットのgt画像です。
mojikyo45_640-2.gif mojikyo45_640-2.gif

import os
from PIL import Image
import numpy as np

base = 'pascal'
all_image = os.listdir(base)
result = []
for img in all_image:
    image = Image.open(f'{base}/{img}').convert('RGB')
    arr = np.array(image)
    colors = arr.reshape(-1, 3)
    result.extend(colors)
    colors = np.unique(result, axis=0, return_counts=1)
    result = colors[0].tolist()
print(result)

これで、全データセットのRGB値は得られます。
結果:

[[0, 0, 0], [128, 0, 0], [128, 128, 0], [224, 224, 192]]

以上です。

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