OpenCV converts an RGB triplet or image to HSV with `cv2.cvtColor`, using `COLOR_RGB2HSV` for explicit RGB data and `COLOR_BGR2HSV` for images loaded by OpenCV.
The second decision is the numeric scale: 8-bit OpenCV output uses hue 0–179 and saturation and value 0–255, while float32 output uses hue on a 0–360 scale and saturation and value as 0–1 fractions. Mixing those conventions is a common reason a mathematically correct conversion appears wrong.

The conversion contract in OpenCV
OpenCV represents ordinary image files as three channels, but `cv2.imread` returns those channels in BGR order rather than RGB. The conversion constant tells `cvtColor` how to interpret the input. Use the constant that matches the array you actually have; choosing the other one can swap the meaning of red and blue.
| Data path | Channel order | OpenCV code | Resulting scale |
|---|---|---|---|
| Image loaded with cv2.imread | BGR | cv2.COLOR_BGR2HSV | 8-bit hue 0–179; saturation and value 0–255 |
| Explicit 8-bit array | RGB | cv2.COLOR_RGB2HSV | 8-bit hue 0–179; saturation and value 0–255 |
| Explicit float32 array | RGB normalized to 0–1 | cv2.COLOR_RGB2HSV | Hue on a 0–360 scale; saturation and value as 0–1 fractions |
If the data came from `cv2.imread`, keep it in BGR and use `cv2.COLOR_BGR2HSV`. If another library gave you a true RGB array, use `cv2.COLOR_RGB2HSV`. For a true RGB array, manually reversing it to BGR and using COLOR_BGR2HSV is also valid and equivalent to COLOR_RGB2HSV, because COLOR_BGR2HSV interprets the channel order rather than swapping the channels again.
Convert one RGB triplet in OpenCV
A single color still needs a NumPy array with one pixel and three channels. This is the cleanest way to test a conversion before applying it to an image.
- Import the modules. Run import cv2 and import numpy as np.
- Create an explicit RGB pixel. For RGB (37, 99, 235), run rgb = np.array([[[37, 99, 235]]], dtype=np.uint8).
- Select the RGB conversion. Run hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV) and unpack it with h, s, v = hsv[0, 0].
- Read the values in their native scale. The unpacked numbers are an 8-bit OpenCV hue, saturation, and value; print them only after deciding which scale the destination expects.
Worked conversion for RGB (37, 99, 235)
For the example color, the normalized channels are approximately R = 0.145098, G = 0.388235, and B = 0.921569. The maximum is 0.921569, the minimum is 0.145098, and their difference, delta, is 0.776471. Therefore, value is 0.921569 and saturation is delta divided by maximum, or 0.842553. Because blue is maximal, hue is 60 × (4 + (R − G) / delta) = 221.2121°.
OpenCV quantizes that result for an 8-bit array, producing hsv = [111, 215, 235]: hue in OpenCV's 0–179 range, saturation in 0–255, and value in 0–255. For a float32 pipeline, build rgb_float = np.array([[[37 / 255, 99 / 255, 235 / 255]]], dtype=np.float32) and run the same RGB conversion. That path returns approximately hue 221.2121, saturation 0.842553, and value 0.921569, with saturation and value as fractions.
The two outputs describe the same color, but a later comparison must normalize them. An 8-bit saturation of 215 corresponds to a fraction near 0.843, not to a percentage value that can be inserted unchanged into a float32 API.
Convert a full RGB image in OpenCV
For a file loaded by OpenCV, the complete path is read in BGR, convert to HSV, then convert back to BGR only when a normal color display is needed. The HSV array itself remains the numeric working representation.
- Load and validate the image. Use image_bgr = cv2.imread("input.png", cv2.IMREAD_COLOR) and stop if image_bgr is None.
- Convert the frame. Run hsv_image = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV); every pixel now has three HSV channels.
- Check the result shape. Run print(image_bgr.shape, hsv_image.shape) to confirm that both arrays retain the image height, width, and three-channel depth.
- Create a display image. Run display_bgr = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR), then show it with cv2.imshow("Original", image_bgr) and cv2.imshow("HSV round trip", display_bgr). Close the windows with cv2.waitKey(0) and cv2.destroyAllWindows().
Do not pass hsv_image directly to imshow when the goal is to judge color fidelity: OpenCV's display path expects an image whose channels it can interpret as BGR. The HSV-to-BGR step reconstructs a displayable color image for comparison. If the goal is to preserve the numeric HSV channels for later analysis, save the array with np.save; writing it through cv2.imwrite treats those numbers as ordinary image channels and can produce a misleading preview.
Create degree-and-percentage reference values
Use the RGB to HSV Converter when you want a readable reference before writing or testing OpenCV code. It converts the same kind of 8-bit RGB triplet into a format that makes scale mismatches easier to spot.
- Enter whole-number channels. Supply red, green, and blue from 0 through 255. Decimal inputs, empty fields, negative numbers, and values above 255 produce an error instead of being rounded or clamped.
- Run the conversion. Select Convert to HSV and read hue in degrees, with saturation and value shown as percentages.
- Use the supporting outputs. The result also includes normalized HSV notation, the source HEX code, and a visual swatch, so you can compare the identified color with the original triplet.
- Confirm the destination scale. Check whether the receiving code expects degrees and percentages, zero-to-one fractions, or a compact integer range before copying values.
The calculator runs entirely in the browser and does not save a color history. Its output ranges follow the convention in Android's Color.colorToHSV documentation, and its typical 0–255 sRGB calculations are independently checked against R's grDevices rgb2hsv convention. The tool displays two decimal places, while the underlying result stays unrounded until formatting.
Its scope is deliberately narrow: it handles sRGB-style triplets, not embedded image profiles or wide-gamut conversions, and alpha is outside the conversion because transparency does not change the underlying three RGB channels.
Map values and check edge cases
To compare the converter with OpenCV's 8-bit output, map hue degrees to the half-degree 8-bit scale, then convert saturation and value percentages to 0–255 integers. For a float32 target, retain hue in degrees and divide the saturation and value percentages by 100. These are comparison rules, not a reason to relabel one output as another: a destination that expects fractions must receive fractions, while a destination that expects percentages must receive percentages.
Several software packages instead compress hue into 0–179 or 0–255. Confirm the destination's documentation instead of assuming that every HSV tuple uses the same integers. For a focused explanation of OpenCV's hue scale, see Convert RGB to HSV in OpenCV: Match the Exact HSV Scale.
Achromatic colors need special attention. When red, green, and blue are equal, saturation is zero and hue has no visual meaning; the browser converter reports the conventional placeholder hue of 0°. For black, the maximum channel is also zero, so saturation is zero and value is zero. These conventions make conversion deterministic, but they do not make hue meaningful for gray.
Finally, HSV value is not HSL lightness: HSV value follows the strongest normalized RGB channel, whereas HSL lightness uses the maximum and minimum channels together. A round-trip HSV image should look like the original when converted back through the matching BGR or RGB conversion code, but that visual check does not replace numeric scale checks.
Related reading: Generate SVG Code for Patterns Without Path Markup.