6

画像を hsv に変換してから rgb に戻そうとしましたが、どういうわけか色情報が失われました。

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

また、シェルでも問題を再現しました。インポート後にこの行を書くだけでも同じ結果が得られます。

plt.imshow(
  matplotlib.colors.hsv_to_rgb(
    matplotlib.colors.rgb_to_hsv(mpimg.imread('go2.jpg'))
  )
)

私が間違っていることを教えてもらえますか?

4

2 に答える 2

4

編集:これは部分的な解決策にすぎません。

https://github.com/matplotlib/matplotlib/pull/2569のディスカッションを参照してください。

これは整数除算の問題です。 numpyはそのタイプについて真剣であり、尊重していないようfrom __future__ import divisionです。簡単な回避策は、rgb 値を float に変換してからrgb_to_hsv、関数を呼び出すかパッチを適用することです。

def rgb_to_hsv(arr):
    """
    convert rgb values in a numpy array to hsv values
    input and output arrays should have shape (M,N,3)
    """
    arr = arr.astype('float')  # <- add this line
    out = np.zeros(arr.shape, dtype=np.float)
    arr_max = arr.max(-1)
    ipos = arr_max > 0
    delta = arr.ptp(-1)
    s = np.zeros_like(delta)
    s[ipos] = delta[ipos] / arr_max[ipos]
    ipos = delta > 0
    # red is max
    idx = (arr[:, :, 0] == arr_max) & ipos
    out[idx, 0] = (arr[idx, 1] - arr[idx, 2]) / delta[idx]
    # green is max
    idx = (arr[:, :, 1] == arr_max) & ipos
    out[idx, 0] = 2. + (arr[idx, 2] - arr[idx, 0]) / delta[idx]
    # blue is max
    idx = (arr[:, :, 2] == arr_max) & ipos
    out[idx, 0] = 4. + (arr[idx, 0] - arr[idx, 1]) / delta[idx]
    out[:, :, 0] = (out[:, :, 0] / 6.0) % 1.0
    out[:, :, 1] = s
    out[:, :, 2] = arr_max
    return out
于 2013-11-02T16:32:03.977 に答える
2

この問題は私にとって再現可能です(matplotlib 1.3.0)。私にはバグのように見えます。問題は、rgb_to_hsvステップで彩度がゼロに落ちていることです。少なくともほとんどの色について:

import numpy as np
darkgreen = np.array([[[0, 100, 0]]], dtype='uint8')
matplotlib.colors.rgb_to_hsv(darkgreen)                  # [0.33, 1., 100.], okay so far
darkgreen2 = np.array([[[10, 100, 10]]], dtype='uint8')  # very similar colour
matplotlib.colors.rgb_to_hsv(darkgreen2)                 # [0.33, 0., 100.], S=0 means this is a shade of gray

バグを報告する正しい場所は github issue trackerだと思います。

于 2013-11-01T19:55:34.920 に答える