Documentation Index
Fetch the complete documentation index at: https://mintlify.com/opencv/opencv/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Color spaces are different ways of representing colors numerically. OpenCV supports numerous color space conversions through thecvtColor() function.
BGR Color Space
Default in OpenCV
OpenCV uses BGR (Blue-Green-Red) as its default color format:Common Color Spaces
RGB/BGR
Use case: Display, most common representationGrayscale
Use case: Simplify processing, reduce computationHSV (Hue-Saturation-Value)
Use case: Color-based segmentation, lighting-independent processing- Hue: Color type (0-179 in OpenCV)
- Saturation: Color intensity (0-255)
- Value: Brightness (0-255)
LAB (Lab*)
Use case: Perceptually uniform, skin detection- L: Lightness (0-100)
- a: Green-Red axis
- b: Blue-Yellow axis
YCrCb
Use case: Video compression, skin detection- Y: Luminance
- Cr: Red-difference
- Cb: Blue-difference
Color Conversion
Basic Conversion
Common Conversion Codes
| From | To | Code |
|---|---|---|
| BGR | Gray | COLOR_BGR2GRAY |
| BGR | RGB | COLOR_BGR2RGB |
| BGR | HSV | COLOR_BGR2HSV |
| BGR | LAB | COLOR_BGR2Lab |
| BGR | YCrCb | COLOR_BGR2YCrCb |
| HSV | BGR | COLOR_HSV2BGR |
| Gray | BGR | COLOR_GRAY2BGR |
Practical Examples
Color Detection
Lighting Normalization
Skin Detection
Color Space Selection
HSV
- Color-based segmentation
- Lighting-independent tracking
- Hue provides rotation invariance
LAB
- Perceptually uniform
- Separate luminance from color
- Better for color difference calculations
YCrCb
- Video compression
- Skin detection
- Chroma subsampling
Grayscale
- Simplest representation
- Fastest processing
- Use when color not needed
Best Practices
Conversion Tips
- Minimize conversions: Convert once, cache result
- Choose appropriate space: Match algorithm requirements
- Remember value ranges: HSV hue is 0-179, not 0-255
- Consider precision: Use CV_32F for sensitive operations
Performance
See Also
- Image Basics - Working with images
- ImgProc Module - Image processing functions
- cvtColor Reference
