plant-raspberrypi3のブログ

ラズベリーパイ3とPythonに挑戦して、植物を愛でたり画像を加工したりします。最近はscikit-imageの勉強してます。

scikit-imageのお勉強 第5回 exposureモジュール ~露出補正~

scikit-imageの便利機能の備忘録シリーズ。

これまでの回はコチラ。

第1回 dataモジュールとioモジュール ~画像の入出力~

第2回 colorモジュール ~色空間~

第3回 drawモジュール ~図形の描画~

第4回 transformモジュール ~拡大縮小・回転・アフィン変換~

今回は第5回です。exposureモジュールについてメモしていきます。

exposureモジュール

露出補正についての機能を含むモジュールです。

公式ページ:Module: exposure — skimage v0.14.0 docs

以下の関数とその効果をざっと紹介します。

  • exposure.equalize_hist
  • exposure.equalize_adapthist
  • exposure.rescale_intensity
  • exposure.adjust_gamma
  • exposure.adjust_sigmoid
  • exposure.adjust_log

まずは準備。

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

from skimage import exposure, data, color

def image_show(img):
    try:
        h,w,c = img.shape
        plt.figure(figsize=[float(w/100),float(h/100)])
        plt.title(f'w={w}, h={h}')
        plt.imshow(img)
    except ValueError:
        h,w = img.shape
        plt.figure(figsize=[float(w/100),float(h/100)])
        plt.title(f'w={w}, h={h}')
        plt.imshow(img,cmap='gray')
    plt.xticks([])
    plt.yticks([])
    plt.show()

rocket = data.rocket()
image_show(rocket)
rocket_g = color.rgb2gray(rocket)
image_show(rocket_g)

・元画像

f:id:plant-raspberrypi3:20180905122655p:plain

・元画像(グレー)

f:id:plant-raspberrypi3:20180905122714p:plain

はじめの3つを実行

exposure.equalize_histexposure.equalize_adapthistはカラー画像で実行するとエラーがでました。

・code

image_show(rocket_g)

hist_img = exposure.equalize_hist(rocket_g)
image_show(hist_img)

hist_img = exposure.equalize_adapthist(rocket_g,kernel_size=50)
image_show(hist_img)

hist_img = exposure.rescale_intensity(rocket_g, in_range=(0, 0.5))
image_show(hist_img)

hist_img = exposure.rescale_intensity(rocket_g, out_range=(0, 0.5))
image_show(hist_img)

image_show(rocket)

hist_img = exposure.rescale_intensity(rocket, in_range=(0, 127))
image_show(hist_img)

hist_img = exposure.rescale_intensity(rocket, out_range=(0, 127))
image_show(hist_img)

・元画像(グレー)

f:id:plant-raspberrypi3:20180905123051p:plain

・exposure.equalize_hist

f:id:plant-raspberrypi3:20180905123057p:plain

・exposure.equalize_adapthist

f:id:plant-raspberrypi3:20180905123106p:plain

・exposure.rescale_intensity(in_range=(0, 0.5))

f:id:plant-raspberrypi3:20180905123115p:plain

・exposure.rescale_intensity(out_range=(0, 0.5))

f:id:plant-raspberrypi3:20180905123123p:plain

・元画像

f:id:plant-raspberrypi3:20180905123134p:plain

・exposure.rescale_intensity(in_range=(0, 0.5))

f:id:plant-raspberrypi3:20180905123142p:plain

・exposure.rescale_intensity(out_range=(0, 0.5))

f:id:plant-raspberrypi3:20180905123151p:plain

exposure.adjust_gamma

gammaの値を色々変えて効果の違いを見てみます。

・code

image_show(rocket)

for i in np.arange(1.2,1.7,0.2):
    print(f'gamma={i:.1f}')
    hist_img = exposure.adjust_gamma(rocket,gamma=i)
    image_show(hist_img)

・元画像

f:id:plant-raspberrypi3:20180905123421p:plain

・gamma = 1.2

f:id:plant-raspberrypi3:20180905123504p:plain

・gamma = 1.4

f:id:plant-raspberrypi3:20180905123511p:plain

・gamma = 1.6

f:id:plant-raspberrypi3:20180905123520p:plain

exposure.adjust_sigmoid

cutoffとgainをいじります。

・code

image_show(rocket)

for i in np.arange(2,9,4):
    print(f'cutoff=0,gain={i:.1f}')
    hist_img = exposure.adjust_sigmoid(rocket,cutoff=0,gain=i)
    image_show(hist_img)
    print(f'cutoff=0.5,gain={i:.1f}')
    hist_img = exposure.adjust_sigmoid(rocket,cutoff=0.5,gain=i)
    image_show(hist_img)
    print(f'cutoff=1.5,gain={i:.1f}')
    hist_img = exposure.adjust_sigmoid(rocket,cutoff=1.0,gain=i)
    image_show(hist_img)

・元画像

f:id:plant-raspberrypi3:20180905123741p:plain

・cutoff=0,gain=2.0

f:id:plant-raspberrypi3:20180905123746p:plain

・cutoff=0.5,gain=2.0

f:id:plant-raspberrypi3:20180905123751p:plain

・cutoff=1.5,gain=2.0

f:id:plant-raspberrypi3:20180905123804p:plain

・cutoff=0,gain=6.0

f:id:plant-raspberrypi3:20180905123818p:plain

・cutoff=0.5,gain=6.0

f:id:plant-raspberrypi3:20180905124040p:plain

・cutoff=1.5,gain=6.0

f:id:plant-raspberrypi3:20180905124047p:plain

exposure.adjust_log

gainを調整してみます。

image_show(rocket)

for i in np.arange(0.2,2,0.4):
    print(f'gain={i:.1f}')
    hist_img = exposure.adjust_log(rocket,gain=i)
    image_show(hist_img)

・元画像

f:id:plant-raspberrypi3:20180905124229p:plain

・gain=0.2

f:id:plant-raspberrypi3:20180905124235p:plain

・gain=0.6

f:id:plant-raspberrypi3:20180905124241p:plain

・gain=1.0

f:id:plant-raspberrypi3:20180905124249p:plain

・gain=1.4

f:id:plant-raspberrypi3:20180905124257p:plain

・gain=1.8

f:id:plant-raspberrypi3:20180905124303p:plain

各々の関数の雰囲気を探ってみました。

解説なしでスミマセン。