scikit imageでHue(色相), Saturation(彩度)の変更をする

scikit-image: Image processing in Python — scikit-image

Hue(色相)とSaturation(彩度)の変更方法は、RGBをHSV変換して、HとSのchannelを変更すれば良い。 どの程度変更すれば良いかがわかりにくいので、GIMPと同じになるように値を調整したものを作成した。

  • Hueの調整値: -180から180で指定
  • Saturationの調整値: -100から100で指定
import skimage.color
import numpy as np


def to_valid_image(image):
    return np.clip(image, 0, 255).astype('uint8')


def adjust_hue_saturation_lightness(
        image, hue_range=0, hue_offset=0, saturation=0, lightness=0):
    # hue is mapped to [0, 1] from [0, 360]
    if hue_offset not in range(-180, 180):
        raise ValueError('Hue should be within (-180, 180)')
    if saturation not in range(-100, 100):
        raise ValueError('Saturation should be within (-100, 100)')
    if lightness not in range(-100, 100):
        raise ValueError('Lightness should be within (-100, 100)')
    image = skimage.color.rgb2hsv(image)
    offset = ((180 + hue_offset) % 180) / 360.0
    image[:, :, 0] = image[:, :, 0] + offset
    image[:, :, 1] = image[:, :, 1] + saturation / 200.0
    image[:, :, 2] = image[:, :, 2] + lightness / 200.0
    image = skimage.color.hsv2rgb(image) * 255.0
    image = to_valid_image(image)
    return image

使い方は以下のようになる。

import skimage.io

img = sikimage.io.imread(path_to_image)
# Hue + 50
img = adjust_hue_saturation_lightness(img, 0, 50, 0, 0)
skimage.io.imshow(img)

元画像

20171204010033

Hue + 50

20171204010032

Saturation + 50

20171204010034

Value + 50

20171204010035