Python hsv to rgb是';t matplotlib上rgb_到_hsv的倒数
我试图将图像转换为hsv并返回rgb,但不知何故,我丢失了颜色信息Python hsv to rgb是';t matplotlib上rgb_到_hsv的倒数,python,image-processing,matplotlib,Python,Image Processing,Matplotlib,我试图将图像转换为hsv并返回rgb,但不知何故,我丢失了颜色信息 import matplotlib import matplotlib.pyplot as plt import matplotlib.image as mpimg 我也在shell上复制了这个问题,在导入之后写这行代码也会得到同样的结果 plt.imshow( matplotlib.colors.hsv_to_rgb( matplotlib.colors.rgb_to_hsv(mpimg.imread('go2
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
我也在shell上复制了这个问题,在导入之后写这行代码也会得到同样的结果
plt.imshow(
matplotlib.colors.hsv_to_rgb(
matplotlib.colors.rgb_to_hsv(mpimg.imread('go2.jpg'))
)
)
你能告诉我我做错了什么吗?这个问题对我来说是重复的(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
我认为报告bug的正确位置是github。edit:这只是部分解决方案,
见第页的讨论
这是一个整数除法问题numpy
对它的类型很认真,似乎不尊重来自未来导入部门的。简单的解决方法是在调用rgb_to_hsv
或修补函数之前,将rgb值转换为浮点数:
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
def rgb_to_hsv(arr):
"""
将numpy数组中的rgb值转换为hsv值
输入和输出数组应具有形状(M、N、3)
"""
arr=arr.astype('float')#0
delta=arr.ptp(-1)
s=np.类零(δ)
s[IPO]=增量[IPO]/arr_max[IPO]
ipos=delta>0
#红色是最大值
idx=(arr[:,:,0]==arr_max)&ipos
out[idx,0]=(arr[idx,1]-arr[idx,2])/delta[idx]
#格林是马克斯
idx=(arr[:,:,1]==arr_max)&ipos
out[idx,0]=2+(arr[idx,2]-arr[idx,0])/delta[idx]
#蓝色是最大值
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]=最大值
返回
谢谢,我用这个问题链接报告了这个错误。如果没有解决办法,我就认为你的答案是正确的。