Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hsv_to_rgb isn't the inverse of rgb_to_hsv on matplotlib

I tried to convert an image to hsv and back to rgb, but somehow I lost color information.

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

And I also replicated the problem on shell too, just writing this line after importing also gives the same result.

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

Can you tell me what am I doing wrong?

like image 633
utdemir Avatar asked Nov 01 '13 17:11

utdemir


2 Answers

edit: this is only a partial solution,

see discussion at https://github.com/matplotlib/matplotlib/pull/2569

This is an integer division issue. numpy is serious about it's types and seems to not respect from __future__ import division. The simple work around is to convert your rgb values to floats before calling rgb_to_hsv or patch the function as such:

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
like image 75
tacaswell Avatar answered Oct 19 '22 02:10

tacaswell


This problem is reproducible for me (matplotlib 1.3.0). It looks like a bug to me. The issue seems to be that in the rgb_to_hsv step, the saturation is being dropped to zero. At least for most colours:

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

I think the correct place to report bugs is on the github issue tracker.

like image 41
spinup Avatar answered Oct 19 '22 04:10

spinup