In Python OpenCV, use cv2.cvtColor(image, cv2.COLOR_BGR2HSV) for a BGR image or cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV) for an RGB array; for 8-bit input, OpenCV returns hue from 0–179 and saturation and value from 0–255.
The conversion constant must match the actual channel order of the source data. OpenCV's default image loader produces BGR pixels, while a manually constructed NumPy array may contain red, green, and blue in that order. Choosing the wrong constant swaps the red and blue channels, which can change the perceived hue rather than merely relabeling the output.
Channel order and output scale are separate concerns. OpenCV also uses different HSV ranges for different input data types, so successful conversion does not guarantee that the resulting values match an API expecting degrees and percentages. Try the RGB to HSV Converter to do this in your browser.

Choose the constant that matches the source order
OpenCV's conversion-code names describe the order of the input channels. Identify that order before calling cv2.cvtColor.
| Source data | Channel order | Conversion code |
|---|---|---|
| Image loaded with cv2.imread | BGR | cv2.COLOR_BGR2HSV |
| Frame returned by cv2.VideoCapture.read | BGR | cv2.COLOR_BGR2HSV |
| NumPy array explicitly ordered as red, green, blue | RGB | cv2.COLOR_RGB2HSV |
Do not apply COLOR_RGB2HSV to the default output of cv2.imread, because that array is already in BGR order. Conversely, use the RGB constant when the source was deliberately built with the first element as red. If an upstream library supplies another arrangement, convert it to a known order before passing it to OpenCV.
Convert an 8-bit image in OpenCV
-
Prepare the Python environment. If OpenCV is not installed, run python -m pip install opencv-python. In the script, load the library with import cv2.
-
Load the image as an 8-bit BGR array. Use image_bgr = cv2.imread("input.png", cv2.IMREAD_COLOR). The color flag requests a three-channel image, and cv2.imread returns its pixels in BGR order.
-
Check that the file was loaded. Test if image_bgr is None before conversion so a missing or unreadable file does not produce a confusing error later in the script.
-
Convert the BGR array to HSV. Run hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV). The result remains a three-channel array with the same height and width as the input.
-
Inspect the output shape and data type. Commands such as print(image_bgr.shape, image_bgr.dtype) and print(hsv.shape, hsv.dtype) help confirm that the operation preserved the array structure. With an 8-bit source, the HSV result is also 8-bit.
-
Preview the colors in their original display order. Convert the HSV result back to BGR with preview_bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR), then display preview_bgr rather than passing the raw HSV array to cv2.imshow. The latter treats the three stored channels as BGR and produces an incorrect preview.
This workflow is appropriate when the goal is to convert a complete image or use HSV as an intermediate representation for subsequent analysis. It does not require manually rearranging the channels when the source remains in OpenCV's native BGR format.
Convert one RGB triplet without changing its shape
For a single color, create an explicit one-pixel array with shape 1 × 1 × 3. The unambiguous shape prevents the three values from being interpreted as an image dimension and channel count in the wrong order.
import numpy as np
rgb = np.array([[[255, 0, 0]]], dtype=np.uint8)
hsv = cv2.cvtColor(rgb, cv2.COLOR_RGB2HSV)
In this worked example, the RGB triplet is pure red. The returned OpenCV array is [[[0, 255, 255]]]: hue is 0, saturation is 255, and value is 255. The output keeps the same one-pixel, three-channel shape as the input.
If the triplet came from a BGR source, use cv2.COLOR_BGR2HSV instead. Do not reorder the values merely to match the variable name; the conversion code must reflect the actual byte order of the array.
Interpret the returned HSV ranges
The numerical range of an HSV array depends on the source data type. For the common 8-bit path, OpenCV stores a compressed hue range and byte-sized saturation and value channels.
| Output channel | 8-bit range | Meaning |
|---|---|---|
| Hue | 0–179 | A compressed scale corresponding to the 0–360-degree color circle |
| Saturation | 0–255 | 255 represents full saturation |
| Value | 0–255 | The strongest normalized RGB channel |
A float32 input follows different output conventions: hue runs from 0–360, while saturation and value run from 0–1. Therefore, an 8-bit result containing saturation 200 does not mean 200 percent, and the same array interpreted as float data would represent an entirely different range.
Many conventional APIs use hue in degrees and saturation and value as fractions. For example, [Android's Color.colorToHSV](https://developer.android.com/reference/android/graphics/Color.html) defines hue below 360 and saturation and value from 0–1. Check the destination's documented scale before transferring an OpenCV result.
Build HSV thresholds in the output scale
cv2.inRange compares each channel against lower and upper bounds, so those bounds must use the same numeric scale and data type as the HSV array. For an 8-bit conversion, construct the arrays with np.uint8.
lower_hsv = np.array([h_min, s_min, v_min], dtype=np.uint8)
upper_hsv = np.array([h_max, s_max, v_max], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_hsv, upper_hsv)
result = cv2.bitwise_and(image_bgr, image_bgr, mask=mask)
Hue wraps around the end of the color circle. A target interval that crosses 0 degrees may therefore need a second interval near the maximum OpenCV hue value, followed by a bitwise OR of the two masks. Select the bounds from representative pixels and lighting conditions rather than copying values intended for a different HSV scale.
HSV value is the maximum normalized RGB channel. It is not HSL lightness or measured luminance, so it should not be relabeled when the destination expects HSL. For example, code that relies on perceptual lightness needs an HSL-specific conversion and threshold strategy.
Handle achromatic colors and boundary values
When all three RGB channels are equal, the color is achromatic. Its delta is zero, its saturation is zero, and hue has no visual meaning. OpenCV reports hue 0 in this case, as does the conventional placeholder used by many conversion tools. Black follows the same achromatic hue rule, with value also at zero.
This matters when writing thresholds: hue 0 does not always mean a vivid red. It can also represent gray or black. Exclude unwanted low-saturation pixels rather than assuming that a hue-only test identifies a chromatic color.
Keep the source values and data type intact until conversion. Rounding or clamping RGB channels beforehand can select a nearby but different color. Also check the height, width, channel count, and data type of both arrays so that an unexpected source order or dtype does not quietly invalidate a threshold.
Reconcile OpenCV values with degrees and percentages
For a one-color comparison, the [RGB to HSV Converter](/color/rgb-to-hsv/) accepts whole-number red, green, and blue channels from 0 through 255. Enter those channels, select Convert to HSV, and read the hue in degrees together with saturation and value as percentages. The result also includes normalized HSV notation, the source HEX code, and a color swatch for comparison.
The converter uses strict 8-bit-style inputs. Decimal values, empty fields, negative numbers, and values above 255 return an error rather than being silently rounded or clamped. It runs entirely in the browser and does not save a color history.
Do not paste its degree-and-percentage output directly into an 8-bit OpenCV HSV array. For an OpenCV uint8 triplet, the corresponding conventional representations can be derived as degrees = hue × 2, saturation_fraction = saturation / 255.0, and value_fraction = value / 255.0. The OpenCV result is quantized, so this conversion cannot recover precision lost when the hue was stored in its compressed range.
The converter keeps the unrounded result internally and formats displayed hue, saturation, and value to two decimal places. It converts the supplied numeric triplet; it does not read embedded image profiles, convert wide-gamut color spaces, or guarantee that another application will round the displayed values identically. Confirm the expected units in the destination before using the result.