In MATLAB, convert RGB to HSV with the rgb2hsv function: use hsv = rgb2hsv(rgb); for one color and hsvImage = rgb2hsv(rgbImage); for an RGB image array.

The main issue is the numeric scale. MATLAB works with normalized hue, saturation, and value when its input is normalized, while many color tools display hue in degrees and saturation and value as percentages. Converting the values into the representation expected by the destination prevents a correct HSV color from being interpreted incorrectly.

For a single 8-bit RGB sample, the RGB to HSV Converter can also serve as an independent scale check. It is not a replacement for MATLAB's image-processing function; it is intended for checking one integer RGB triplet at a time.

convert rgb to hsv matlab
Convert RGB to HSV in MATLAB: Step-by-Step Code

What MATLAB returns for RGB input

MathWorks' rgb2hsv function accepts a three-element RGB vector for one color or an RGB image array with three color channels. In the workflow below, the input is converted to double values from 0 to 1. The returned vector follows the same component order:

  • hsv(1) is hue as a fraction.
  • hsv(2) is saturation as a fraction.
  • hsv(3) is value as a fraction.

That means MATLAB can represent an entire hue circle with values from 0 up to, but not including, 1. To express the same components as degrees and percentages, use hsv(1) × 360 for hue and multiply saturation and value by 100.

HSV value is the largest normalized RGB channel. It is not the same measurement as HSL lightness, which is based on the midpoint between the smallest and largest channels, and it is not a direct measurement of perceived brightness.

Convert one RGB color in MATLAB

Use this sequence when starting with whole-number red, green, and blue channels such as those read from an 8-bit image or supplied by another application.

  1. Store the channels in RGB order. Assign the source values to a three-element vector: rgb255 = [R G B];. The positions represent red, green, and blue respectively; they are not percentages at this stage.
  2. Convert the values to normalized doubles. Use rgb = double(rgb255) / 255;. Dividing by 255 maps each 8-bit channel to the 0–1 range. If the source already contains normalized doubles, use them directly instead of dividing again.
  3. Call rgb2hsv. Run hsv = rgb2hsv(rgb);. With normalized input, the returned hue, saturation, and value are also normalized fractions.
  4. Read the three components. Access hsv(1) for hue, hsv(2) for saturation, and hsv(3) for value. Keep the component order intact when passing the vector to another function.
  5. Convert only if the destination requires another scale. Calculate hueDegrees = hsv(1) * 360;, saturationPercent = hsv(2) * 100;, and valuePercent = hsv(3) * 100;. If the destination accepts MATLAB-style fractions, keep the original hsv vector.

For one worked example, an 8-bit red triplet follows this calculation:

  • Formula: rgb = double(rgb255) / 255 and hsv = rgb2hsv(rgb).
  • Substitution: rgb255 = [255 0 0] becomes rgb = [1 0 0].
  • Result: MATLAB returns hsv = [0 1 1], corresponding to a hue of 0 degrees, saturation of 100%, and value of 100%.

Process RGB image pixels

MATLAB can apply the same conversion to every pixel in a three-channel RGB image. Keeping the row and column positions unchanged makes it possible to inspect an HSV result at the same location as the source pixel.

  1. Read the image as RGB data. For a three-channel image, use imageData = imread('sample.png');.
  2. Normalize the full image when the source uses 8-bit values. Run rgbImage = double(imageData) / 255;. This makes the scale of every channel explicit.
  3. Convert the array. Run hsvImage = rgb2hsv(rgbImage);. The result retains the image's row and column layout, with hue, saturation, and value stored along its third dimension.
  4. Extract one pixel when needed. Use hsvPixel = reshape(hsvImage(row, col, :), 1, 3); to read the corresponding hue, saturation, and value as a vector.
  5. Format that pixel for a destination. Leave the fractions unchanged for a normalized API, or convert the components to degrees and percentages using the same formulas as for a single color.

Do not apply a second normalization if the image array already contains values from 0 to 1. Also keep grayscale, indexed, and multichannel workflows separate: the example above assumes an RGB array with exactly three color channels.

Check one color with the browser tool

The RGB to HSV Converter is useful when you want to verify one 8-bit RGB triplet or confirm the scale required by another interface. Its supported workflow is straightforward:

  1. Enter whole-number red, green, and blue channel values from 0 through 255.
  2. Select Convert to HSV.
  3. Read hue in degrees and saturation and value as percentages.
  4. Check whether the destination expects degrees and percentages, zero-to-one fractions, or a compact integer range before copying the results.

To compare the browser output with MATLAB, convert one representation into the other. Divide the displayed hue by 360 to obtain MATLAB's hue fraction. Divide the displayed saturation and value percentages by 100 to obtain their fractional equivalents. Comparing a MATLAB hue fraction directly with a degree value would produce a mismatch even when both tools calculated the same color.

The converter has deliberately strict input rules. It accepts only finite whole-number channels from 0 through 255; decimal values, empty fields, negative numbers, and values above 255 produce an error instead of being silently rounded or clamped. It runs locally in the browser and does not retain a color history. It is therefore suitable for checking an individual 8-bit sample, not replacing a MATLAB array operation.

Its output represents an sRGB-style triplet. It does not read embedded image profiles, convert between wide-gamut color spaces, or guarantee that another application will round the displayed numbers identically. Transparency is outside its scope because alpha does not change the HSV calculation for the underlying RGB channels.

Handle achromatic colors and HSV value correctly

Black and any color whose red, green, and blue channels are equal need special interpretation. Black has zero value and zero saturation. Equal channels produce a gray or another achromatic color with zero saturation. In both cases, hue has no visual meaning.

The browser converter reports a conventional placeholder hue of 0 degrees for an achromatic input. Treat that number as a placeholder rather than evidence that the color lies at the red position on the wheel. This distinction is especially important when sorting colors, generating labels, or comparing hue values automatically.

For non-achromatic colors, saturation measures the color's distance from the gray axis relative to its value, while value follows the strongest RGB channel. An HSV value of 100% does not necessarily mean a color will appear as light as white because hue and saturation can still affect its perceived appearance.

Match the destination's numeric scale

HSV does not have one universally required storage format. Compare the destination's documented convention before transferring the result.

Representation Hue Saturation Value Transfer method
MATLAB rgb2hsv result 0 to below 1 0 to 1 0 to 1 Keep the normalized fractions.
Browser converter display 0 to below 360 degrees 0% to 100% 0% to 100% Multiply MATLAB hue by 360; divide converter percentages by 100 to recover fractions.
Compact integer destination Application-specific Application-specific Application-specific Consult its documentation rather than assuming a universal integer scale.

For a detailed comparison of these forms, see the guide to RGB-to-HSV degrees, percentages, and fractions. Some software stores hue in a reduced range such as 0–179 or 0–255 for image-processing convenience. Those ranges are destination-specific and should not be inferred from the mathematical hue circle.

The browser converter's range convention follows the Android Color.colorToHSV reference, which defines hue in degrees and saturation and value in fractional ranges. MATLAB's normalized output and the converter's display can therefore describe the same color even though their raw numbers differ.

Keep the conversion accurate

The converter calculates with full floating-point values and formats the visible result to two decimal places. MATLAB also retains numerical precision in its returned array. If two outputs look slightly different after transfer, check display rounding before suspecting the underlying conversion.

The converter's implementation is independently checked against the R grDevices rgb2hsv convention for typical 0–255 sRGB input, including primary and secondary wheel positions, black, and middle gray. That comparison supports consistent coordinate transformation, but it does not make MATLAB, Android, R, and every graphics package use the same output scale.

Before finishing, verify these points:

  • The MATLAB input is either already normalized or has been divided by 255 exactly once.
  • The RGB channel order has not been reversed.
  • MATLAB fractions are not being compared directly with degrees or percentages.
  • A placeholder hue for gray or black is not interpreted as a meaningful hue.
  • The destination is not silently expected to use HSL instead of HSV.
  • The source is treated as an 8-bit sRGB-style value unless a broader color-space workflow has been established separately.

If you're weighing options, How Do I Plan the Steps to Convert CMYK to RGB? covers this in detail.