Matlab:特定像素的 RGB 到 HSV 值不起作用

Matlab: RGB to HSV Values of specific pixels not working

我一直在尝试使用以下代码获取几个 RGB 像素的 HSV 值

im = imread('peppers.png'); %// example image
c = [12 146 410]; %// column coordinates of desired pixels
r = [104 156 129]; %// row coordinates of desired pixels
pixels = impixel(im,c,r); %// rgb values of pixels
hsv = rgb2hsv(pixels); %// convert to hsv
hue = hsv(:,1); %// hue is first column of hsv

但是我得到一个错误,Valid colormaps cannot have values outside the range [0,1]

有人可以告诉我我的代码有什么问题吗?

RGB 颜色图的范围必须为 [0,1](双精度),rgb2hsv 才能工作;你的问题是像素矩阵颜色范围从 0 到 255 (int)。你必须找到一种方法来解决这个问题。 im2double 可能会有帮助。

详情见the rgb2hsv documentation

此外,我建议使用 imfinfo 来了解您正在处理的图像类型,然后在您的代码中采取相应的行动。