This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Convert RGB colors to HSV


hsv = rgb2hsv(rgb)



hsv = rgb2hsv(rgb) converts RGB values to the appropriate hue, saturation, and value (HSV) coordinates. rgb can be a p-by-3 colormap array or an m-by-n-by-3 image array. hsv is the same size as rgb.


collapse all

Get a down-sampled version of the prism colormap.

rgb = prism(6)
rgb = 

    1.0000         0         0
    1.0000    0.5000         0
    1.0000    1.0000         0
         0    1.0000         0
         0         0    1.0000
    0.6667         0    1.0000

Convert the RGB values in the colormap to HSV.

hsv = rgb2hsv(rgb)
hsv = 

         0    1.0000    1.0000
    0.0833    1.0000    1.0000
    0.1667    1.0000    1.0000
    0.3333    1.0000    1.0000
    0.6667    1.0000    1.0000
    0.7778    1.0000    1.0000

Create a 2-by-2 truecolor image.

rgb(:,:,1) = [1 1; 0 .5];
rgb(:,:,2) = [0 1; 0 .5];
rgb(:,:,3) = [0 0; 1 .5];

Convert the image to an HSV array.

hsv = rgb2hsv(rgb)
hsv = 
hsv(:,:,1) =

         0    0.1667
    0.6667         0

hsv(:,:,2) =

     1     1
     1     0

hsv(:,:,3) =

    1.0000    1.0000
    1.0000    0.5000

Input Arguments

collapse all

RGB values to convert, specified as a p-by-3 array or an m-by-n-by-3 image array.

If rgb is a p-by-3 array, then it must be of type double, and each row must contain an RGB triplet. An RGB triplet is a three-element row vector whose values specify the red, green, and blue components of a color, respectively. The values must be in the range [0, 1].

If rgb is an m-by-n-by-3 image array, then it can be of type double, single, uint8, or uint16. The third dimension of the array specifies the red, green, or blue intensity at pixel (i,j):

  • rgb(i,j,1) specifies the red intensity.

  • rgb(i,j,2) specifies the green intensity.

  • rgb(i,j,3) specifies the blue intensity.

Data Types: single | double | uint8 | uint16

Output Arguments

collapse all

HSV values, returned as returned as an array that is the same size as rgb: a p-by-3 array or an m-by-n-by-3 array. The values are in the range [0, 1], and their type depends on the type of rgb:

  • If rgb is of type single, then hsv is of type single.

  • Otherwise, hsv is of type double.

The attributes (hue, saturation, and value) are described in the following table.

AttributeDescription Location in hsv Array
HueValue from 0 to 1 that corresponds to the color’s position on a color wheel. As hue increases from 0 to 1, the color transitions from red to orange, yellow, green, cyan, blue, magenta, and finally back to red.
  • hsv(:,1) when hsv is p-by-3

  • hsv(:,:,1) when hsv is m-by-n-by-3

SaturationAmount of hue or departure from neutral. 0 indicates a neutral shade, whereas 1 indicates maximum saturation.
  • hsv(:,2) when hsv is p-by-3

  • hsv(:,:,2) when hsv is m-by-n-by-3


Maximum value among the red, green, and blue components of a specific color.

  • hsv(:,3) when hsv is p-by-3

  • hsv(:,:,3) when hsv is m-by-n-by-3


[1] Smith, A. R. “Color Gamut Transform Pairs”. SIGGRAPH 78 Conference Proceedings. 1978, pp. 12–19.

See Also

Introduced before R2006a

Was this topic helpful?